java.lang.RuntimeException:无法启动活动

时间:2012-12-26 11:03:12

标签: java android android-activity

我正在尝试研究如何为Android制作应用。 按照我的修改by Google教程后我得到了这个错误

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

}

感谢您的帮助。

3 个答案:

答案 0 :(得分:1)

logcat显示了以下问题: com.example.startactivity / com.example.startactivity.DisplayMessageActivity

因此,最好的猜测是,在您在DisplayMessageActivity中的texviews中设置文本时,nome和cognome值可能为null

尝试在带有文字的行中添加调试点

String nome = intent.getBundleExtra("nome").toString();

并查看任何空值。

希望这个jelps

答案 1 :(得分:1)

您正在调用toString的{​​{1}}方法,而不是检索存储在其上的字符串。此外,您正在尝试使用存储在其中的密钥获取Bundle。尝试chaning

Bundle

String nome = intent.getBundleExtra("nome").toString();
String cognome = intent.getBundleExtra("cognome").toString();

请注意,如果收到的意图不是您的意图,并且没有Bundle bundle = intent.getBundleExtra(MainActivity.EXTRA_MESSAGE); if (bundle != null) { String nome = bundle.getString("nome"); String cognome = bundle.getString("cognome"); } 或其他意图,则Bundlenome字符串将为{{1} },而不是空字符串。

答案 2 :(得分:0)

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

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


}

@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;
}


}

确保清单文件中的条目:

<activity android:name=".DisplayMessageActivity"></activity>