1

如果已经查看过,我无法显示一个 toast 而不是另一个 toast。我有两个活动,在第一个活动 toast 显示,我有这个代码:

@Override
 protected void onCreate(Bundle savedInstanceState) {
 // TODO Auto-generated method stub
 super.onCreate(savedInstanceState);
 setContentView(R.layout.q_050);
 String str = getIntent().getStringExtra("sent");
// at this line--> if(str.equals("activity_two")){
     Toast.makeText(getApplicationContext(), getApplicationContext().getString(R.string.app_name), Toast.LENGTH_SHORT).show();
 }
 else{
     Toast.makeText(getApplicationContext(), getApplicationContext().getString(R.string.checkpoint), Toast.LENGTH_SHORT).show();
 }

在后退按钮的第二个活动中,我包括了这个:

  Button b3 = (Button)findViewById(R.id.b3);
                b3.setOnClickListener(new Button.OnClickListener() {  
                       public void onClick(View v){

                           Intent myIntentActivity1 = new Intent(Q_051.this,Q_050.class);
                           myIntentActivity1.putExtra("sent", "activity_two");
                           startActivity(myIntentActivity1);

logcat 中的问题是这样的:

 11-09 16:29:51.920: E/AndroidRuntime(1510): FATAL EXCEPTION: main
11-09 16:29:51.920: E/AndroidRuntime(1510): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.quizcelebrita/org.quizcelebrita.Q_050}: java.lang.NullPointerException
11-09 16:29:51.920: E/AndroidRuntime(1510):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
11-09 16:29:51.920: E/AndroidRuntime(1510):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
11-09 16:29:51.920: E/AndroidRuntime(1510):     at android.app.ActivityThread.access$600(ActivityThread.java:140)
11-09 16:29:51.920: E/AndroidRuntime(1510):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
11-09 16:29:51.920: E/AndroidRuntime(1510):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-09 16:29:51.920: E/AndroidRuntime(1510):     at android.os.Looper.loop(Looper.java:137)
11-09 16:29:51.920: E/AndroidRuntime(1510):     at android.app.ActivityThread.main(ActivityThread.java:4898)
11-09 16:29:51.920: E/AndroidRuntime(1510):     at java.lang.reflect.Method.invokeNative(Native Method)
11-09 16:29:51.920: E/AndroidRuntime(1510):     at java.lang.reflect.Method.invoke(Method.java:511)
11-09 16:29:51.920: E/AndroidRuntime(1510):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
11-09 16:29:51.920: E/AndroidRuntime(1510):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
11-09 16:29:51.920: E/AndroidRuntime(1510):     at dalvik.system.NativeStart.main(Native Method)
11-09 16:29:51.920: E/AndroidRuntime(1510): Caused by: java.lang.NullPointerException
11-09 16:29:51.920: E/AndroidRuntime(1510):     at org.quizcelebrita.Q_050.onCreate(Q_050.java:32)
11-09 16:29:51.920: E/AndroidRuntime(1510):     at android.app.Activity.performCreate(Activity.java:5206)
11-09 16:29:51.920: E/AndroidRuntime(1510):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
11-09 16:29:51.920: E/AndroidRuntime(1510):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
11-09 16:29:51.920: E/AndroidRuntime(1510):     ... 11 more

检测到问题的行是这样的:

   if(str.equals("activity_two")){
4

2 回答 2

2

改变

 Bundle value = getIntent().getExtras();
 String str= value.getString("sent").toString();

String str = getIntent().getStringExtra("sent");

另外,Java中的字符串比较应该通过equals方法进行

if(str.equals("activity_two"))
于 2013-11-09T15:23:29.007 回答
0

在您提到的 if 条件下,您得到一个空指针异常尝试调试您的应用程序并在此行上放置一个断点:-

String str = getIntent().getStringExtra("sent");

检查 str 是否获得任何值。我猜它没有收到任何字符串,所以这就是它显示空指针异常的原因

于 2013-11-09T17:03:32.257 回答