使用Intent的空指针异常

时间:2011-05-25 11:50:21

标签: android android-intent nullpointerexception

我正在获取空指针异常,尝试在用户选择其他语言环境时启动新活动。它似乎在标记的意图线中出现。 (请给我一个菜鸟这么简单的解释!) 感谢

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 callback=android.app.ITransientNotification$Stub$Proxy@4380ba08 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 com.android.internal.view.IInputMethodClient$Stub$Proxy@438d8b88 (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

1 个答案:

答案 0 :(得分:1)

您的Prefs活动没有填充,因为您可能已经在com.silifeform.android.Us.onOptionsItemSelected()中自己实例化了(我猜这个实现仍然类似于您发布的here)。正如我在回答你提到的问题时已经指出的那样,如果Prefs类没有被Android实例化(和管理),你就不能假设它正常工作(特别是来自Context的所有方法)因为没有正确启动。同样适用相同的策略,使用您传递给Context的单独changeLocale对象来解决此问题。