0

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

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

我得到一个空指针异常:

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 文件夹中。谁能看到我做错了什么?提前致谢

@编辑:

似乎我发布的代码不够多,所以这是我试图从我的资源中检索 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;
}

}

4

6 回答 6

1

将其更改为:

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

和活动调用ConfigFeatures()使它像这样ConfigFeatures(getBaseContext()

于 2013-06-13T09:34:58.873 回答
0

我你想在strings.xml中添加一些东西,你应该写

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

这就是您的 NullPointerException 的来源。另一方面,不要认为您可以将布尔值或其他值放入 strings.xml

于 2013-06-13T09:09:18.250 回答
0

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

第一个明显的就是R.bool当你的布尔值在R.string

将您的 bool 放入 bools.xml 或使用 R.string(最好只使用 bools.xml)。

笔记:

@Raghunandan 提出了一个空洞的问题,因为也有可能getResources()缺少上下文。

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

于 2013-06-13T09:10:52.910 回答
0

像这样做:

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

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

boolean custom= getResources().getBoolean(R.bool.customMade);
于 2013-06-13T09:12:46.390 回答
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() ,则需要 context 。将活动的上下文传递给非活动类构造函数并使用相同的

形成你的活动

   new  ConfigFeatures(ActivityName.this);

在你的非活动课上

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

   }
于 2013-06-13T09:14:03.523 回答
0

如堆栈跟踪中的这一行所示,您访问资源的时间过早:

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

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

-- 问题更新后编辑

ConfigFeatures不是一个活动,即使它扩展了它。因此,它没有有效ContextgetResources()工作。解决方案:删除extends Activity并添加一个Context参数到构造函数,调用getResources()传入的上下文。

于 2013-06-13T09:24:50.250 回答