我在我的 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;
}
}