安卓:NPE尝试与ACTION_APPWIDGET_PICK意图activityForResult(

2019-08-18 12:25发布

我有那么一个问题,试图把有意向AppWidgetManager.ACTION_APPWIDGET_PICK有Android的生态系统AppWidget里面,我可以从日志中看到的一个问题somethere。 所以,我做错了什么?
看的示例代码和堆栈跟踪下面

public class NPEDemoActivity extends Activity {

    private final static int HOST_CODE = 1024;

    private AppWidgetHost host;
    private AppWidgetManager manager;
    private int PICK_WIDGET_RC = 1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);        
        setContentView(R.layout.main);
        host = new AppWidgetHost(this, HOST_CODE);
        host.startListening();
        manager = AppWidgetManager.getInstance(this);
        Button b = (Button)findViewById(R.id.Button01);
        b.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                int nextId = host.allocateAppWidgetId();
                Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);             
                pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, nextId);
                startActivityForResult(pickIntent, PICK_WIDGET_RC);   
            }
        });
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }
}

堆栈日志跟踪猫:

10-15 17:33:06.873: ERROR/AndroidRuntime(821): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.settings/com.android.settings.AppWidgetPickActivity}: java.lang.NullPointerException
10-15 17:33:06.873: ERROR/AndroidRuntime(821):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2401)
10-15 17:33:06.873: ERROR/AndroidRuntime(821):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
10-15 17:33:06.873: ERROR/AndroidRuntime(821):     at android.app.ActivityThread.access$2100(ActivityThread.java:116)
10-15 17:33:06.873: ERROR/AndroidRuntime(821):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
10-15 17:33:06.873: ERROR/AndroidRuntime(821):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-15 17:33:06.873: ERROR/AndroidRuntime(821):     at android.os.Looper.loop(Looper.java:123)
10-15 17:33:06.873: ERROR/AndroidRuntime(821):     at android.app.ActivityThread.main(ActivityThread.java:4203)
10-15 17:33:06.873: ERROR/AndroidRuntime(821):     at java.lang.reflect.Method.invokeNative(Native Method)
10-15 17:33:06.873: ERROR/AndroidRuntime(821):     at java.lang.reflect.Method.invoke(Method.java:521)
10-15 17:33:06.873: ERROR/AndroidRuntime(821):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
10-15 17:33:06.873: ERROR/AndroidRuntime(821):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
10-15 17:33:06.873: ERROR/AndroidRuntime(821):     at dalvik.system.NativeStart.main(Native Method)
10-15 17:33:06.873: ERROR/AndroidRuntime(821): Caused by: java.lang.NullPointerException
10-15 17:33:06.873: ERROR/AndroidRuntime(821):     at com.android.settings.AppWidgetPickActivity.putAppWidgetItems(AppWidgetPickActivity.java:170)
10-15 17:33:06.873: ERROR/AndroidRuntime(821):     at com.android.settings.AppWidgetPickActivity.putCustomAppWidgets(AppWidgetPickActivity.java:132)
10-15 17:33:06.873: ERROR/AndroidRuntime(821):     at com.android.settings.AppWidgetPickActivity.getItems(AppWidgetPickActivity.java:208)
10-15 17:33:06.873: ERROR/AndroidRuntime(821):     at com.android.settings.ActivityPicker.onCreate(ActivityPicker.java:99)
10-15 17:33:06.873: ERROR/AndroidRuntime(821):     at com.android.settings.AppWidgetPickActivity.onCreate(AppWidgetPickActivity.java:63)
10-15 17:33:06.873: ERROR/AndroidRuntime(821):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
10-15 17:33:06.873: ERROR/AndroidRuntime(821):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)

另一个小问题,其中机器人项目的支持com.android.settings住呢? (我卡与Windows所以不能用回购不幸的)帮助真正apreciated。

Answer 1:

看起来像在Android设置中的错误,如果你试图挑选appwidget没有规定你会失败的任何额外的部件,在谷歌代码填补了一个问题链接



Answer 2:

编辑 :说话太快,没看到这是一个ViewListener方法。 不知道这是否仍然需要。


原来的答案

你应该叫Acitivy的onClick你覆盖。

从Android 文档 :“ 任何活动的生命周期方法的实现应该总是首先调用超类版本

希望解决这个问题。



Answer 3:

你确定一个解决方法吗? 我见过它们实现appwidget选择器在他们的活动运行appwidget主机的应用程序,现在我收到这个确切的错误,说明EXTRA_CUSTOM_INFO不存在。 我从这个发射源的使用测试一些东西,将报告回来,如果我至少可以解决该错误。



文章来源: Android:NPE while trying to activityForResult with ACTION_APPWIDGET_PICK intent