Android:NPE while trying to activityForResult with

2020-04-12 08:30发布

I have a problem then trying to put an intent with AppWidgetManager.ACTION_APPWIDGET_PICK there is a problem somethere inside of android AppWidget ecosystem as I can see from the logs. So that I doing wrong?
See sample code and stack trace below

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);
    }
}

stack trace from log cat:

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)

Another small question, in which of android projects does com.android.settings live in? (I'm stuck with windows so can't use repo unfortunatly) Help really apreciated.

3条回答
太酷不给撩
2楼-- · 2020-04-12 08:44

Did you determine a workaround? I've seen apps which implement the appwidget picker to run appwidget host in their activity, and I am now receiving this exact error stating EXTRA_CUSTOM_INFO is not present. I am testing some things from the Launcher source usage of this and will report back if I can workaround the error at least.

查看更多
别忘想泡老子
3楼-- · 2020-04-12 08:47

Edit: spoke too soon, didn't see it was a ViewListener method. Don't know if that's still required.


Original answer:

You should call the Acitivy's onClick in your override.

From the Android docs: "An implementation of any activity lifecycle method should always first call the superclass version.

Hope that solves the problem.

查看更多
欢心
4楼-- · 2020-04-12 08:51

Looks like a bug in android settings, if you trying to pick appwidget without any extra widgets specified you'll fail, filled an issue at google code link

查看更多
登录 后发表回答