android application crashes at the getString() line

孤街浪徒 提交于 2019-12-02 01:50:26
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().

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

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