getIntent extras总是返回null

时间:2014-07-30 18:19:08

标签: android nullpointerexception

我有3个活动,ListAcitive,InitialChoose和MyActivity。 InitialChoose向MyActivity发送额外内容,但在MyActivity的oncCreate方法中,附加内容始终为null。我在网站上看到过很多类似的问题,但他们的方法对我没有帮助。

InitialChoose.java

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ...
        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Bundle bun = new Bundle();
                Intent intent = new Intent(InitialChoose.this, MyActivity.class);
                bun.putIntArray("date", new int[]{month + 1, day, year});
                bun.putString("template", template.getSelectedItem().toString());
                bun.putString("property", property.getSelectedItem().toString());
                bun.putString("unit", unit.getSelectedItem().toString());
                bun.putString("type", type.getSelectedItem().toString());
                bun.putString("status", status.getSelectedItem().toString());
                bun.putString("customer", customer.getText().toString());
                bun.putString("description", description.getText().toString());
                bun.putString("inspector", inspector.getText().toString());
                intent.putExtra("bundle", bun);
                startActivity(new Intent(InitialChoose.this, MyActivity.class));
            }
        });
    }

MyActivity.java:

protected void onCreate(Bundle savedInstanceState) {
        selectedItems = new ArrayList<String>();
        super.onCreate(savedInstanceState);
        Bundle extras = getIntent().getBundleExtra("bundle");
            date = extras.getIntArray("date");
            customer = extras.getString("customer");
            System.out.println("Customer: 1: " + customer);
            description = extras.getString("description");
            inspector = extras.getString("inspector");
            template = extras.getString("template");
            property = extras.getString("property");
            unit = extras.getString("unit");
            type = extras.getString("type");
            status = extras.getString("status");
            ...
}

具体来说,这是我得到的错误:

07-30 14:07:30.217  17913-17913/viva.inspection.com.inspectionpicker E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: viva.inspection.com.inspectionpicker, PID: 17913
    java.lang.RuntimeException: Unable to start activity ComponentInfo{viva.inspection.com.inspectionpicker/viva.inspection.com.inspectionpicker.MyActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at viva.inspection.com.inspectionpicker.MyActivity.onCreate(MyActivity.java:53)
            at android.app.Activity.performCreate(Activity.java:5231)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:3)

你不应该感到惊讶,为什么你得到NullPointerException,因为你实际上并没有正确地设置数据。变化:

intent.putExtra("bundle", bun);

intent.putExtras(bun);

以及

startActivity(new Intent(InitialChoose.this, MyActivity.class));

startActivity(intent);