如果已经查看,我有问题显示吐司而不是其他吐司。我有两个活动,在第一个活动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")){
答案 0 :(得分:2)
更改
Bundle value = getIntent().getExtras();
String str= value.getString("sent").toString();
在
String str = getIntent().getStringExtra("sent");
另外,Java中的字符串比较应该通过equals方法
来执行if(str.equals("activity_two"))
答案 1 :(得分:0)
您在提到的if条件中遇到空指针异常 尝试调试您的应用程序并在此行上设置断点: -
String str = getIntent().getStringExtra("sent");
检查str是否获得任何值。我猜它没有收到任何字符串,这就是它显示空指针异常
的原因