android - Xamarin StartActivity ActionView - NullPointerException 尝试调用虚拟方法

标签 android xamarin.forms auth0

我正在尝试创建一个使用 Auth0 服务进行登录的 Xamarin Forms 应用程序。我一直在按照他们关于如何使其工作的说明进行操作,但似乎每次都崩溃。

uri 已创建且有效,因此客户端的实现看起来很好,但是当调用 StartActivity 行时,我收到此错误:

Attempt to invoke virtual method 'android.app.ActivityThread$ApplicationThread android.app.ActivityThread.getApplicationThread()' on a null object reference

我认为这与我使用 DependencyService 能够调用 Android 和 iOS 项目中的函数并且它以异步方式运行但我找不到任何其他方法来使其工作有关。

我尝试使用 RunOnUiThread 和 BeginInvokeOnMainThread,但结果还是一样。

我遵循了这些说明和示例:

Auth0 Instructions

Github Example

因此,我创建了一个简单的 View ,其中包含一个按钮,点击即可使用他们的服务登录。此按钮调用委托(delegate)服务,以便能够在 Android 和 iOS 中启动指令

登录按钮的 XAML 代码

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="App.LoginPage">
<ContentPage.Content>
    <StackLayout VerticalOptions="StartAndExpand">
        <Button x:Name="LoginButton" Text="Login / Signup" Clicked="OnLoginSignupButtonClicked" />
    </StackLayout>
</ContentPage.Content>

调用依赖服务的按钮代码

void OnLoginSignupButtonClicked(object sender, EventArgs e)
{
    DependencyService.Get<ILoginButton>().LoginRegisterButtonTap();
}

然后我在方法的实现中执行他们提供的代码

public async void LoginRegisterButtonTap()
{
    // Prepare for the login
    try
    {
        authorizeState = await client.PrepareLoginAsync();
        // Send the user off to the authorization endpoint
        var uri = Android.Net.Uri.Parse(authorizeState.StartUrl);
        var intent = new Intent(Intent.ActionView, uri);
        intent.AddFlags(ActivityFlags.NoHistory);
        StartActivity(intent);
    } catch (Exception e)
    {
        Console.WriteLine(e.Message);
    }
 }

最佳答案

TLDR 出现问题是因为我需要从 MainActivity 指定应用程序上下文,现在一切正常。

Application.Context.StartActivity(intent);

我花了很多时间研究这个问题,解决方案是关于如何从 Android 项目在 Xamarin 中创建新 Intent 的简单教程,并且它有效。

关于android - Xamarin StartActivity ActionView - NullPointerException 尝试调用虚拟方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49592419/

相关文章:

Android - 如何禁用底部工作表的 STATE_HALF_EXPANDED 状态

android - Droid 辅助功能 - 禁用特定 TextView 的 TalkBack

xaml - 如何在 Xamarin 中调整按钮的大小

reactjs - useAuth0().user 不显示用户角色

javascript - Passport JS 和 Auth0 之间有什么关系?

android - 显示图像字符串资源(xml)android

android - 从 Activity 外部调用 findViewById()

ios - 为什么在 Xamarin IOS customrenderer 中没有准确检测到长按开始/结束?

c# - 通过 Xamarin Forms 在 XAML 中使用 LINQ

graphql - 如何在 Graphql 架构级别检查 AWS AppSync OIDC 指令的角色或权限