0

我是安卓新手。并编写此程序但出现错误。

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

请帮忙

4

3 回答 3

0

在活动 1-

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
}

=================

在你的代码中

于 2012-05-31T06:43:08.527 回答
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); 

Intent intent = getIntent();
String message = intent.getStringExtra("x");
于 2012-05-31T06:46:34.410 回答
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);

        }

    }
于 2012-05-31T06:51:51.987 回答