getStringExtra给出了java.lang.NullPointerException错误

时间:2015-08-06 08:00:05

标签: android

我将值发送到pag2.java,如下所示

Intent myIntent = new Intent(getApplicationContext(), page2.class);
        intent.putExtra("pos", list1);
        intent.putExtra("back1", "back");
        startActivity(myIntent);

并在第2页中接收

Intent intent = getIntent();
        pos = intent.getStringExtra("pos");

我收到以下错误:

FATAL EXCEPTION: main
Process: com.example.namrathasrinivas.karnatakatemples, PID: 4410
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.namrathasrinivas.karnatakatemples/com.example.namrathasrinivas.karnatakatemples.page2}: java.lang.NullPointerException: println needs a message
        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: println needs a message
        at android.util.Log.println_native(Native Method)
        at android.util.Log.d(Log.java:139)
        at com.example.namrathasrinivas.karnatakatemples.page2.onCreate(page2.java:41)
        at android.app.Activity.performCreate(Activity.java:5231)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)

      

2 个答案:

答案 0 :(得分:0)

使用此:

Bundle bundle = getIntent().getExtras();
    String str =  bundle.getString("pos");

答案 1 :(得分:0)

首先,尝试这种方式,因为您可能在活动之间传输数据时出错(它与savedInstanceState有关)

Bundle extras = getIntent().getExtras();
if (extras != null) String pos = extras.getString("pos");

如果这仍然无效,那么您的清单可能会出现问题。您是否以适当的方式宣布了接收活动?