我已经尝试了两天了,但我第二次活动只是在单击按钮时才开始,没有错误消息,没有空白页,好像按钮不起作用,我想念了吗?明显的东西?
public class createMessage extends AppCompatActivity {
public static final String EXTRA_MESSAGE = "message";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_message);
}
Button b ;
public void clickSend(){
b = (Button) findViewById(R.id.send);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(createMessage.this,receiveMessage.class);
startActivity(intent);
EditText editText = (EditText) findViewById(R.id.typeMessage);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
});
}
}
第二项活动:
public class receiveMessage extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_receive_message);
// Get the Intent that started this activity and extract the string
Intent intent = getIntent();
String message = intent.getStringExtra(createMessage.EXTRA_MESSAGE);
// Capture the layout's TextView and set the string as its text
TextView textView = (TextView) findViewById(R.id.showMessage);
textView.setText(message);
}
}
最佳答案
您必须在clickSend()
方法中调用onCreate()
方法。
然后,只有onClickListener
将起作用。
并删除两次调用startActivity(intent)
的操作。
如下更改createMessage
类的代码
public class createMessage extends AppCompatActivity {
public static final String EXTRA_MESSAGE = "message";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_message);
clickSend();
}
Button b ;
public void clickSend(){
b = (Button) findViewById(R.id.send);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(createMessage.this,receiveMessage.class);
EditText editText = (EditText) findViewById(R.id.typeMessage);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
});
}
}
关于java - 新 Activity 未响应,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45295357/