这是我的strings.xml
<resources>
<string name="alert_internet">out</string>
</resources>
我试图在我的主要活动中调用此字符串,如下所示:
final String net = getString(R.string.alert_internet);
我也检查了我的R.java文件,&#39; alert_internet&#39;存在。但出于某种原因,每当我启动应用程序时,它都会崩溃到地面。试图获得一个简单的字符串,我做错了什么?
这是我的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
答案 0 :(得分:3)
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)
在Context
之前,您无法将活动用作onCreate()
。从类初始化中删除getString()
调用(如<init>
所暗示的那样)并将其移至onCreate()
。
答案 1 :(得分:0)
你应该这样称呼它:
getResources().getString(R.string.alert_internet);
在Context
上(例如在Activity
中)。
此处有更多信息:http://developer.android.com/reference/android/content/Context.html#getResources()
答案 2 :(得分:0)
尝试在您的活动的 onCreate() 函数中初始化您的变量。所以:
弦网;
然后在你的 onCreate() net = getString(R.string.alert_internet);
尽管我意识到那时您将无法使用 final 变量。