onActivityResult没有显示它应该是什么

时间:2014-02-15 23:43:45

标签: android

我正在尝试将具有一个Activity的EditText返回到第一个Activity但是当我按下按钮以完成de second Activity而onActivityResult调用TextView时应该显示EditText保存中的文本,没有任何shos。

这是意图创建和第二个活动的开始:

Intent myInt = new Intent(ActivityLoaderActivity.this, ExplicitlyLoadedActivity.class);
startActivityForResult(myInt, GET_TEXT_REQUEST_CODE); 

以下是我在ExplicitlyLoadedActivity.java中保存EditText的方法:

private void enterClicked() {

    Log.i(TAG,"Entered enterClicked()");

    // TODO - Save user provided input from the EditText field
    Editable res = mEditText.getText();
    // TODO - Create a new intent and save the input from the EditText field as an extra
    Intent returnIntent = new Intent();
    returnIntent.putExtra("result", res);
    // TODO - Set Activity's result with result code RESULT_OK
    setResult(RESULT_OK,returnIntent);
    // TODO - Finish the Activity
    finish();
}

这是onActivityResult的代码:

if (requestCode == 1) {

             if(resultCode == RESULT_OK){      
                 String res = data.getStringExtra("result");
                 mUserTextView.setText(res);
             }
        }

在调试模式下,我可以看到res有我放在EditText中的文本,但它在aplication中没有任何内容。

这是mUserTextView的代码:

private TextView mUserTextView;
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_loader_activity);

        // Get reference to the textView
        mUserTextView = (TextView) findViewById(R.id.textView1);

这是onActivityResult()的完整代码:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        Log.i(TAG, "Entered onActivityResult()");

        // TODO - Process the result only if this method received both a
        // RESULT_OK result code and a recognized request code
        // If so, update the Textview showing the user-entered text.
        if (requestCode == 1) {

             if(resultCode == RESULT_OK){
                 String res = data.getStringExtra("result");
                 mUserTextView.setText(res);
             }
        }

    }

2 个答案:

答案 0 :(得分:0)

if (requestCode == 1) {

         if(resultCode == RESULT_OK){      
             String res = getIntent.getStringExtra("result");
             mUserTextView.setText(res);
         }
    }

尝试这段代码 并确保GET_TEXT_REQUEST_CODE == 1

答案 1 :(得分:0)

我尝试过与您上面描述的相同的代码,但效果很好。由于在onActivityResult中确实收到了res,因此您应该检查mUserTextView相关代码。