I am having trouble accessing my XML resources from outside my main activity. I suspect this is easy to do, but I am a novice to android/ java and this is driving me crazy.
I want to declare a class so it is not nested, but rather public to all other classes in my program. However, when I create this class, I cannot access the XML resources, like I could in my main Activity. While "getString(R.string.def_id)" works fine in my main Activity, if I try this in my non-main class, Eclipse tells me "getString" is not defined for this class. I tried using "getResources().getString(R.string.def_id)" but it said getResources() cannot be resolved". I even created a dummy ContextWrapper so I could access resources that way: ContextWrapper context = new ContextWrapper(null); this.setId(context.getResources().getString(R.string.def_id), true); When I do this, the contextWrapper is created OK, but I get a null pointer exception thrown at the second line ("this.setId(context..."). I have looked for similar problems on the web and forums but to no avail. Somebody posted that they were unable to use their resources in the initialization block of their main Activity, and could only use them in (or after) their onCreate() method where they worked fine. I suspect this is part of the same problem, but there was no solution posted to that thread... Any help would be greatly appreciated. Full code below: This works: DEFAULTS.XML: <?xml version="1.0" encoding="UTF-8"?> <resources> <string name="def_id">007</string> </resources> RESOURCEQUESTION.JAVA: package com.northwestradiology.com; import android.app.ListActivity; import android.os.Bundle; import android.util.Log; public class ResourceQuestion extends ListActivity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.v("onCreate", "entering on create"); Person1 me = new Person1(); Log.v("onCreate", "def_id = " + getString(R.string.def_id)); } } PERSON1.JAVA package com.northwestradiology.com; public class Person1 { public String Id; public Person1() { super(); } } ------------------------------- This doesn't work: DEFAULTS.XML: <?xml version="1.0" encoding="UTF-8"?> <resources> <string name="def_id">007</string> </resources> RESOURCEQUESTION.JAVA package com.northwestradiology.com; import android.app.ListActivity; import android.os.Bundle; import android.util.Log; public class ResourceQuestion extends ListActivity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.v("onCreate", "entering on create"); Person1 me = new Person1(); } } PERSON1.JAVA package com.northwestradiology.com; import android.content.ContextWrapper; import android.util.Log; public class Person1 { public String Id; public Person1() { super(); ContextWrapper context = new ContextWrapper(null); Log.v("person construct", "after context wrapper..."); this.Id = (context.getResources().getString(R.string.def_id)); Log.v("person construct", "after getResources..."); } } ERROR LOG: 09-24 01:28:56.985: VERBOSE/onCreate(2789): entering on create 09-24 01:28:56.985: VERBOSE/person construct(2789): after context wrapper... 09-24 01:28:56.985: DEBUG/AndroidRuntime(2789): Shutting down VM 09-24 01:28:56.997: WARN/dalvikvm(2789): threadid=3: thread exiting with uncaught exception (group=0x40010e28) 09-24 01:28:56.997: ERROR/AndroidRuntime(2789): Uncaught handler: thread main exiting due to uncaught exception 09-24 01:28:57.007: ERROR/AndroidRuntime(2789): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.northwestradiology.com/ com.northwestradiology.com.ResourceQuestion}: java.lang.NullPointerException etc... Thanks, Jim --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~----------~----~----~----~------~----~------~--~---