2013-08-28 39 views
1

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

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

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

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

你能碰撞后logcat的细节? – gunar

回答

5

变化:

Intent intent = new Intent(); 

到:

Intent intent = new Intent(MyCurrentActivityClass.this, NextActivity.class); 
从第二个活动

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); 
    } 
} 

代码

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

+0

你的救命恩人非常感谢你,不敢相信我犯了这样一个愚蠢的错误 –

+1

正确的解决办法。 – ridoy

4

试试这个:

从第一次活动发送这样的:

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); 
0

你声明一个变量textview1?更改

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

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

2

你应该得到的第二个活动的信息是这样的:

Bundle extras = getIntent().getExtras(); 
String myLocation= extras.getString("location"); 
相关问题