Xamarin NPE磨损确认活动

时间:2019-07-02 07:58:55

标签: android xamarin wear-os android-wear-2.0

我正在开发Wear应用程序,并希望在单击后向用户显示确认消息。

我知道对于Wear来说,存在专用工具(如ConfirmationActivity),但是在尝试显示该工具后,我收到了空指针异常:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageDrawable(android.graphics.drawable.Drawable)' on a null object reference

这是引起NPE的有害代码:

Intent intent = new Intent(ApplicationContext, typeof(ConfirmationActivity));
intent.PutExtra(ConfirmationActivity.ExtraAnimationType, ConfirmationActivity.SuccessAnimation);
StartActivity(intent);

这是应用程序清单(注意,我声明为ConfirmationActivity):

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="xx.xxxxxxx.xxxxxxx" android:installLocation="auto">
    <uses-sdk android:minSdkVersion="26" android:targetSdkVersion="28" />
    <uses-feature android:name="android.hardware.type.watch" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
    <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@android:style/Theme.DeviceDefault">
        <uses-library android:name="com.google.android.wearable" android:required="true" />
        <!--
      Set to true if your app is Standalone, that is, it does not require the handheld
      app to run.
    -->
        <meta-data android:name="com.google.android.wearable.standalone" android:value="true" />
        <receiver android:name="com.google.firebase.iid.FirebaseInstanceIdInternalReceiver" android:exported="false" />
        <receiver android:name="com.google.firebase.iid.FirebaseInstanceIdReceiver" android:exported="true" android:permission="com.google.android.c2dm.permission.SEND">
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
                <category android:name="${applicationId}" />
            </intent-filter>
        </receiver>
        <receiver android:name=".BootReceiver" android:enabled="true" android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </receiver>
    <activity android:name="android.support.wearable.activity.ConfirmationActivity"></activity>
    </application>
</manifest>

这是堆栈跟踪:

07-02 07:41:50.270 E/AndroidRuntime( 6760): java.lang.RuntimeException: Unable to start activity ComponentInfo{xx.xxxxxxxx.xxxxxx/android.support.wearable.activity.ConfirmationActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageDrawable(android.graphics.drawable.Drawable)' on a null object reference
07-02 07:41:50.270 E/AndroidRuntime( 6760):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
07-02 07:41:50.270 E/AndroidRuntime( 6760):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
07-02 07:41:50.270 E/AndroidRuntime( 6760):     at android.app.ActivityThread.-wrap11(Unknown Source:0)
07-02 07:41:50.270 E/AndroidRuntime( 6760):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
07-02 07:41:50.270 E/AndroidRuntime( 6760):     at android.os.Handler.dispatchMessage(Handler.java:105)
07-02 07:41:50.270 E/AndroidRuntime( 6760):     at android.os.Looper.loop(Looper.java:164)
07-02 07:41:50.270 E/AndroidRuntime( 6760):     at android.app.ActivityThread.main(ActivityThread.java:6541)
07-02 07:41:50.270 E/AndroidRuntime( 6760):     at java.lang.reflect.Method.invoke(Native Method)
07-02 07:41:50.270 E/AndroidRuntime( 6760):     at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
07-02 07:41:50.270 E/AndroidRuntime( 6760):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
07-02 07:41:50.270 E/AndroidRuntime( 6760): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageDrawable(android.graphics.drawable.Drawable)' on a null object reference
07-02 07:41:50.270 E/AndroidRuntime( 6760):     at android.support.wearable.view.ConfirmationOverlay.updateImageView(ConfirmationOverlay.java:302)
07-02 07:41:50.270 E/AndroidRuntime( 6760):     at android.support.wearable.view.ConfirmationOverlay.updateOverlayView(ConfirmationOverlay.java:249)
07-02 07:41:50.270 E/AndroidRuntime( 6760):     at android.support.wearable.view.ConfirmationOverlay.showOn(ConfirmationOverlay.java:194)
07-02 07:41:50.270 E/AndroidRuntime( 6760):     at android.support.wearable.activity.ConfirmationActivity.onCreate(ConfirmationActivity.java:79)
07-02 07:41:50.270 E/AndroidRuntime( 6760):     at android.app.Activity.performCreate(Activity.java:6975)
07-02 07:41:50.270 E/AndroidRuntime( 6760):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
07-02 07:41:50.270 E/AndroidRuntime( 6760):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)

1 个答案:

答案 0 :(得分:0)

我认为您的上下文不正确。试试这个:

Intent intent = new Intent (this, typeof(ConfirmationActivity))
    .SetFlags (ActivityFlags.NewTask | ActivityFlags.NoAnimation)
    .PutExtra (ConfirmationActivity.ExtraAnimationType, ConfirmationActivity.SuccessAnimation)
    .PutExtra (ConfirmationActivity.ExtraMessage, "Message Goes Here");
StartActivity (intent);

如果这也不起作用,那么您可能还必须覆盖Intent处理功能,如您所见herehere