我有一个类,我应该在其中插入一些字符串(取自 strings.xml)到StringBuffer
. 这将传递给一个活动。问题是我应该使用 getString() 方法,但我不能,因为它是一个类。我能做些什么?
我尝试将活动的上下文传递给它,如下所示:
MainActivity aq = new MainActivity();
Context ctx = aq.getApplicationContext();
this.primo= this.primo.append(ctx.getString(R.string.A)+ "\n");
但是当要打印字符串时应用程序崩溃。并提出这个例外:
11-07 16:50:56.750: E/AndroidRuntime(11871): FATAL EXCEPTION: main
11-07 16:50:56.750: E/AndroidRuntime(11871): java.lang.NullPointerException
11-07 16:50:56.750: E/AndroidRuntime(11871): at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:100)
11-07 16:50:56.750: E/AndroidRuntime(11871): at com.example.test.Mainclass.<init>(Mainclass.java:130)
11-07 16:50:56.750: E/AndroidRuntime(11871): at com.example.test.MainActivity.calcolatore(MainActivity.java:534)
11-07 16:50:56.750: E/AndroidRuntime(11871): at com.example.test.MainActivity$18.onClick(MainActivity.java:500)
11-07 16:50:56.750: E/AndroidRuntime(11871): at android.view.View.performClick(View.java:2485)
11-07 16:50:56.750: E/AndroidRuntime(11871): at android.view.View$PerformClick.run(View.java:9080)
11-07 16:50:56.750: E/AndroidRuntime(11871): at android.os.Handler.handleCallback(Handler.java:587)
11-07 16:50:56.750: E/AndroidRuntime(11871): at android.os.Handler.dispatchMessage(Handler.java:92)
11-07 16:50:56.750: E/AndroidRuntime(11871): at android.os.Looper.loop(Looper.java:130)
11-07 16:50:56.750: E/AndroidRuntime(11871): at android.app.ActivityThread.main(ActivityThread.java:3687)
11-07 16:50:56.750: E/AndroidRuntime(11871): at java.lang.reflect.Method.invokeNative(Native Method)
11-07 16:50:56.750: E/AndroidRuntime(11871): at java.lang.reflect.Method.invoke(Method.java:507)
11-07 16:50:56.750: E/AndroidRuntime(11871): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
11-07 16:50:56.750: E/AndroidRuntime(11871): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
11-07 16:50:56.750: E/AndroidRuntime(11871): at dalvik.system.NativeStart.main(Native Method)