我正在尝试在TextView上使用Explicit intent创建的基本表单上显示用户输入的名称,但是当我单击该按钮时,应用程序崩溃时出现以下“不幸的是,formIntent已经停止”但我的代码没有错误。我在新活动中使用Toast消息,但使用TextView。任何帮助将不胜感激。
这是我的第一个活动`公共类MainActivity扩展了AppCompatActivity {
Button submit;
TextView name;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
submit = (Button)findViewById(R.id.btnSubmit);
name = (TextView)findViewById(R.id.editTextname);
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//from sending Activity
String userName = name.getText().toString();
String value = "Thank you " + userName + "your request is being processed" ;
Intent sendIntent = new Intent(v.getContext(), Main2Activity.class);
sendIntent.putExtra("userName", value);
//Verify that the intent will resolve to an activity
if (sendIntent.resolveActivity(getPackageManager()) != null)
{
startActivity(sendIntent);
}
}
});
}
} ` 这是我的第二个活动
public class Main2Activity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Bundle data = getIntent().getExtras();
if(data == null){
return;
}
String getName = getIntent().getExtras().getString("userName");
final TextView inputMessage = (TextView)findViewById(R.id.display);
inputMessage.setText(getName);
}
}
答案 0 :(得分:2)
请将此行更改为您的活动名称
Intent sendIntent = new Intent(MainActivity.this, Main2Activity.class);