您好我正在尝试将2个字符串发送到另一个活动,但我收到空异常错误,这是我的第一个活动的代码:
Intent zistactivity = new Intent(reshte.this,List1.class);
zistactivity.putExtra("url","http://api.safshari.com/test.json");
zistactivity.putExtra("daste", "zist");
startActivity(zistactivity);
我的第二项活动是:
String url = getIntent().getExtras().getString("url");
String daste = getIntent().getExtras().getString("daste");
和logcat:
06-26 13:36:20.323: E/AndroidRuntime(597): FATAL EXCEPTION: main
06-26 13:36:20.323: E/AndroidRuntime(597): Process: com.safshari.konkor, PID: 597
06-26 13:36:20.323: E/AndroidRuntime(597): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.safshari.konkor/com.safshari.konkor.List1}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference
06-26 13:36:20.323: E/AndroidRuntime(597): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2236)
06-26 13:36:20.323: E/AndroidRuntime(597): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
06-26 13:36:20.323: E/AndroidRuntime(597): at android.app.ActivityThread.access$800(ActivityThread.java:151)
06-26 13:36:20.323: E/AndroidRuntime(597): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
06-26 13:36:20.323: E/AndroidRuntime(597): at android.os.Handler.dispatchMessage(Handler.java:102)
06-26 13:36:20.323: E/AndroidRuntime(597): at android.os.Looper.loop(Looper.java:135)
06-26 13:36:20.323: E/AndroidRuntime(597): at android.app.ActivityThread.main(ActivityThread.java:5254)
06-26 13:36:20.323: E/AndroidRuntime(597): at java.lang.reflect.Method.invoke(Native Method)
06-26 13:36:20.323: E/AndroidRuntime(597): at java.lang.reflect.Method.invoke(Method.java:372)
06-26 13:36:20.323: E/AndroidRuntime(597): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
06-26 13:36:20.323: E/AndroidRuntime(597): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
06-26 13:36:20.323: E/AndroidRuntime(597): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference
06-26 13:36:20.323: E/AndroidRuntime(597): at com.safshari.konkor.List1.<init>(List1.java:32)
06-26 13:36:20.323: E/AndroidRuntime(597): at java.lang.reflect.Constructor.newInstance(Native Method)
06-26 13:36:20.323: E/AndroidRuntime(597): at java.lang.Class.newInstance(Class.java:1606)
06-26 13:36:20.323: E/AndroidRuntime(597): at android.app.Instrumentation.newActivity(Instrumentation.java:1066)
06-26 13:36:20.323: E/AndroidRuntime(597): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2226)
答案 0 :(得分:0)
在发件人活动中:
editPictureViewController
在接收者活动中:
Intent intent = new Intent(reshte.this,List1.class);
Bundle bundle = new Bundle();
bundle.putString("key1","value1");
(...)
intent.putExtras(bundle);
// Launch intent
试试吧。您必须将所有传递的数据放入一个包中,以获得更好的体验(至少在我看来)
答案 1 :(得分:0)
如果声明了两个活动,请检查清单文件。参考日志:
06-26 13:36:20.323: E/AndroidRuntime(597): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.safshari.konkor/com.safshari.konkor.List1}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference
答案 2 :(得分:0)
您使用Intent而不是Bundle发送字符串值。所以使用getStringExtra从
Intent intent=this.getIntent();
if(intent !=null)
value = intent.getStringExtra("variable");
答案 3 :(得分:0)
If you are putting extra directly, use get(Type)Extra instead.
答案 4 :(得分:-1)
Bundle bundle = getIntent().getExtras();
String url= bundle.getString("url");
//bundle contain all String data