如果已经查看过,我无法显示一个 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")){