第二次配置启动时App小部件崩溃

时间:2016-12-20 09:12:17

标签: android android-appwidget

我正在开发一个app小部件,当小部件移动到屏幕时会启动配置活动。我第一次将小部件放在屏幕上时,配置启动并成功运行。删除小部件后,再次将其放在屏幕上,活动崩溃,logcat显示以下内容:

E/AndroidRuntime(15830): FATAL EXCEPTION: main
E/AndroidRuntime(15830): Process: com.lge.launcher2, PID: 15830
E/AndroidRuntime(15830): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=5, result=-1, data=Intent { (has extras) }} to activity {com.lge.launcher2/com.lge.launcher2.Launcher}: java.lang.NullPointerException: Attempt to read from field 'android.content.ComponentName android.appwidget.AppWidgetProviderInfo.provider' on a null object reference
E/AndroidRuntime(15830):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3554)
E/AndroidRuntime(15830):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3597)
E/AndroidRuntime(15830):    at android.app.ActivityThread.access$1300(ActivityThread.java:148)
E/AndroidRuntime(15830):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1332)
E/AndroidRuntime(15830):    at android.os.Handler.dispatchMessage(Handler.java:102)
E/AndroidRuntime(15830):    at android.os.Looper.loop(Looper.java:135)
E/AndroidRuntime(15830):    at android.app.ActivityThread.main(ActivityThread.java:5272)
E/AndroidRuntime(15830):    at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(15830):    at java.lang.reflect.Method.invoke(Method.java:372)
E/AndroidRuntime(15830):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:909)
E/AndroidRuntime(15830):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704)
E/AndroidRuntime(15830): Caused by: java.lang.NullPointerException: Attempt to read from field 'android.content.ComponentName android.appwidget.AppWidgetProviderInfo.provider' on a null object reference
E/AndroidRuntime(15830):    at com.lge.launcher2.views.LauncherAppWidgetHostView.<init>(LauncherAppWidgetHostView.java:71)
E/AndroidRuntime(15830):    at com.lge.launcher2.application.LauncherAppWidgetHost.onCreateView(LauncherAppWidgetHost.java:56)
E/AndroidRuntime(15830):    at android.appwidget.AppWidgetHost.createView(AppWidgetHost.java:313)
E/AndroidRuntime(15830):    at com.lge.launcher2.application.WidgetManager.createView(WidgetManager.java:105)
E/AndroidRuntime(15830):    at com.lge.launcher2.Launcher.completeAddAppWidget(Launcher.java:1670)
E/AndroidRuntime(15830):    at com.lge.launcher2.Launcher.completeAdd(Launcher.java:649)
E/AndroidRuntime(15830):    at com.lge.launcher2.Launcher.onActivityResult(Launcher.java:685)
E/AndroidRuntime(15830):    at android.app.Activity.dispatchActivityResult(Activity.java:6179)
E/AndroidRuntime(15830):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3550)
E/AndroidRuntime(15830):    ... 10 more
W/ActivityManager( 1022):   Force finishing activity com.lge.launcher2/.Launcher
W/ResourcesManager(14274): Asset path '/system/framework/com.google.android.maps.jar' does not exist or contains no resources.
W/ResourcesManager(14274): Asset path '/system/framework/com.lge.mdm.jar' does not exist or contains no resources.
W/ResourcesManager(14274): Asset path '/system/framework/com.lge.telephony.sms.jar' does not exist or contains no resources.
W/ResourcesManager(14274): Asset path '/system/framework/lghiddenlibs.jar' does not exist or contains no resources.
W/ResourcesManager(14274): Asset path '/system/framework/qcrilhook.jar' does not exist or contains no resources.
W/PhoneWindowManagerEx( 1022): Call!!!getLGSystemUiVisibility. =0x0
D/StatusBarManagerServiceEx( 1022): setLGSystemUiVisibility(0x0)
D/StatusBarManagerServiceEx( 1022): manageNaviBtnDisableList userId=0 what=0x0 pkg=WindowManager.LayoutParams

我按照this question中的建议切换了“不要保持活动”配置,但这并未改变任何内容。 我的测试系统是运行Lollipop的LG3设备。导致崩溃的原因是什么?

其他要点

  1. 如果我在第一个输入屏幕上等待一段时间(TBD确切多长时间),则会发生同样的崩溃。
  2. 我将结果设置为onCreate,如下所示:

    Intent resultValue = new Intent();  resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,appWidgetId);  setResult(RESULT_CANCELED,resultValue);

  3. 当用户完成配置过程时,如下所示:

     Intent resultValue = new Intent();
     resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
     setResult(RESULT_OK, resultValue);
     finish();
    
    1. 我的问题可能与this question有关,但尚未得到解答。

2 个答案:

答案 0 :(得分:0)

重新生成配置活动时,无论是在旋转屏幕还是放置新窗口小部件,我都希望立即退出配置活动,因此我在onCreate()中间调用了finish()。这样做了很糟糕的事情,并在问题中引起了异常。

答案 1 :(得分:0)

AppWidgetId有效也不仅意味着它已经传递给活动,而且它已经在Android的AppWidgetManager中注册。 默认情况下会在调用配置活动之前发生。

但是,在我的情况下,每当调用onUpdate时,我都会从Android的AppWidgetManager手动删除appWidgetIds,我的应用程序不知道首选项。获得的经验教训 - 不要改变Android的appwidget状态。