【问题标题】:Passing string from edit text to another activity将字符串从编辑文本传递到另一个活动
【发布时间】:2026-01-13 16:10:01
【问题描述】:

我已经查看了此处有关堆栈溢出的示例。但是,我无法获得正常工作的解决方案。我的应用程序仍然崩溃。如何将字符串从一个活动中的编辑文本传递到另一个活动?

这是我第一次活动的代码:

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

btnGo.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        EditText etLocation = (EditText) findViewById(R.id.et_location);
        Intent intent = new Intent();
        intent.putExtra("location", etLocation.getText().toString());
        startActivity(intent);
    }
}

第二个活动的代码:

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

Intent intent = getIntent();
String str = intent.getStringExtra("location");
textView1.setText(str);

【问题讨论】:

  • 你能发布崩溃 Logcat 的详细信息吗?

标签: android android-activity android-edittext


【解决方案1】:

变化:

Intent intent = new Intent();

到:

Intent intent = new Intent(MyCurrentActivityClass.this, NextActivity.class);

确保 NextActivity 在 Manifest 中。在第一种情况下,您没有提供足够的信息来启动活动。

【讨论】:

  • 你的救命稻草非常感谢你,不敢相信我犯了这么愚蠢的错误
【解决方案2】:

试试这个:

从第一个活动发送如下:

btnGo.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        EditText etLocation = (EditText) findViewById(R.id.et_location);
       Intent i = new Intent(this, ActivityTwo.class);
        i.putExtra("location", etLocation.getText().toString());      
        startActivity(i);
}
});

在第二个活动中这样做:

Intent in = getIntent();
String tv1= in.getExtras().getString("location");
textView1.setText(tv1);

【讨论】:

    【解决方案3】:

    你声明了一个变量 textview1 吗?改变

    textView1 = (TextView) findViewById(R.id.textView1);到

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

    【讨论】:

      【解决方案4】:

      您应该通过这种方式从第二个活动中获取信息:

      Bundle extras = getIntent().getExtras();
      String myLocation= extras.getString("location");
      

      【讨论】:

        最近更新 更多