我是android新手。并编写此程序但收到错误。
public class First1Activity extends Activity {
/** Called when the activity is first created. */
public final static String x= "com.example.my.p";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void mess(View view) {
Intent intent=new Intent(First1Activity.this,DisMess.class);
EditText edittext = (EditText) findViewById(R.id.editText1);
String m= edittext.getText().toString();
intent.putExtra(x, m);
TextView t=(TextView)findViewById(R.id.textView1);
t.setText(m);
First1Activity.this.startActivity(intent);
}
public class DisMess extends Activity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String message = intent.getStringExtra(First1Activity.x);
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
setContentView(textView);
}
}
现在我得到像::::::
的错误应用程序已停止工作......
我遇到了错误: First1Activity.this.startActivity(intent);
请帮助
答案 0 :(得分:0)
-
Intent i = new Intent(this, ActivityTwo.class);
i.putExtra("Value1", "This value one for ActivityTwo ");
i.putExtra("Value2", "This value two ActivityTwo");
startActivity(i);
在活动2中 - 在onCreate函数
Bundle extras = getIntent().getExtras();
if (extras == null) {
return;
}
// Get data via the key
String value1 = extras.getString(Intent.EXTRA_TEXT);
if (value1 != null) {
// Do something with the data
}
=====
代码
答案 1 :(得分:0)
Intent intent=new Intent(First1Activity.this,DisMess.class);
EditText edittext = (EditText) findViewById(R.id.editText1);
String m= edittext.getText().toString();
intent.putExtra("x", m);
AND
Intent intent = getIntent();
String message = intent.getStringExtra("x");
答案 2 :(得分:0)
确保您已在清单中声明了DisMess活动。
我稍微修改了你的代码。
public class First1Activity extends Activity {
public final static String key= "com.example.my.p";
private Context context;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
context=First1Activity.this;
}
public void mess(View view) {
EditText edittext = (EditText) findViewById(R.id.editText1);
TextView t=(TextView)findViewById(R.id.textView1);
String m= edittext.getText().toString();
t.setText(m);
Intent intent=new Intent(context,DisMess.class);
intent.putExtra(x, m);
startActivity(intent);
}
public class DisMess extends Activity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String message = intent.getStringExtra(First1Activity.x);
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
setContentView(textView);
}
}