我想在android studio中製作一個To Do應用程序。但只要我點擊添加按鈕(+在右上角)應用程序崩潰。我很新的Java和Android的工作室,但我認爲這個問題可以躺在「saveListInfo()」,但我無法弄清楚如何解決它......要做應用程序崩潰
代碼從EditedActivity.java:
public class EditedActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edited);
String name;
name = getIntent().getStringExtra("theName");
EditText editList = (EditText) findViewById(R.id.editText);
editList.setText(name);
}
private void saveListInfo() {
EditText editList = (EditText) findViewById(R.id.editText);
String name = editList.getText().toString();
Bundle listBundle = new Bundle();
listBundle.putString("name", name);
Intent finalIntent = new Intent(this, MainActivity.class);
finalIntent.putExtras(listBundle);
setResult(RESULT_OK, finalIntent);
}
Button addBtn = (Button) findViewById(R.id.add_btn);
{
addBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
saveListInfo();
}
});
}
}
從MainActivity.java
代碼:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_add_task:
saveTodoInfo();
}
return true;
}
private void saveTodoInfo(){
TextView nameView = (TextView) findViewById(R.id.action_add_task);
String name = nameView.getText().toString();
Intent myIntent = new Intent(this, EditedActivity.class);
myIntent.putExtra("theName", name);
startActivityForResult(myIntent, 0);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 0){
if(resultCode == RESULT_OK){
Bundle ListBundle = data.getExtras();
String name = ListBundle.getString("theName");
updateToDo(name);
}
}
}
private void updateToDo(String name){
TextView nameView = (TextView) findViewById(R.id.action_add_task);
nameView.setText(name);
}
}
這是點擊添加按鈕時,我得到的錯誤:
02-12 16:07:40.553 1410- 1410/com.carpe_diem.anitas_todolist E/AndroidRuntime:致命異常:主 工藝:com.carpe_diem.anitas_todolist,PID:1410 了java.lang.RuntimeException:無法實例活動 ComponentInfo {com.carpe_diem.anitas_todolist/COM。 carpe_diem.anitas_todolist.EditedActivity}: 顯示java.lang.NullPointerException:嘗試在 android.app上一個空 對象引用 調用虛擬方法 'android.view.View android.view.Window.findViewById(INT)'。 ActivityThread.performLaunchActivity(ActivityThread.java:2327) at android.app.ActivityThread.handleLaunchActivity(Activity Thread.java:2476) at android.app.ActivityThread.-wrap11(ActivityThread.java) at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1344) at android.os.Handler.dispatchMessage( Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(本地方法) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 由於:java.lang.NullPointerException:嘗試調用虛擬 方法'android.view.View android.view.Window.findViewById(int)'在 空對象引用 at android.app.Activity.findViewById(Activity的.java:2090) 在 com.carpe_diem.anitas_todolist.EditedActivity(EditedActivity.java:40) 在java.lang.Class.newInstance(本機方法) 在android.app.Instrumentation.newActivity(Instrumentation.java: 1067) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 在android.app.ActivityThread.-wrap11(ActivityThread.java) 在 android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1344) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java .lang.reflect.Method。調用(本地方法) 在 com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
我想知道爲什麼這個問題被關閉爲重複只是因爲指示「NullPointerException」 – Jorgesys