如何使用intent和startActivityforresult在android中获取消息?

时间:2013-12-07 09:25:56

标签: android android-intent onactivityresult

我正在创建Android应用程序,允许用户使用intent进入第二个活动,并在编辑文本中键入一个字符串,系统会将字符串作为Toa​​st返回,但问题是系统强制关闭。 / p>

任何人都可以帮我解决这个问题吗?

MainActivity.java

package com.devleb.intentmenudemoapp;

import android.app.Activity;
import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

    private static final int INTENT_GET_MSG = 1;

    TextView txt1, txt2;
    EditText edittxt1, edittxt2;
    Button btn1, btn2, btn3, btn4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        txt1 = (TextView) findViewById(R.id.txt1);

        txt2 = (TextView) findViewById(R.id.txt2);

        edittxt1 = (EditText) findViewById(R.id.edit1);

        edittxt2 = (EditText) findViewById(R.id.edit2);

        btn1 = (Button) findViewById(R.id.btn);

        btn1.setOnClickListener(this);

        btn2 = (Button) findViewById(R.id.btn2);

        btn2.setOnClickListener(this);

        btn3 = (Button) findViewById(R.id.btn3);

        btn3.setOnClickListener(this);

        btn4 = (Button) findViewById(R.id.btn4);

        btn4.setOnClickListener(this);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        String value;

        switch (v.getId()) {
        case R.id.btn:

            value = edittxt1.getText().toString();
            txt2.setText(value);

            break;

        case R.id.btn2:
            value = edittxt1.getText().toString();
            Intent i = new Intent(this, SecondActivity.class);
            i.putExtra("msgFromFirstActivity", value);
            startActivity(i);
            break;
        case R.id.btn3:
            Intent ii = new Intent();
            ii.setType("image/pictures/*");
            ii.setAction(Intent.ACTION_GET_CONTENT);
            startActivity(ii);
            break;
        case R.id.btn4:
            Intent iii = new Intent(this, SecondActivity.class);
            startActivityForResult(iii, INTENT_GET_MSG);

        default:
            break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub

        switch (requestCode) {
        case INTENT_GET_MSG:

            if (resultCode == Activity.RESULT_OK) {
                String returnMSG = data.getExtras().getString(
                        SecondActivity.RETURN_MSG);
                Toast.makeText(this, returnMSG, Toast.LENGTH_SHORT).show();

            }
            Toast.makeText(this, "Error!!!", Toast.LENGTH_SHORT).show();

            break;

        default:
            break;
        }

        super.onActivityResult(requestCode, resultCode, data);
    }

}

SecondActivity.java

package com.devleb.intentmenudemoapp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class SecondActivity extends Activity {


    public static   String RETURN_MSG = "return_msg";
    TextView txt;
    EditText edittxt;
    String result, editResult;
    Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        edittxt = (EditText)findViewById(R.id.edit_txt);
        btn = (Button)findViewById(R.id.btn);
        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
            editResult = edittxt.getText().toString();
            Intent i = new Intent();
            i.putExtra(RETURN_MSG, editResult);

            setResult(Activity.RESULT_OK, i);
            }
        });

        txt = (TextView) findViewById(R.id.txt);

        Bundle extras = getIntent().getExtras();
        result = extras.getString("msgFromFirstActivity");
        txt.setText(result);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.second, menu);
        return true;
    }

}

的logcat

12-07 08:59:45.811: E/AndroidRuntime(1504): Caused by: java.lang.NullPointerException
12-07 08:59:45.811: E/AndroidRuntime(1504):     at com.devleb.intentmenudemoapp.SecondActivity.onCreate(SecondActivity.java:45)

这是第45行 result = extras.getString(“msgFromFirstActivity”);

3 个答案:

答案 0 :(得分:0)

当您使用 MainActivity 中的startActivityForResult时,您没有通过Intent传递任何数据,那么显然是 SecondActivity 中的Bundle,将为null ..

首先验证并重新审核 SecondActivity

中的附加内容(数据)
Bundle extras = getIntent().getExtras();
if(extras != null){     
    result = extras.getString("msgFromFirstActivity");
    txt.setText(result);
}

答案 1 :(得分:0)

像这样使用。

Intent i = getIntent();
result = i.getStringExtra("msgFromFirstActivity");
tv.setText(result);

答案 2 :(得分:0)

试试这个

String result=getIntent().getStringExtra("msgFromFirstActivity");
txt.setText(result);

而不是

 Bundle extras = getIntent().getExtras();
 result = extras.getString("msgFromFirstActivity");
 txt.setText(result);