按下后退按钮时强制关闭

时间:2013-11-23 20:24:29

标签: java android back

如果我在其中一个活动中按下硬件后退按钮,则会出现错误 我无法弄清楚错误在哪里 这是我的代码:

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
}

}

这是我的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)

有人看到错误吗?我不知道 提前致谢

编辑:

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

我发布了我的onActivityResult,也许错误就在那里。

1 个答案:

答案 0 :(得分:1)

您为结果启动此活动,因此您需要在离开此活动时提供结果。尝试挖掘方法onBackPressed()

例如:

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

同时在MainActivity中检查onActivityResult,也许你需要为它添加一些外部逻辑。

关于您的上一次修改:

您需要像现在一样继续resultCode,而不仅仅是requestCode。 例如,你可以这样做:

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

这不是最好的方法,但我使用你的代码来帮助:)

祝你好运!