我正在开发一个Android应用程序,它使用手势来表示字母。我在文本区域中显示了一个字符串数组元素,以便用户知道要绘制的字母。 我在全球宣布:
Resources res = getResources();
String [] letters = res.getStringArray(R.array.LetterToBeDrawn)
然后我在我的oncreate中有这个:
TextView tv = (TextView) findViewById(R.id.text);
tv.setText("Draw the letter: "+letters[i]);
在我的gesturePerformed方法中,我得到了这个比较:
if (predictions.size() > 0)
{
Prediction prediction = predictions.get(0);
if (prediction.score > 1.0 && prediction.name.equals(letters[i]))
{
Toast.makeText(MainActivity.this, prediction.name, Toast.LENGTH_SHORT).show();
}
}
当我在我的模拟器上启动它时,我在LogCat中遇到了很多错误,它们似乎是由NullPointerException引起的。但是我看不出那是怎么回事,因为目前的数组只包含三个项目,索引int i设置为1。
编辑:这是LogCat,意思是早先发布:
04-25 15:37:31.266: D/AndroidRuntime(1889): Shutting down VM
04-25 15:37:31.266: W/dalvikvm(1889): threadid=1: thread exiting with uncaught exception (group=0xb1a4eb90)
04-25 15:37:31.366: E/AndroidRuntime(1889): FATAL EXCEPTION: main
04-25 15:37:31.366: E/AndroidRuntime(1889): Process: com.gmail.Sheridjohn.letterchecker, PID: 1889
04-25 15:37:31.366: E/AndroidRuntime(1889): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.gmail.Sheridjohn.letterchecker/com.gmail.Sheridjohn.letterchecker.MainActivity}: java.lang.NullPointerException
04-25 15:37:31.366: E/AndroidRuntime(1889): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2102)
04-25 15:37:31.366: E/AndroidRuntime(1889): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
04-25 15:37:31.366: E/AndroidRuntime(1889): at android.app.ActivityThread.access$700(ActivityThread.java:135)
04-25 15:37:31.366: E/AndroidRuntime(1889): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
04-25 15:37:31.366: E/AndroidRuntime(1889): at android.os.Handler.dispatchMessage(Handler.java:102)
04-25 15:37:31.366: E/AndroidRuntime(1889): at android.os.Looper.loop(Looper.java:137)
04-25 15:37:31.366: E/AndroidRuntime(1889): at android.app.ActivityThread.main(ActivityThread.java:4998)
04-25 15:37:31.366: E/AndroidRuntime(1889): at java.lang.reflect.Method.invokeNative(Native Method)
04-25 15:37:31.366: E/AndroidRuntime(1889): at java.lang.reflect.Method.invoke(Method.java:515)
04-25 15:37:31.366: E/AndroidRuntime(1889): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
04-25 15:37:31.366: E/AndroidRuntime(1889): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
04-25 15:37:31.366: E/AndroidRuntime(1889): at dalvik.system.NativeStart.main(Native Method)
04-25 15:37:31.366: E/AndroidRuntime(1889): Caused by: java.lang.NullPointerException
04-25 15:37:31.366: E/AndroidRuntime(1889): at android.content.ContextWrapper.getResources(ContextWrapper.java:89)
04-25 15:37:31.366: E/AndroidRuntime(1889): at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:78)
04-25 15:37:31.366: E/AndroidRuntime(1889): at com.gmail.Sheridjohn.letterchecker.MainActivity.<init>(MainActivity.java:21)
04-25 15:37:31.366: E/AndroidRuntime(1889): at java.lang.Class.newInstanceImpl(Native Method)
04-25 15:37:31.366: E/AndroidRuntime(1889): at java.lang.Class.newInstance(Class.java:1208)
04-25 15:37:31.366: E/AndroidRuntime(1889): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
04-25 15:37:31.366: E/AndroidRuntime(1889): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2093)
04-25 15:37:31.366: E/AndroidRuntime(1889): ... 11 more
这是我的XML:
<android.gesture.GestureOverlayView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/gestures"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</android.gesture.GestureOverlayView>
答案 0 :(得分:4)
getResources()
为null。将res
和letters
的初始化移至您的onCreate()
方法,并将声明保留在您当前拥有它们的位置。