Intent返回null值onActivityResult

时间:2011-05-16 11:42:01

标签: android exception android-activity android-intent

HI

我有一个扩展DashboardActivity类的MainActivity类。在主要活动类中有顶部操作栏,单击时将转到AddReminderActivity类。顶部操作栏的操作在仪表板活动类中定义为

private void createReminder() {
    // TODO: fill in implementation
    Intent intent = new Intent(DashboardActivity.this,AddReminderActivity.class);
    Log.i("in OnActivityResult", "create reminder called");       
    startActivityForResult(intent, ACTIVITY_CREATE);
    Log.i("in OnActivityResult", "Start Activity called Result");       
}

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        Log.i("in OnActivityResult", "Activity Result" + resultCode);       
        super.onActivityResult(requestCode, resultCode, intent);
        Log.i("in OnActivityResult", "Activity Result" + resultCode);    
        Log.i("in OnActivityResult", "intent Result" + intent.getExtras());    
        Bundle extras = intent.getExtras();
        Toast.makeText(getApplicationContext(),resultCode, Toast.LENGTH_SHORT).show();
        switch(requestCode){
        case ACTIVITY_CREATE:
              if (resultCode == Activity.RESULT_OK) {
                String title = extras.getString(ReminderDBAdapter.KEY_REMINDER_TITLE);
                String content = extras.getString(ReminderDBAdapter.KEY_REMINDER_CONTENT);
                mDbHelper.createReminder(title, content);
                mDbHelper.close();
              }
        startActivity(new Intent(this,ListReminder.class));
            break;

        }
        // TODO: fill in rest of method

    }

在我调用的AddReminderActivityClass上

btnSubmitReminder.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Bundle bundle = new Bundle();

            bundle.putString(ReminderDBAdapter.KEY_REMINDER_TITLE, mReminderTitle.getText().toString());
            bundle.putString(ReminderDBAdapter.KEY_REMINDER_CONTENT, mReminderContent.getText().toString());
            if(mRowId != null){
                bundle.putLong(ReminderDBAdapter.KEY_ROWID, mRowId);
            }
            System.out.println(mReminderTitle.getText().toString());
            Toast.makeText(getApplicationContext(), mReminderTitle.getText().toString(), Toast.LENGTH_SHORT).show();
            Intent mIntent = new Intent();
            mIntent.putExtras(bundle);
            //setResult(RESULT_OK, mIntent);
            if (getParent() == null) {
                  setResult(DashboardActivity.RESULT_OK, mIntent);
            } else {
                getParent().setResult(DashboardActivity.RESULT_OK, mIntent);
            }

            Log.i("in OnActivityResult", "set result" +RESULT_OK);       
            finish();

        }
    });

但是当返回OnActivityResult时,intent返回null并发生以下异常。我无法识别问题是什么

05-16 17:25:09.686: ERROR/AndroidRuntime(441): FATAL
     

例外:主要05-16   17:25:09.686:   ERROR / AndroidRuntime(441):   java.lang.RuntimeException:失败   提供结果ResultInfo {who = null,   request = 0,result = -1,data = Intent {   (有额外的)}}到活动   {com.babz.android.businessReminder / com.babz.android.businessReminder.MainActivity}:   android.content.res.Resources $ NotFoundException:   字符串资源ID#0xffffffff 05-16   17:25:09.686:   错误/ AndroidRuntime(441):at   android.app.ActivityThread.deliverResults(ActivityThread.java:3515)   05-16 17:25:09.686:   错误/ AndroidRuntime(441):at   android.app.ActivityThread.handleSendResult(ActivityThread.java:3557)   05-16 17:25:09.686:   错误/ AndroidRuntime(441):at   android.app.ActivityThread.access $ 2800(ActivityThread.java:125)   05-16 17:25:09.686:   错误/ AndroidRuntime(441):at   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2063)   05-16 17:25:09.686:   错误/ AndroidRuntime(441):at   android.os.Handler.dispatchMessage(Handler.java:99)   05-16 17:25:09.686:   错误/ AndroidRuntime(441):at   android.os.Looper.loop(Looper.java:123)   05-16 17:25:09.686:   错误/ AndroidRuntime(441):at   android.app.ActivityThread.main(ActivityThread.java:4627)   05-16 17:25:09.686:   错误/ AndroidRuntime(441):at   java.lang.reflect.Method.invokeNative(母语   方法)05-16 17:25:09.686:   错误/ AndroidRuntime(441):at   java.lang.reflect.Method.invoke(Method.java:521)   05-16 17:25:09.686:   错误/ AndroidRuntime(441):at   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868)   05-16 17:25:09.686:   错误/ AndroidRuntime(441):at   com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)   05-16 17:25:09.686:   错误/ AndroidRuntime(441):at   dalvik.system.NativeStart.main(母语   方法)05-16 17:25:09.686:   错误/ AndroidRuntime(441):引起:   android.content.res.Resources $ NotFoundException:   字符串资源ID#0xffffffff 05-16   17:25:09.686:   错误/ AndroidRuntime(441):at   android.content.res.Resources.getText(Resources.java:201)   05-16 17:25:09.686:   错误/ AndroidRuntime(441):at   android.widget.Toast.makeText(Toast.java:258)   05-16 17:25:09.686:   错误/ AndroidRuntime(441):at   com.babz.android.businessReminder.DashboardActivity.onActivityResult(DashboardActivity.java:68)   05-16 17:25:09.686:   错误/ AndroidRuntime(441):at   android.app.Activity.dispatchActivityResult(Activity.java:3890)   05-16 17:25:09.686:   错误/ AndroidRuntime(441):at   android.app.ActivityThread.deliverResults(ActivityThread.java:3511)

4 个答案:

答案 0 :(得分:1)

取消注释setResult方法调用。

答案 1 :(得分:1)

据我所知,当您尝试使用捆绑中不存在的密钥获取值时会出现异常,因此在您的情况下可能是其中之一:

String title = extras.getString(ReminderDBAdapter.KEY_REMINDER_TITLE);
String content = extras.getString(ReminderDBAdapter.KEY_REMINDER_CONTENT);

您可以通过调用捆绑包上的containsKey(String key)来询问捆绑包是否存在该密钥,因此我建议您使用该捆绑包来确定其是否属实。

答案 2 :(得分:1)

正如Guru所说,你没有调用setResult()方法。因此,意图永远不会被设置。

我正在单独回答,因为有一个需要解决的常见问题:使用错误 setResult()。

确保使用setResult(resultCode,intentData),而不是setResult(resultCode)。我不得不强调,因为很多书中的许多例子使用后者,这意味着通过startActivityForResult()发送到Activity的相同意图将返回相同的Intent。所有意图必须直接添加,否则每次都会在onActivityResult()中为Intent获取NULL。

答案 3 :(得分:1)

实际上,问题出在这一行:

Toast.makeText(getApplicationContext(),resultCode, Toast.LENGTH_SHORT).show();

这会导致异常,因为参数resultCodeint,在这种情况下,makeText()方法假定该值是资源ID(即:R.string.something) 。但是,在这种情况下,值为-1并生成:

android.content.res.Resources$NotFoundException: String resource ID #0xffffffff

您实际上希望调用makeText()方法来代替String。试试这个:

Toast.makeText(getApplicationContext(),"ResultCode: " + resultCode,
                  Toast.LENGTH_SHORT).show();