【问题标题】:Why I don't get the String from the other Activity?为什么我没有从其他活动中获取字符串?
【发布时间】:2014-09-23 09:27:25
【问题描述】:

嗨,我想构建一个 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);
    }
}

【问题讨论】:

  • 我在您的代码中没有看到 2 个活动。
  • 这是第二个......我也发布了第一个......时刻
  • 发布您的 MainActivity 代码。因为当您正确传入 MainActivity 并启动当前活动(DisplayMessageActivity)时,它就来了

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


【解决方案1】:

尝试在 DisplayMessageActivity 中获取您的 Bundle

            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 是您的关键通行证

【讨论】:

  • 为什么不呢?你能在 DisplayMessageActivity 中获取你的 Bundle 吗?
【解决方案2】:

使用键从第一个活动传递消息并在第二个活动中像这样获取这些值

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

在第一个活动中传递这样的消息

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

【讨论】:

    【解决方案3】:

    试试

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

    在第一个活动中

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

    【讨论】:

      猜你喜欢
      • 2019-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-30
      相关资源
      最近更新 更多