我正在学习如何使用意图打开一个新活动并传递一条消息,但是当新活动开始时,它需要关闭!我做错了什么?帮助是preciated!谢谢!
package test.intent;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
Button btn1;
String message;
public final static String EXTRA_MESSAGE = "test.intent.MESSAGE";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button) findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
message = "Message via an intent";
Intent intent = new Intent(MainActivity.this, Activity1.class);
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
});
}
}
接收消息的活动:
package test.intent;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class Activity1 extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
TextView display = (TextView) findViewById(R.id.txtv1);
setContentView(R.layout.activity1);
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
display.setText(message);
}
}