java - android可能出现空指针异常

标签 java android

我正在 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>

最佳答案

当您全局声明时,

getResources() 为 null。将 resletters 的初始化移至 onCreate() 方法,并将声明保留在当前位置。

关于java - android可能出现空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23301387/

相关文章:

java - 项目运行缓慢

java - 将Android手机转换为FM发射器

android - 如何在不进入 GPS 设置页面的情况下启用和禁用 GPS(单击按钮)

java - 从数据库读取到谷歌地图

android - DisplayListCanvas 在未绑定(bind)的 RenderNode 上启动(没有 mOwningView)

java - 在 Java Web 应用程序中提取网络用户登录信息

java - 如何使用 Spring Boot 从类中仅返回几种类型?

java - this(<params>) 作为构造函数中的快捷方式?

android - 'keystore' 的值无效。它必须解析为单一路径

Android比较2张图片并告诉水果类别是水果(苹果/香蕉)还是不是水果