我有2个活动,这是MainActivity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(Tag, "onCreate Main");
setContentView(R.layout.activity_main);
Log.d(Tag, "set contentview ");
Intent intent = getIntent();
Log.d(Tag, "intent Main");
name1 = intent.getStringExtra("playerName1");
name2 = intent.getStringExtra("playerName2");
Log.d(Tag, "get names Main");
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
Log.d(Tag, "toolbar");
setSupportActionBar(toolbar);
Log.d(Tag, "setsupport");
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
Log.d(Tag, "floating");
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d(Tag, "onclick");
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
//assign buttons to a button array
for (int i = 0; i < buttons.length; ++i) {
buttons[i] = (Button) findViewById(id);
buttons[i].setOnClickListener(this);
isClicked[i] = false;
xClicked[i] = false;
oClicked[i] = false;
//initialization of the string array
s[i] = "";
id++;
}
Log.d(Tag, "assigning finished");
}
这是将信息发送到MainActivity
的活动protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(Tag, "onCreate");
setContentView(R.layout.activity_user_name);
Log.d(Tag, "setContentView");
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
btnName = (Button) findViewById(id);
btnName.setOnClickListener(this);
}
所以,正如你所看到的,我已经使用logcat进行调试,崩溃发生在这一行之后:
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
Log.d(Tag, "floating");
我无法弄清楚问题是什么......我需要帮助!!
如果我不是非常具体,我道歉,这是堆栈跟踪!
10-18 22:54:21.493 12068-12068/edu.ncc.hkim.tictactoepart2 D/Checking: floating
10-18 22:54:21.499 12068-12068/edu.ncc.hkim.tictactoepart2 D/AndroidRuntime: Shutting down VM
10-18 22:54:21.501 12068-12068/edu.ncc.hkim.tictactoepart2 E/AndroidRuntime: FATAL EXCEPTION: main
Process: edu.ncc.hkim.tictactoepart2, PID: 12068
java.lang.RuntimeException: Unable to start activity ComponentInfo{edu.ncc.hkim.tictactoepart2/edu.ncc.hkim.tictactoepart2.MainActivity}: java.lang.NullPointerException: Attempt to write to null array
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2947)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3008)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1650)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6688)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
Caused by: java.lang.NullPointerException: Attempt to write to null array
at edu.ncc.hkim.tictactoepart2.MainActivity.onCreate(MainActivity.java:84)
at android.app.Activity.performCreate(Activity.java:6912)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2900)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3008)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1650)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6688)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
答案 0 :(得分:1)
它在错误Caused by: java.lang.NullPointerException: Attempt to write to null array
中说。
在for循环中,您尝试将值分配给尚未初始化的数组。这就是你得到NullPointerException的原因。