Passing extras to the main activity from another o

2019-06-11 01:30发布

In the Main activity I have a button to add a new Note (it's a sort of sticky notes android application):

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;

Then I have the AddActivity (which contains a form to add a new Note), its onCreate method is:

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 : My Note Class implements Parcelable Class !

Here's my problem: when I click on the add button in the main activity my application crashes ("stops unexpectedly") WHY :( ???

Tell me if there is some other missing information or code... Thank you ...

The logCat says it's a 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

I think it's because of these lines in the Main Activity:

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

where I try to get values from newNote while it's null But why is it null?

-----------------------------------------------------------Edit-----------------------------------------------------------

I've tried the onActivityResult solution, but i still have a NullPointerException for this line:

Note newNote = getIntent().getExtras().getParcelable("myNewNote");

Here is the code: In Main Activity:

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;

then:

@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 ");
        }
    }

and in the 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条回答
该账号已被封号
2楼-- · 2019-06-11 01:37

You can also try this...

Make a static class which holds an instance of note,then in AddActivity add the values to it as

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

then on the OnREsume of your mainactivity check

   if (StaticClass.noteitem!=null)

and access your item as StaticClass.noteitem.summary etc.

查看更多
登录 后发表回答