StackOverloads "Dumbass of the Year Award"を獲得する危険がありますが、私はまだこの問題に悩まされています。この上の私の他の記事はされている:startActivityForResult NULLポインタ例外です。コンテキスト?
- 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)
5月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):com.android.internal.view.menu.MenuDialogHelper.onClick(MenuDialogHelper.java:120)
Stephanは、onOptionsItemSelected()
のprefsオブジェクトをインスタンス化することに関して非常に親切にいくつかの提案をしましたが、私がAndroidとjavaの両方に新しいので、私はinstの他の方法を知らない私はすでに試みた2つ以外のオブジェクトを防止する。
私は何度も同じ質問をしています。
ありがとうございました.Nallathさん、Intent Intent行にエラーが発生しています。私は "this"またはEu.classがどのように空でないかを確認する方法を理解していません。アクティビティの拡張の一部としてコンテキストが自動的に与えられないのですか? – Broo
"this"は、コードが呼び出されたアクティビティまたはビューへのポインタであるキーワードです。 Eu.classは空であってはいけません。私は、Intentに入れているcxt(コンテキスト)が空であるか、Intentが機能する必要がないと推測しています。コンテキストはどのように設定されていますか? (どこからコンテキストを取得しますか?) いつでも試してみることができます:Intent intent = new Intent(FirstActivity.this、SecondActivity.class); – Nallath
私はこのコンテキストがサブメニューの呼び出しで設定されていると思う:case R.id.uk_item:// < - このブロックをCALL Toast.makeText(USFMで選択された "UK、Toast.LENGTH_SHORT).show() ; //これはOKですLog.i(LogTag、 "UK selected"); if(pob.isUk && item.isChecked()){item.setChecked(true); } else item.setChecked(false); pob.changeLocale(this、 "uk"); // finish();真を返します。 – Broo