在主要活动中,我有一个添加新注释的按钮(它是一种便签 android 应用程序):
ArrayAdapter<Note> adapter = (ArrayAdapter<Note>) getListAdapter();
Note note = null;
switch (view.getId()) {
case R.id.add:
// Call AddActivity to specify content of the note fields to be
// added to the DB
intentToAdd = new Intent(this, AddActivity.class);
startActivity(intentToAdd);
//get the extras from AddActivity
Note newNote = getIntent().getExtras().getParcelable("myNewNote");
String summary = newNote.getSummary();
String details = newNote.getDetails();
// Save the new note in the database
note = datasource.createNote(summary, details);
adapter.add(note);
break;
然后我有AddActivity(其中包含一个添加新注释的表单),它的onCreate 方法是:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
butAdd = (Button) findViewById(R.id.buttonAjout);
intentToMain = new Intent(this, MainActivity.class);
butAdd.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
EditText summary = ((EditText) findViewById(R.id.editTextSum));
EditText details = ((EditText) findViewById(R.id.editTextDet));
Note note =new Note(summary.toString(),details.toString());
intentToMain.putExtra("myNewNote", note);
startActivity(intentToMain);
}
});
}
注意:我的笔记类实现了 Parcelable 类!
这是我的问题: 当我单击主活动中的添加按钮时,我的应用程序崩溃(“意外停止”)为什么:( ???
告诉我是否还有其他缺少的信息或代码...谢谢...
logCat 说这是一个 nullPointerException:
04-21 17:00:07.832: D/AndroidRuntime(278): Shutting down VM
04-21 17:00:07.832: W/dalvikvm(278): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
04-21 17:00:07.892: E/AndroidRuntime(278): FATAL EXCEPTION: main
04-21 17:00:07.892: E/AndroidRuntime(278): java.lang.IllegalStateException: Could not execute method of the activity
04-21 17:00:07.892: E/AndroidRuntime(278): at android.view.View$1.onClick(View.java:2072)
04-21 17:00:07.892: E/AndroidRuntime(278): at android.view.View.performClick(View.java:2408)
04-21 17:00:07.892: E/AndroidRuntime(278): at android.view.View$PerformClick.run(View.java:8816)
04-21 17:00:07.892: E/AndroidRuntime(278): at android.os.Handler.handleCallback(Handler.java:587)
04-21 17:00:07.892: E/AndroidRuntime(278): at android.os.Handler.dispatchMessage(Handler.java:92)
04-21 17:00:07.892: E/AndroidRuntime(278): at android.os.Looper.loop(Looper.java:123)
04-21 17:00:07.892: E/AndroidRuntime(278): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-21 17:00:07.892: E/AndroidRuntime(278): at java.lang.reflect.Method.invokeNative(Native Method)
04-21 17:00:07.892: E/AndroidRuntime(278): at java.lang.reflect.Method.invoke(Method.java:521)
04-21 17:00:07.892: E/AndroidRuntime(278): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-21 17:00:07.892: E/AndroidRuntime(278): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-21 17:00:07.892: E/AndroidRuntime(278): at dalvik.system.NativeStart.main(Native Method)
04-21 17:00:07.892: E/AndroidRuntime(278): Caused by: java.lang.reflect.InvocationTargetException
04-21 17:00:07.892: E/AndroidRuntime(278): at com.insat.notememoiresqlite.MainActivity.onClick(MainActivity.java:69)
04-21 17:00:07.892: E/AndroidRuntime(278): at java.lang.reflect.Method.invokeNative(Native Method)
04-21 17:00:07.892: E/AndroidRuntime(278): at java.lang.reflect.Method.invoke(Method.java:521)
04-21 17:00:07.892: E/AndroidRuntime(278): at android.view.View$1.onClick(View.java:2067)
04-21 17:00:07.892: E/AndroidRuntime(278): ... 11 more
04-21 17:00:07.892: E/AndroidRuntime(278): Caused by: java.lang.NullPointerException
04-21 17:00:07.892: E/AndroidRuntime(278): ... 15 more
04-21 17:04:37.812: I/Process(278): Sending signal. PID: 278 SIG: 9
我认为这是因为 Main Activity 中的这些行:
String summary = newNote.getSummary();
String details = newNote.getDetails();
我尝试在 newNote 为空时从其中获取值 但为什么它为空?
-------------------------------------------------- - - - - -编辑 - - - - - - - - - - - - - - - - - - - - ------------------
我已经尝试过 onActivityResult 解决方案,但我仍然有此行的 NullPointerException:
Note newNote = getIntent().getExtras().getParcelable("myNewNote");
这是代码:在主要活动中:
public void onClick(View view) {
adapter = (ArrayAdapter<Note>) getListAdapter();
switch (view.getId()) {
case R.id.add:
// Call AddActivity to specify content of the note fields to be
// added to the DB
intentToAdd = new Intent(this, AddActivity.class);
startActivityForResult(intentToAdd, 0);
break;
然后:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
Log.i("aaaaa", "Dans le onActivityResult ");
if (resultCode == RESULT_OK) {
Log.i("aaaaa", "RESULT_OK ");
// get the extras from AddActivity
Note newNote = getIntent().getExtras().getParcelable("myNewNote");
if (newNote != null) {
String summary = newNote.getSummary();
String details = newNote.getDetails();
// Save the new note in the database
note = datasource.createNote(summary, details);
adapter.add(note);
} else {
Log.i("aaaaa", "newNote est null !!!!!!!! ");
}
} else {
Log.i("aaaaa", "RESULT_PAS OK ");
}
}
并在 AddActivity 中:
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
Log.i("aaaaa", "Dans le onCreate de AddActivity");
butAdd = (Button) findViewById(R.id.buttonAjout);
intentToMain = new Intent(this, MainActivity.class);
butAdd.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.i("aaaaa", "Dans le onClick du bouton ajout");
EditText summary = ((EditText) findViewById(R.id.editTextSum));
EditText details = ((EditText) findViewById(R.id.editTextDet));
Note note =new Note(summary.toString(),details.toString());
intentToMain.putExtra("myNewNote", note);
setResult(RESULT_OK);
finish();
}
});