为什么我没有从其他活动中获取字符串?

时间:2014-09-23 09:27:26

标签: java android android-intent android-activity fragment

您好我想构建一个Android应用程序,将文本发送到其他活动,但我没有看到第二个活动的输入文本:(我尝试这个教程:

http://developer.android.com/training/basics/firstapp/index.html

我在本教程中使用了相同的内容:

如果我点击发送,我会使用Intent类,然后输出我的字符串......

这是我的DisplayMessageActivity.java代码

public class DisplayMessageActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //setContentView(R.layout.activity_display_message);
                if (savedInstanceState == null) {
                    getSupportFragmentManager().beginTransaction()
                            .add(R.id.container, new PlaceholderFragment()).commit();
                }

                // Get the message from the intent
                Intent intent = getIntent();
                String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

                // Create the text view
                TextView textView = new TextView(this);
                textView.setTextSize(40);
                textView.setText(message);

                // Set the text view as the activity layout
                setContentView(textView);


    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container,
                    false);

            return rootView;
        }
    }
}

更新:

我的第一项活动:

public class MainActivity extends ActionBarActivity {

    public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";

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

    // Button Events

    public void sendMessage(View view) {
        Intent intent = new Intent(this, DisplayMessageActivity.class);
        EditText editText = (EditText) findViewById(R.id.edit_message);
        String message = editText.getText().toString();
        intent.putExtra(EXTRA_MESSAGE, message);
        startActivity(intent);
    }
}

3 个答案:

答案 0 :(得分:3)

尝试将您的Bundle放入DisplayMessageActivity

            Bundle bundle = getIntent().getExtras();
            if(bundle != null)
            {
                String message = bundle.getString(MainActivity.EXTRA_MESSAGE);
            }

在发件人行为中。 :

                    Intent intent = new Intent(Activity_A.this, DisplayMessageActivity.class);
                    intent.putExtra(MainActivity.EXTRA_MESSAGE, "Your title");

                    Activity_A.this.startActivity(intent);

MainActivity.EXTRA_MESSAGE是您的关键传递

答案 1 :(得分:2)

使用密钥从第一个活动传递消息,并在第二个活动中获取这些值,如此

    bundle = getIntent().getExtras();
    if (bundle != null) {
       value1 = bundle.getString("key1");
        value2 = bundle.getString("key2");
    }

在First Activity中传递消息,如

 intent.putExtra("key1", "message1");
 intent.putExtra("key2", "message2");

答案 2 :(得分:2)

String message = intent.getStringExtra("EXTRA_MESSAGE");

并在第一个活动中

intent.putExtra("EXTRA_MESSAGE", "Your title");