我收到一個空指針異常,試圖在用戶選擇不同的語言環境時嘗試獲取新的活動。它似乎在標記的意圖行中發生。 (!我是小白這麼簡單的解釋請) 感謝使用意圖的空指針異常
public void changeLocale(String locale){
try{
String l=locale;
if(l.equals("uk")){
this.isUk=true;
Log.i(Log_tag,"isUk is true");
//Toast.makeText(this, "UK region selected in Prefs", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Prefs.this, UK.class);//<---ERROR IS OCCURING HERE
startActivity(intent);
}else if(l.equals("eu")){
this.isEu=true;
Toast.makeText(this, "EU region selected", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Prefs.this, EU.class);
startActivity(intent);
}else if(l.equals("us")){
this.isUs=true;
Toast.makeText(this, "Us region selected", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Prefs.this, US.class);
startActivity(intent);
}else if (l.equals("")){
Log.i(Log_tag,"no locale passed in");
finish();
}
}catch (NullPointerException e){
Log.i(Log_tag, "Null Pointer Error in changeLocale()"+e);
finish();
}
}
我logcat中這樣說:
05-25 11:55:57.851: WARN/KeyCharacterMap(898): No keyboard for id 0
05-25 11:55:57.861: WARN/KeyCharacterMap(898): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
05-25 11:56:01.522: INFO/(898): UK selected in UsFM
05-25 11:56:01.543: INFO/(898): editRegion() started
05-25 11:56:01.642: INFO/(898): isUk is true
05-25 11:56:01.642: INFO/(898): Null Pointer Error in changeLocale()java.lang.NullPointerException
05-25 11:56:01.953: INFO/NotificationService(69): enqueueToast pkg=com.silifeform.android [email protected] duration=0
05-25 11:56:01.995: INFO/ActivityManager(69): Starting activity: Intent { cmp=com.silifeform.android/.Us }
05-25 11:56:02.182: WARN/InputManagerService(69): Starting input on non-focused client [email protected] (uid=10024 pid=898)
全班看起來是這樣的:
package com.silifeform.android;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
public class Prefs extends Activity {
public static final String PREFS_NAME="LocalePrefs";
private String region;
public boolean isUk;
public boolean isUs;
public boolean isEu;
private String Log_tag;
@Override
protected void onCreate(Bundle state) {
super.onCreate(state);
//restore prefs
SharedPreferences settings= getSharedPreferences(PREFS_NAME,0);
String myRegion = settings.getString(region,"us");
this.region=myRegion;
changeLocale(getRegion(this));
}
@Override
protected void onResume() {
super.onResume();
changeLocale(getRegion(this));
}
public void editRegion(Context cxt, String sregion) {
setRegion(sregion);
Log.i(Log_tag,"editRegion() started");
SharedPreferences settings = cxt.getSharedPreferences(PREFS_NAME,0);
SharedPreferences.Editor ed = settings.edit();
ed.clear();
ed.putString(this.region,sregion);
ed.commit();
changeLocale(sregion);
}
public String getRegion(Context cxt){
SharedPreferences settings= cxt.getSharedPreferences(PREFS_NAME,0);
String myRegion = settings.getString(this.region,"us");
return myRegion;
}
public void setRegion(String region) {
this.region = region;
}
public void changeLocale(String locale){
try{
String l=locale;
if(l.equals("uk")){
this.isUk=true;
Log.i(Log_tag,"isUk is true");
Log.i(Log_tag,l);
//Toast.makeText(this, "UK region selected in Prefs", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Prefs.this, UkFuelMoney.class);
startActivity(intent);
}else if(l.equals("eu")){
this.isEu=true;
Toast.makeText(this, "EU region selected", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Prefs.this, EuFuelMoney.class);
startActivity(intent);
}else if(l.equals("us")){
this.isUs=true;
Toast.makeText(this, "Us region D selected", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Prefs.this, UsFuelMoney.class);
startActivity(intent);
}else if (l.equals("")){
Log.i(Log_tag,"no locale passed in");
finish();
}
}catch (NullPointerException e){
Log.i(Log_tag, "Null Pointer Error in changeLocale()"+e);
finish();
}
}
@Override
protected void onStop() {
super.onStop();
SharedPreferences settings = getPreferences(0);
SharedPreferences.Editor ed = settings.edit();
ed.putString(region,region);
ed.commit();
}
}
感謝基諾。堆棧跟蹤看起來是這樣的:
05-25 12:10:44.162: WARN/System.err(961): java.lang.NullPointerException
05-25 12:10:44.172: WARN/System.err(961): at android.content.ContextWrapper.getPackageName(ContextWrapper.java:120)
05-25 12:10:44.182: WARN/System.err(961): at android.content.ComponentName.<init>(ComponentName.java:75)
05-25 12:10:44.182: WARN/System.err(961): at android.content.Intent.<init>(Intent.java:2302)
05-25 12:10:44.182: WARN/System.err(961): at com.silifeform.android.Prefs.changeLocale(Prefs.java:68)
05-25 12:10:44.192: WARN/System.err(961): at com.silifeform.android.Prefs.editRegion(Prefs.java:46)
05-25 12:10:44.192: WARN/System.err(961): at com.silifeform.android.Us.onOptionsItemSelected(Us.java:347)
05-25 12:10:44.192: WARN/System.err(961): at android.app.Activity.onMenuItemSelected(Activity.java:2096)
05-25 12:10:44.202: WARN/System.err(961): at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:825)
05-25 12:10:44.202: WARN/System.err(961): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:139)
05-25 12:10:44.202: WARN/System.err(961): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:813)
05-25 12:10:44.202: WARN/System.err(961): at com.android.internal.view.menu.MenuDialogHelper.onClick(MenuDialogHelper.java:120)
05-25 12:10:44.212: WARN/System.err(961): at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:884)
05-25 12:10:44.212: WARN/System.err(961): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
05-25 12:10:44.212: WARN/System.err(961): at android.widget.ListView.performItemClick(ListView.java:3246)
05-25 12:10:44.222: WARN/System.err(961): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1635)
05-25 12:10:44.222: WARN/System.err(961): at android.os.Handler.handleCallback(Handler.java:587)
05-25 12:10:44.222: WARN/System.err(961): at android.os.Handler.dispatchMessage(Handler.java:92)
05-25 12:10:44.232: WARN/System.err(961): at android.os.Looper.loop(Looper.java:123)
05-25 12:10:44.232: WARN/System.err(961): at android.app.ActivityThread.main(ActivityThread.java:4203)
05-25 12:10:44.242: WARN/System.err(961): at java.lang.reflect.Method.invokeNative(Native Method)
05-25 12:10:44.242: WARN/System.err(961): at java.lang.reflect.Method.invoke(Method.java:521)
05-25 12:10:44.242: WARN/System.err(961): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
05-25 12:10:44.252: WARN/System.err(961): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
05-25 12:10:44.252: WARN/System.err(961): at dalvik.system.NativeStart.main(Native Method)
05-25 12:10:44.252: INFO/(961): Null Pointer Error in changeLocale()java.lang.NullPointerException
plz在這裏添加您的日誌, – 2011-05-25 11:52:35
什麼是'Prefs',是它還是'Prefs.this' null? – forsvarir 2011-05-25 11:55:41
只是打印「l」來控制和檢查你進入它並且還把你的日誌 – Sumant 2011-05-25 11:57:35