2

这是我的 strings.xml

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

我试图在我的主要活动中调用这个字符串,如下所示:

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

我也检查了我的 R.java 文件,'alert_internet' 的 int 存在。但是由于某种原因,每当我启动应用程序时,它都会崩溃。试图得到一个简单的字符串我做错了什么?

这是我的 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
4

3 回答 3

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)

您不能将活动用作Contextuntil onCreate()。从类初始化中删除getString()调用(由 暗示<init>)并将其移至onCreate().

于 2013-09-23T10:23:02.733 回答
0

你应该这样称呼它:

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

在一个Context(例如在一个Activity)。

更多信息在这里:http: //developer.android.com/reference/android/content/Context.html#getResources()

于 2013-09-23T10:19:12.943 回答
0

尝试在活动的 onCreate() 函数中初始化变量。所以:

串网;

然后在你的 onCreate() net = getString(R.string.alert_internet);

尽管我意识到那时您将无法使用最终变量。

于 2021-03-01T21:57:52.513 回答