我正在尝试研究如何为 Android 制作应用程序。按照谷歌制作的教程和我的修改我得到这个错误
12-26 10:42:23.848: D/dalvikvm(1302): GC_CONCURRENT freed 362K, 15% free 2737K/3216K, paused 70ms+89ms, total 204ms
12-26 10:42:23.898: D/AndroidRuntime(1302): Shutting down VM
12-26 10:42:23.898: W/dalvikvm(1302): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
12-26 10:42:23.928: E/AndroidRuntime(1302): FATAL EXCEPTION: main
12-26 10:42:23.928: E/AndroidRuntime(1302): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.startactivity/com.example.startactivity.DisplayMessageActivity}: java.lang.NullPointerException
12-26 10:42:23.928: E/AndroidRuntime(1302): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
12-26 10:42:23.928: E/AndroidRuntime(1302): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-26 10:42:23.928: E/AndroidRuntime(1302): at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-26 10:42:23.928: E/AndroidRuntime(1302): at android.app.ActivityThread$H.handleMessage(ActivityThread.java: 1234)
12-26 10:42:23.928: E/AndroidRuntime(1302): at android.os.Handler.dispatchMessage(Handler.java:99)
12-26 10:42:23.928: E/AndroidRuntime(1302): at android.os.Looper.loop(Looper.java:137)
12-26 10:42:23.928: E/AndroidRuntime(1302): at android.app.ActivityThread.main(ActivityThread.java:5039)
12-26 10:42:23.928: E/AndroidRuntime(1302): at java.lang.reflect.Method.invokeNative(Native Method)
12-26 10:42:23.928: E/AndroidRuntime(1302): at java.lang.reflect.Method.invoke(Method.java:511)
12-26 10:42:23.928: E/AndroidRuntime(1302): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-26 10:42:23.928: E/AndroidRuntime(1302): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-26 10:42:23.928: E/AndroidRuntime(1302): at dalvik.system.NativeStart.main(Native Method)
12-26 10:42:23.928: E/AndroidRuntime(1302): Caused by: java.lang.NullPointerException
12-26 10:42:23.928: E/AndroidRuntime(1302): at com.example.startactivity.DisplayMessageActivity.onCreate(DisplayMessageActivity.java:20)
12-26 10:42:23.928: E/AndroidRuntime(1302): at android.app.Activity.performCreate(Activity.java:5104)
12-26 10:42:23.928: E/AndroidRuntime(1302): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
12-26 10:42:23.928: E/AndroidRuntime(1302): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
12-26 10:42:23.928: E/AndroidRuntime(1302): ... 11 more
12-26 10:43:11.418: I/Process(1302): Sending signal. PID: 1302 SIG: 9
这是 MainActivity 的代码
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.startactivity.MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void sendMessage(View view){
Intent intent = new Intent(this,DisplayMessageActivity.class);
EditText edit_nome = (EditText) findViewById(R.id.edit_nome);
EditText edit_cognome = (EditText) findViewById(R.id.edit_cognome);
Bundle bundle = new Bundle();
bundle.putString("nome", edit_nome.getText().toString());
bundle.putString("cognome", edit_cognome.getText().toString());
intent.putExtra(EXTRA_MESSAGE, bundle);
startActivity(intent);
}
}
这是 DisplayMessageActivity 的代码
public class DisplayMessageActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
// Show the Up button in the action bar.
getActionBar().setDisplayHomeAsUpEnabled(true);
Intent intent = getIntent();
String nome = intent.getBundleExtra("nome").toString();
String cognome = intent.getBundleExtra("cognome").toString();
TextView textNome = new TextView(this);
TextView textCognome = new TextView(this);
textNome.setTextSize(40);
textCognome.setTextSize(40);
textNome.setText(nome);
textCognome.setText(cognome);
setContentView(textNome);
setContentView(textCognome);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
感谢帮助。