android application crashes at the getString() line

≡放荡痞女 提交于 2019-12-20 03:20:02

问题


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

回答1:


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().




回答2:


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()



来源:https://stackoverflow.com/questions/18956766/android-application-crashes-at-the-getstring-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!