Question

this is my strings.xml

 <resources>
 <string name="alert_internet">out</string>
 </resources>

I am trying to call this string in my main activity like below:

 final String net = getString(R.string.alert_internet);

I also checked my R.java file too, the int of 'alert_internet' exists. But for some reason whenever I launch the application it crashes down to the ground. What am I doing wrong trying to get a simple string?

this is my log.cat

   09-23 13:17:33.092: E/AndroidRuntime(21302): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1573)
   09-23 13:17:33.092: E/AndroidRuntime(21302):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
   09-23 13:17:33.092: E/AndroidRuntime(21302):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
   09-23 13:17:33.092: E/AndroidRuntime(21302):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
   09-23 13:17:33.092: E/AndroidRuntime(21302):     at android.os.Handler.dispatchMessage(Handler.java:99)
   09-23 13:17:33.092: E/AndroidRuntime(21302):     at android.os.Looper.loop(Looper.java:130)
   09-23 13:17:33.092: E/AndroidRuntime(21302):     at android.app.ActivityThread.main(ActivityThread.java:3693)
   09-23 13:17:33.092: E/AndroidRuntime(21302):     at java.lang.reflect.Method.invokeNative(Native Method)
   09-23 13:17:33.092: E/AndroidRuntime(21302):     at java.lang.reflect.Method.invoke(Method.java:507)
   09-23 13:17:33.092: E/AndroidRuntime(21302):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
   09-23 13:17:33.092: E/AndroidRuntime(21302):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
   09-23 13:17:33.092: E/AndroidRuntime(21302):     at dalvik.system.NativeStart.main(Native Method)
   09-23 13:17:33.092: E/AndroidRuntime(21302): Caused by: java.lang.NullPointerException
   09-23 13:17:33.092: E/AndroidRuntime(21302):     at android.content.ContextWrapper.getResources(ContextWrapper.java:80)
   09-23 13:17:33.092: E/AndroidRuntime(21302):     at android.content.Context.getString(Context.java:183)
   09-23 13:17:33.092: E/AndroidRuntime(21302):     at com.akilli.ticaret.Main.<init>(Main.java:25)
   09-23 13:17:33.092: E/AndroidRuntime(21302):     at java.lang.Class.newInstanceImpl(Native Method)
   09-23 13:17:33.092: E/AndroidRuntime(21302):     at java.lang.Class.newInstance(Class.java:1409)
   09-23 13:17:33.092: E/AndroidRuntime(21302):     at  android.app.Instrumentation.newActivity(Instrumentation.java:1021)
   09-23 13:17:33.092: E/AndroidRuntime(21302):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1565)
   09-23 13:17:33.092: E/AndroidRuntime(21302):     ... 11 more
   09-23 13:17:33.102: E/(179): Dumpstate > /data/log/dumpstate_app_error
Was it helpful?

Solution

Caused by: java.lang.NullPointerException
   09-23 13:17:33.092: E/AndroidRuntime(21302):     at android.content.ContextWrapper.getResources(ContextWrapper.java:80)
   09-23 13:17:33.092: E/AndroidRuntime(21302):     at android.content.Context.getString(Context.java:183)
   09-23 13:17:33.092: E/AndroidRuntime(21302):     at com.akilli.ticaret.Main.<init>(Main.java:25)

You cannot use the activity as a Context until onCreate(). Remove the getString() call from class initialization (as implied by <init>) and move it to onCreate().

OTHER TIPS

You should call it like that:

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

on a Context (e.g. in an Activity).

More info in here: http://developer.android.com/reference/android/content/Context.html#getResources()

Try initializing your variable in the onCreate() function of your activity. So:

String net;

And then in your onCreate() net = getString(R.string.alert_internet);

Although I realize you wouldn't be able to use a final variable then.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top