将附加内容传递给它调用的另一个主活动

时间:2013-04-21 17:02:36

标签: android android-intent

主要活动中,我有一个按钮添加一个新的注释(这是一种粘滞便笺的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);
        }
    });

}

N.B:我的Note类实现了Parcelable Class!

这是我的问题: 当我点击主要活动中的添加按钮时,我的应用程序崩溃(“意外停止”)为什么:(???

告诉我是否还有其他遗漏信息或代码...谢谢......

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

我认为这是因为主要活动中的这些行:

String summary = newNote.getSummary();
String details = newNote.getDetails();

我尝试从newNote获取值,而它是null 但为什么它为空?

-------------------------------------------- - - - - - - - -编辑 - - - - - - - - - - - - - - - - - -------------------------

我已经尝试了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();
            }
        });

1 个答案:

答案 0 :(得分:0)

你也可以试试这个......

创建一个包含注释实例的静态类,然后在AddActivity中将值添加为

  StaticClass.noteitem.summary="abc";
  StaticClass.noteitem.details="abcdef";

然后是你的主要活动检查的OnREsume

   if (StaticClass.noteitem!=null)

并以StaticClass.noteitem.summary等方式访问您的项目。