从strings.xml加载boolean时的Nullpointer

时间:2013-06-13 09:04:42

标签: android android-resources

我在strings.xml中添加了一个boolean var,但是当我尝试使用以下代码检索它时。

boolean custom = getResources().getBoolean(R.bool.customMade);

我得到了一个nullpointerexception:

06-13 11:01:45.983: W/System.err(11951): java.lang.NullPointerException
06-13 11:01:45.983: W/System.err(11951):    at android.content.ContextWrapper.getResources(ContextWrapper.java:89)
06-13 11:01:45.993: W/System.err(11951):    at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:78)
06-13 11:01:46.003: W/System.err(11951):    at com.koeriers.tools.ConfigFeatures.<init>(ConfigFeatures.java:32)
06-13 11:01:46.003: W/System.err(11951):    at com.koeriers.erasmus.actLogin.<init>(actLogin.java:50)
06-13 11:01:46.003: W/System.err(11951):    at java.lang.Class.newInstanceImpl(Native Method)
06-13 11:01:46.013: W/System.err(11951):    at java.lang.Class.newInstance(Class.java:1319)
06-13 11:01:46.013: W/System.err(11951):    at android.app.Instrumentation.newActivity(Instrumentation.java:1071)
06-13 11:01:46.013: W/System.err(11951):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2163)
06-13 11:01:46.013: W/System.err(11951):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2296)
06-13 11:01:46.023: W/System.err(11951):    at android.app.ActivityThread.access$700(ActivityThread.java:151)
06-13 11:01:46.023: W/System.err(11951):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281)
06-13 11:01:46.023: W/System.err(11951):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-13 11:01:46.023: W/System.err(11951):    at android.os.Looper.loop(Looper.java:137)
06-13 11:01:46.033: W/System.err(11951):    at android.app.ActivityThread.main(ActivityThread.java:5293)
06-13 11:01:46.033: W/System.err(11951):    at java.lang.reflect.Method.invokeNative(Native Method)
06-13 11:01:46.033: W/System.err(11951):    at java.lang.reflect.Method.invoke(Method.java:511)
06-13 11:01:46.033: W/System.err(11951):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
06-13 11:01:46.033: W/System.err(11951):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
06-13 11:01:46.043: W/System.err(11951):    at dalvik.system.NativeStart.main(Native Method)

这是我在strings.xml中添加var的地方:

<resources>
<bool name="customMade">true</bool>
</resources>

strings.xml像往常一样本地化在res / values / strings.xml文件夹中。 谁能看出我做错了什么? 提前致谢

@edit:

似乎我发布的代码不够,所以这里是我尝试从我的资源中检索bool var的整个类:

import android.app.Activity;
import android.content.res.Resources;
import android.database.Cursor;

public class ConfigFeatures extends Activity{

//public vars
Cursor mCursor;
public String HeaderIconColor;
public String HeaderStateColor;
public String BackgroundColor;
public String TextColor;
public String DateColor;
public String OrderColorOne;
public String OrderColorTwo;
public int Mijlpaal1;
public int Mijlpaal2;
public Boolean Ritten;
public Boolean Handtekening;
public Boolean Foto;
public Boolean WijzigOrderDetail;
public Boolean Firma;
public Boolean LocatieGegevens;
public int LocatieInterval;

public ConfigFeatures(){
    //initialize the vars

    try {
        boolean custom = getResources().getBoolean(R.bool.customMade);
        if (custom) {
            DefaultSettings();
        } else {
            try{
                mCursor = Session.globalTDBAdapter().getData("tbl_Setting", new String[]{TDBAdapterSingleton.KEY_HEADERICONCOLOR,
                        TDBAdapterSingleton.KEY_HEADERSTATECOLOR,TDBAdapterSingleton.KEY_BACKGROUNDCOLOR,
                        TDBAdapterSingleton.KEY_TEXTCOLOR,TDBAdapterSingleton.KEY_DATECOLOR,
                        TDBAdapterSingleton.KEY_ORDERCOLORONE,TDBAdapterSingleton.KEY_ORDERCOLORTWO,
                        TDBAdapterSingleton.KEY_MIJLPAAL1, TDBAdapterSingleton.KEY_MIJLPAAL2,
                        TDBAdapterSingleton.KEY_RITTEN, TDBAdapterSingleton.KEY_HANDTEKENING,
                        TDBAdapterSingleton.KEY_FOTO,TDBAdapterSingleton.KEY_FIRMA,
                        TDBAdapterSingleton.KEY_WIJZIGORDERDETAIL,TDBAdapterSingleton.KEY_LOCATIEGEGEVENS,
                        TDBAdapterSingleton.KEY_LOCATIEINTERVAL});

                if (mCursor == null){
                    DefaultSettings();
                } else {
                    this.HeaderIconColor = mCursor.getString(mCursor.getColumnIndexOrThrow(TDBAdapterSingleton.KEY_HEADERICONCOLOR));
                    if (this.HeaderIconColor.equals("")){
                        this.HeaderIconColor = "#FFFFFF";
                    }
                    this.HeaderStateColor = mCursor.getString(mCursor.getColumnIndexOrThrow(TDBAdapterSingleton.KEY_HEADERSTATECOLOR));
                    if (this.HeaderStateColor.equals("")){
                        this.HeaderStateColor = "#FF8000";
                    }
                    this.BackgroundColor = mCursor.getString(mCursor.getColumnIndexOrThrow(TDBAdapterSingleton.KEY_BACKGROUNDCOLOR));
                    if (this.BackgroundColor.equals("")){
                        this.BackgroundColor = "#FFFFFF";
                    }
                    this.TextColor = mCursor.getString(mCursor.getColumnIndexOrThrow(TDBAdapterSingleton.KEY_TEXTCOLOR));
                    if (this.TextColor.equals("")){
                        this.TextColor = "#000000";
                    }
                    this.DateColor = mCursor.getString(mCursor.getColumnIndexOrThrow(TDBAdapterSingleton.KEY_DATECOLOR));
                    if (this.DateColor.equals("")){
                        this.DateColor = "#808080";
                    }
                    this.OrderColorOne = mCursor.getString(mCursor.getColumnIndexOrThrow(TDBAdapterSingleton.KEY_ORDERCOLORONE));
                    if (this.OrderColorOne.equals("")){
                        this.OrderColorOne = "#EBE3E3";
                    }
                    this.OrderColorTwo = mCursor.getString(mCursor.getColumnIndexOrThrow(TDBAdapterSingleton.KEY_ORDERCOLORTWO));
                    if (this.OrderColorTwo.equals("")){
                        this.OrderColorTwo = "#FFFFFF";
                    }
                    this.Mijlpaal1 = mCursor.getInt(mCursor.getColumnIndexOrThrow(TDBAdapterSingleton.KEY_MIJLPAAL1));
                    this.Mijlpaal2 = mCursor.getInt(mCursor.getColumnIndexOrThrow(TDBAdapterSingleton.KEY_MIJLPAAL2));
                    this.Ritten = mCursor.getString(mCursor.getColumnIndexOrThrow(TDBAdapterSingleton.KEY_RITTEN)).contains("0");
                    this.Handtekening = mCursor.getString(mCursor.getColumnIndexOrThrow(TDBAdapterSingleton.KEY_HANDTEKENING)).contains("0");
                    this.Foto = mCursor.getString(mCursor.getColumnIndexOrThrow(TDBAdapterSingleton.KEY_FOTO)).contains("0");
                    this.WijzigOrderDetail = mCursor.getString(mCursor.getColumnIndexOrThrow(TDBAdapterSingleton.KEY_WIJZIGORDERDETAIL)).contains("0");
                    this.Firma = mCursor.getString(mCursor.getColumnIndexOrThrow(TDBAdapterSingleton.KEY_FIRMA)).contains("0");
                    this.LocatieGegevens = mCursor.getString(mCursor.getColumnIndexOrThrow(TDBAdapterSingleton.KEY_LOCATIEGEGEVENS)).contains("0");
                    this.LocatieInterval = mCursor.getInt(mCursor.getColumnIndexOrThrow(TDBAdapterSingleton.KEY_LOCATIEINTERVAL));
                    mCursor.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
                mCursor.close();
            } finally {
                DefaultSettings();
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        DefaultSettings();
    }
}
public void DefaultSettings(){
    this.HeaderIconColor = "#FFFFFF";
    this.HeaderStateColor = "#3DA2C8";
    this.BackgroundColor = "#FFFFFF";
    this.TextColor = "#000000";
    this.DateColor = "#808080";
    this.OrderColorOne = "#3DA2C8";
    this.OrderColorTwo = "#FFFFFF";
    this.Mijlpaal1 = 1;
    this.Mijlpaal2 = 2;
    this.Ritten = false;
    this.Handtekening = true;
    this.Foto = true;
    this.WijzigOrderDetail = true;
    this.Firma = true;
    this.LocatieGegevens = true;
    this.LocatieInterval = 1000 * 60 * 5;
}

}

6 个答案:

答案 0 :(得分:1)

将其更改为:

public ConfigFeatures(Activity activity){
    boolean custom = activity.getResources().getBoolean(R.bool.customMade);

,调用ConfigFeatures()的活动就像这样ConfigFeatures(getBaseContext()

答案 1 :(得分:0)

我想要一些内容到strings.xml中,你应该写一下

getResources().getString(R.string.customMade);

这是你的NullPointerException的来源。另一方面,不要认为你能够将boolean或其他值放入strings.xml

答案 2 :(得分:0)

getResources()返回null,因为它无法找到您的文件。

第一个显而易见的问题是,当您的布尔位于R.bool

时,您指向R.string

将bool放在bools.xml中或使用R.string(最好只使用bools.xml)。

注意:

@Raghunandan问一个问题,因为getResources()也可能缺少上下文。

根据您拨打电话的位置,当您未将Context作为参考传递时,getResource()也会返回null。例如:getApplicationContext().getResources();

答案 3 :(得分:0)

这样做:

<?xml version="1.0" encoding="utf-8"?>
<resources>
     <item type="bool" name="customMade">true</item>
</resources>

然后在代码中通过执行以下操作获取值:

boolean custom= getResources().getBoolean(R.bool.customMade);

答案 4 :(得分:0)

你应该有这个/res/values/bools.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="customMade">true</bool>
</resources>

参考

Resources res = getResources();
boolean custom  = res.getBoolean(R.bool.customMade);

http://developer.android.com/guide/topics/resources/more-resources.html#Bool

如果您在非活动类中使用getResources(),则需要上下文。将活动的上下文传递给非活动类构造函数并使用相同的

形成您的活动

   new  ConfigFeatures(ActivityName.this);

在你的非活动分类中

   Context mContext;  
   public ConfigFeatures(Context c){
     mContext =c; // use the context to get resources

   }

答案 5 :(得分:0)

您过早访问资源,如stacktrace中的此行所示:

 06-13 11:01:46.003: W/System.err(11951):    at com.koeriers.erasmus.actLogin.<init>(actLogin.java:50)

这意味着actLogin的初始化代码。将需要访问资源的成员变量init代码移动到onCreate()

- 问题更新后编辑

您的ConfigFeatures不是活动,即使它扩展了它。因此,Context无效getResources()。解决方案:删除extends Activity并向构造函数添加Context参数,在传入的上下文中调用getResources()