【问题标题】:Possible nullpointer exception android可能的空指针异常android
【发布时间】:2014-04-25 19:25:48
【问题描述】:

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

【问题讨论】:

  • 你能发布你的 Logcat 吗?
  • 当谈到错误时,请将它们与所有代码一起发布。
  • 你在 tv.setText() 获得 NPE 吗?
  • prediction 为空?顺便说一句,请学习使用调试器。这些问题是最容易找到的错误。在方法的第一行放置一个断点,运行你的应用程序,单步执行,直到你看到问题。工作完成。
  • 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.&lt;init&gt;(MainActivity.java:21) - 第 21 行是什么?

标签: java android


【解决方案1】:

getResources() 在全局声明时为空。将 resletters 的初始化移动到您的 onCreate() 方法,并将声明保留在您当前拥有它们的位置。

【讨论】:

  • 好地方。我错过了。
猜你喜欢
  • 2016-07-12
  • 2013-04-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多