在贏得StackOverloads「年度蠢貨獎」的風險中,我仍然有這個問題(嘆氣)。我對這個其他職位已經:startActivityForResult空指針異常。語境?
- Null Pointer Exception using Intent
- NullPointerException in SharedPreferences Android
- How to route the user to the correct activity?
呼叫來自一個子菜單:
public boolean onOptionsItemSelected(MenuItem item){
pob = new Prefs();// public Prefs pob; in field vars
switch (item.getItemId()){
//-------------------------------------Options menu----------------------------------------
case R.id.about:
//Toast.makeText(this, "About menu", Toast.LENGTH_SHORT).show();
//showAbout();
return true;
case R.id.locale:
//Toast.makeText(this, "Locale menu", Toast.LENGTH_SHORT).show();
return true;
//-----Sub menu----------
case R.id.uk_item://<-- USING THIS BLOCK FOR CALL
Toast.makeText(this, "UK selected in UsFM", Toast.LENGTH_SHORT).show();// this is ok
Log.i(LogTag, "UK selected");
if(pob.isUk&&item.isChecked()){
item.setChecked(true);
}else item.setChecked(false);
pob.changeLocale(this,"uk");
//finish();
return true;
case R.id.us_item:
//Toast.makeText(this, "US already selected", Toast.LENGTH_SHORT).show();
if(pob.isUs&&item.isChecked()){
item.setChecked(true);
}
else item.setChecked(false);
pob.setRegion("us");
pob.getRegion(this);
finish();
return true;
case R.id.eu_item:
Toast.makeText(this, "EU selected", Toast.LENGTH_SHORT).show();
if(pob.isEu&&item.isChecked()){
item.setChecked(true);
// pob.changeLocale("eu");
}else item.setChecked(false);
return true;
default :
return super.onOptionsItemSelected(item);
}
它在偏好設置調用此方法等級:
public void changeLocale(Context cxt,String locale){
try{
String l=locale;
if(l.equals("uk")){
this.isUk=true;
Log.i(Log_tag,l);
//Toast.makeText(this, "UK region selected in Prefs",Toast.LENGTH_SHORT).show();
Intent intent = new Intent(cxt, Uk.class);
Log.i(Log_tag,"intent run");
startActivity(intent);// <--- ERROR HERE
}else if(l.equals("eu")){
this.isEu=true;
//Toast.makeText(this, "EU region selected", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(cxt, 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(cxt, Us.class);
startActivity(intent);
}else if (l.equals("")){
Log.i(Log_tag,"no locale passed in");
finish();
}
}catch (NullPointerException e){
e.printStackTrace();
Log.i(Log_tag, "Null Pointer Error in changeLocale()"+e);
finish();
}
}
堆棧跟蹤爲:
05-26 13:53:05.880: WARN/System.err(478): java.lang.NullPointerException
05-26 13:53:05.890: WARN/System.err(478): at android.app.Activity.startActivityForResult(Activity.java:2661)
05-26 13:53:05.900: WARN/System.err(478): at android.app.Activity.startActivity(Activity.java:2705)
05-26 13:53:05.900: WARN/System.err(478): at com.silifeform.android.Prefs.changeLocale(Prefs.java:70)
05-26 13:53:05.900: WARN/System.err(478): at com.silifeform.android.Us.onOptionsItemSelected(UsFuelMoney.java:347)
05-26 13:53:05.910: WARN/System.err(478): at android.app.Activity.onMenuItemSelected(Activity.java:2096)
05-26 13:53:05.910: WARN/System.err(478): at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:825)
05-26 13:53:05.920: WARN/System.err(478): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:139)
05-26 13:53:05.920:WARN/System.err的(478):在com.android.internal.view.menu.MenuBuilder.performItemAction (MenuBuilder.java:813) 05-26 13:53:05.920:WARN/System.err(478):at com.android.internal.view.menu.MenuDialogHelper.onClick(MenuDialogHelper.java:120)
Stephan非常友好地提出了一些關於實例化onOptionsItemSelected()
中的prefs對象的建議,但是因爲我對android和java都是新手,所以我不知道任何其他方法來inst反對一個對象,而不是我已經嘗試過的兩個對象。
對不起,如果我一遍又一遍地問相同的問題!
謝謝Nallath,多數民衆贊成在意向意圖行上發生的錯誤。我不明白如何確保「this」或者Eu.class是不是空的。作爲擴展活動的一部分,它們是否不會自動獲得上下文? – Broo 2011-05-26 14:36:15
「this」是關鍵字,它是指向活動的指針或查看從中調用的代碼的指針。 Eu.class也不應該是空的,所以我的猜測是你在Intent中放置的cxt(context)是空的或者不是Intent需要的功能。上下文是如何設置的? (它從哪裏獲得上下文?) 您可以隨時嘗試:Intent intent = new Intent(FirstActivity.this,SecondActivity.class); – Nallath 2011-05-26 14:46:09
我認爲上下文是在子菜單調用中設置的:case R.id.uk_item:// < - 使用此塊調用Toast.makeText(這個「UK在UsFM中選擇」,Toast.LENGTH_SHORT).show() ; //這是正確的Log.i(LogTag,「UK selected」); if(pob.isUk && item.isChecked()){item.setChecked(true); } else item.setChecked(false); pob.changeLocale(這一點, 「英國」); //完();返回true; – Broo 2011-05-26 14:50:28