java 尝试在空对象引用上调用虚拟方法“...”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41532198/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Attempt to invoke virtual method '...' on a null object reference
提问by ahsen mughal
I getting the "Attempt to invoke virtual method 'java.lang.String[] android.os.Bundle.getStringArray(java.lang.String)' on a null " error in some devices:
我在某些设备中收到“Attempt to invoke virtual method 'java.lang.String[] android.os.Bundle.getStringArray(java.lang.String)' on a null”错误:
Stack Trace:
堆栈跟踪:
Exception java.lang.RuntimeException: Unable to start activity ComponentInfo{com.facebook.misstest/com.facebook.misstest.LoveMeterResultActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String[] android.os.Bundle.getStringArray(java.lang.String)' on a null object reference
android.app.ActivityThread.performLaunchActivity (ActivityThread.java:3254)
android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:3350)
android.app.ActivityThread.access00 (ActivityThread.java:222)
android.app.ActivityThread$H.handleMessage (ActivityThread.java:1795)
android.os.Handler.dispatchMessage (Handler.java:102)
android.os.Looper.loop (Looper.java:158)
android.app.ActivityThread.main (ActivityThread.java:7229)
java.lang.reflect.Method.invoke (Method.java)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run
(ZygoteInit.java:1230)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1120)
Caused by java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String[] android.os.Bundle.getStringArray(java.lang.String)' on a null object reference
com.facebook.misstest.LoveMeterResultActivity.onCreate
(LoveMeterResultActivity.java:133)
android.app.Activity.performCreate (Activity.java:6876)
android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1135)
android.app.ActivityThread.performLaunchActivity (ActivityThread.java:3207)
android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:3350)
Here is the code which is send data to the next activity:
这是将数据发送到下一个活动的代码:
//send this result to LoveMeterResultActivity
final Bundle basket = new Bundle();
basket.putStringArray("key", loveData);
Intent intent = new Intent(LoveMeterActivity.this, LoveMeterResultActivity.class);
intent.putExtras(basket);
startActivity(intent);
采纳答案by Charu?
Check intent is null or not before you get data
在获取数据之前检查意图是否为空
Intent intent=this.getIntent();
if(intent !=null){
// do something you want
}
example
例子
In ClassA:
在 A 类中:
Intent intent = new Intent(this, ClassB);
String[] myStrings = new String[] {"test", "test2"};
intent.putExtra("strings", myStrings);
startActivity(intent);
In ClassB:
B类:
Intent intent = getIntent();
if(intent !=null){
String[] myStrings = intent.getStringArrayExtra("strings");
}