Issue
I've added a boolean var to my strings.xml, but when I am trying to retrieve it with the following code.
boolean custom = getResources().getBoolean(R.bool.customMade);
I am getting a 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)
this is where I add the var in my strings.xml:
<resources>
<bool name="customMade">true</bool>
</resources>
The strings.xml is localized in the res/values/strings.xml folder as usual. Anyone who can see what I am doing wrong? Thanks in advance
@edit:
it seems that I've posted not enough code, so here is the whole class in which I trying to retrieve the bool var from my resources:
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;
}
}
Solution
Change it to:
public ConfigFeatures(Activity activity){
boolean custom = activity.getResources().getBoolean(R.bool.customMade);
and the activity calling ConfigFeatures()
make it like this ConfigFeatures(getBaseContext()
Answered By - LuckyMe
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.