2

我正在尝试研究如何为 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);
}

}

感谢帮助。

4

3 回答 3

1

您正在调用 的toString方法,Bundle而不是检索存储在其上的字符串。此外,您正在尝试Bundle使用存储在其中的密钥来获取 a 。尝试改变

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

考虑到如果收到的意图不是你的,并且它带有 noBundle或不同的意图,那么你的nomecognome字符串将是null,而不是空字符串。

于 2012-12-26T11:30:36.100 回答
1

logcat 在 com.example.startactivity/com.example.startactivity.DisplayMessageActivity 处显示问题

因此,最好的猜测是在 DisplayMessageActivity 的 texviews 中设置文本时,名称和认知值可能为空

尝试在带有文本的行添加一个调试点

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

并查看任何空值。

希望这能激起

于 2012-12-26T11:20:39.493 回答
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>
于 2012-12-26T11:15:30.060 回答