-1

I get an error if i press the Hardware-Back-Button in one of my Activitys and I cant figure out where the error lies Here is my code:

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class NoteScreen extends Activity {

/** Called when the activity is first created. */
private EditText mTitleText;
private EditText mBodyText;
private Long mRowId;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.note_screen);
    setTitle("Edit Note");

    mTitleText = (EditText) findViewById(R.id.title);
    mBodyText = (EditText) findViewById(R.id.note);
    ImageButton acceptButton = (ImageButton) findViewById(R.id.accept);

    mRowId = null;
    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        String title = extras.getString(NotesDbAdapter.KEY_TITLE);
        String body = extras.getString(NotesDbAdapter.KEY_BODY);
        mRowId = extras.getLong(NotesDbAdapter.KEY_ROWID);

        if (title != null) {
            mTitleText.setText(title);
        }

        if (body != null) {
            mBodyText.setText(body);
        }
    }

    acceptButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            Bundle bundle = new Bundle();

            bundle.putString(NotesDbAdapter.KEY_TITLE,         mTitleText.getText().toString());
            bundle.putString(NotesDbAdapter.KEY_BODY, mBodyText.getText().toString());
            if (mRowId != null) {
                bundle.putLong(NotesDbAdapter.KEY_ROWID, mRowId);
            }

            Intent mIntent = new Intent();
            mIntent.putExtras(bundle);
            setResult(RESULT_OK, mIntent);
            finish();
        }
    });

    ImageButton cancelButton = (ImageButton) findViewById(R.id.cancel);
    cancelButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            Intent i = new Intent(NoteScreen.this, MainActivity.class);
            startActivity(i);
            finish();
        }
    });





    // TODO Auto-generated method stub
}

}

Here is my LogCat:

11-23 19:59:10.851: E/AndroidRuntime(1009): FATAL EXCEPTION: main
11-23 19:59:10.851: E/AndroidRuntime(1009): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=0, data=null} to activity {com.reekapps.simplenote/com.reekapps.simplenote.MainActivity}: java.lang.NullPointerException
11-23 19:59:10.851: E/AndroidRuntime(1009):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3319)
11-23 19:59:10.851: E/AndroidRuntime(1009):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3362)
11-23 19:59:10.851: E/AndroidRuntime(1009):     at android.app.ActivityThread.access$1100(ActivityThread.java:141)
11-23 19:59:10.851: E/AndroidRuntime(1009):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)
11-23 19:59:10.851: E/AndroidRuntime(1009):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-23 19:59:10.851: E/AndroidRuntime(1009):     at android.os.Looper.loop(Looper.java:137)
11-23 19:59:10.851: E/AndroidRuntime(1009):     at android.app.ActivityThread.main(ActivityThread.java:5041)
11-23 19:59:10.851: E/AndroidRuntime(1009):     at java.lang.reflect.Method.invokeNative(Native Method)
11-23 19:59:10.851: E/AndroidRuntime(1009):     at java.lang.reflect.Method.invoke(Method.java:511)
11-23 19:59:10.851: E/AndroidRuntime(1009):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
11-23 19:59:10.851: E/AndroidRuntime(1009):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-23 19:59:10.851: E/AndroidRuntime(1009):     at dalvik.system.NativeStart.main(Native Method)
11-23 19:59:10.851: E/AndroidRuntime(1009): Caused by: java.lang.NullPointerException
11-23 19:59:10.851: E/AndroidRuntime(1009):     at com.reekapps.simplenote.MainActivity.onActivityResult(MainActivity.java:171)
11-23 19:59:10.851: E/AndroidRuntime(1009):     at android.app.Activity.dispatchActivityResult(Activity.java:5293)
11-23 19:59:10.851: E/AndroidRuntime(1009):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3315)
11-23 19:59:10.851: E/AndroidRuntime(1009):     ... 11 more
11-23 19:59:43.861: E/Trace(1120): error opening trace file: No such file or directory (2)

Does anybody see the error? I have no Idea Thanks in advance

EDIT:

     @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        Bundle extras = intent.getExtras();
        switch(requestCode) {
            case ACTIVITY_CREATE:
                String title = extras.getString(NotesDbAdapter.KEY_TITLE);
                String body = extras.getString(NotesDbAdapter.KEY_BODY);
                mDbHelper.createNote(title, body);
                fillData();
                break;
            case ACTIVITY_EDIT:
                Long rowId = extras.getLong(NotesDbAdapter.KEY_ROWID);
                if (rowId != null) {
                    String editTitle = extras.getString(NotesDbAdapter.KEY_TITLE);
                    String editBody = extras.getString(NotesDbAdapter.KEY_BODY);
                    mDbHelper.updateNote(rowId, editTitle, editBody);
                }
                fillData();
                break;
        }
    }

I posted my onActivityResult, maybe the error is somewhere in there.

mleko
  • 11,650
  • 6
  • 50
  • 71
mind
  • 432
  • 6
  • 18
  • 3
    Yes I see it: `Caused by: java.lang.NullPointerException at com.reekapps.simplenote.MainActivity.onActivityResult(MainActivity.java:171)` – keyser Nov 23 '13 at 20:26
  • 1
    @ᴋᴇʏsᴇʀ you won me by 1 second. REEK Apps, if you need more help, you have to pase your onActivityResult method too. – gian1200 Nov 23 '13 at 20:28
  • I posted the onActivityResult now, could you please check it? – mind Nov 23 '13 at 20:47

1 Answers1

1

You start this Activity for result, so you need to deliver the result when you go away from this Activity. Try to ovverride the method onBackPressed().

For example:

@Override
public void onBackPressed() {
  setResult(RESULT_CANCELED);
  finish();
}

Also check you onActivityResult in the MainActivity, maybe you need to add some external logic to it too.

Regarding your last edit:

You need to proceed the resultCode, not only the requestCode like you do now. For example you can do smth like this:

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);
 if (resultCode == RESULT_OK) {
    Bundle extras = intent.getExtras();
    switch(requestCode) {
        case ACTIVITY_CREATE:
            String title = extras.getString(NotesDbAdapter.KEY_TITLE);
            String body = extras.getString(NotesDbAdapter.KEY_BODY);
            mDbHelper.createNote(title, body);
            fillData();
            break;
        case ACTIVITY_EDIT:
            Long rowId = extras.getLong(NotesDbAdapter.KEY_ROWID);
            if (rowId != null) {
                String editTitle = extras.getString(NotesDbAdapter.KEY_TITLE);
                String editBody = extras.getString(NotesDbAdapter.KEY_BODY);
                mDbHelper.updateNote(rowId, editTitle, editBody);
            }
            fillData();
            break;
    }
}
}

It's not the best way, but I use your code for helping :)

Good luck !

yurezcv
  • 1,033
  • 12
  • 19