我一直在通过三星S7 Phone(API 23和6.0.1)在Android Studio模拟器中测试我的应用程序,而且工作正常。当我从笔记本电脑上拔下插头并运行应用程序崩溃的应用程序。
我的logcat说我试图调用一个空数组...但是不是空的吗?我不明白为什么当我通过Android Studio运行该应用到手机时,一切都可以正常工作,但是当我独自运行时,它会崩溃。感谢任何帮助!
这是我的logcat
FATAL EXCEPTION: main
Process: com.example.abc.def, PID: 28693
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.abc.def/com.example.abc.def.MainActivity}: java.lang.NullPointerException: Attempt to read from null array
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3253)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)
at android.app.ActivityThread.access$1100(ActivityThread.java:221)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.NullPointerException: Attempt to read from null array
at com.example.abc.def.MainActivity.onCreate(MainActivity.java:49)
at android.app.Activity.performCreate(Activity.java:6876)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3206)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)
at android.app.ActivityThread.access$1100(ActivityThread.java:221)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
这是我的代码。当我调用btn1.setText(itemarray [0])时,问题出现在底部,因为数组为" null"
public class MainActivity extends AppCompatActivity {
Button btn1, btn2;
String btn1Name, btn2Name;
String btnValue = "", itemValue = "", itemPosition = "";
String[] buttonArray = new String[]{"blank", "blank"};
String[] itemArray = new String[]{"blank", "blank"};
String[] positionArray = new String[]{"blank", "blank"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button) findViewById(R.id.button1);
btn2 = (Button) findViewById(R.id.button2);
Bundle extras = getIntent().getExtras();
if (extras != null) {
btnValue = extras.getString("btnValue");
itemValue = extras.getString("itemValue");
itemPosition = extras.getString("itemPosition");
buttonArray = extras.getStringArray("buttonArray");
itemArray = extras.getStringArray("array");
positionArray = extras.getStringArray("positionArray");
if (btnValue != null && btnValue.equals("btn1")){
buttonArray[0] = btnValue;
itemArray[0] = itemValue;
positionArray[0] = itemPosition;
} else if (btnValue != null && btnValue.equals("btn2")) {
buttonArray[1] = btnValue;
itemArray[1] = itemValue;
positionArray[1] = itemPosition;
}
}
btn1.setText(itemArray[0]);
btn2.setText(itemArray[1]);
}
}
我在这里猜,但是主动是项目中的第一个活动吗?
我认为您编写了代码来处理其他活动的数据返回此活动。我刚刚尝试过, getIntent()。getExtras()首先在模拟器上创建活动时返回null。但是,当我在设备上运行它时,GetIntent生成了此例外。
请注意,on创建一次是一次,因此,如果您想查看另一个活动的结果,请查看如何在Android上管理" StartactivityForresult"。