原因不明的Nullpointer错误

时间:2014-04-30 18:37:30

标签: java android nullpointerexception

我正在开发一个Android应用程序,在该应用程序中,用户被告知要绘制的字母以及使用手势处理字母识别。我不知道为什么会这样。在eclipse中没有检测到错误,但是logcat声称它是一个nullpointer异常。这是我的应用程序代码:

public class MainActivity extends Activity implements OnGesturePerformedListener
{
    GestureLibrary mLibrary;
    Resources res;
    String [] letters;
    TextView tv = (TextView) findViewById(R.id.text);
    int i = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);
        if (!mLibrary.load())
        {
           finish();
        }

        res = getResources();
        letters = res.getStringArray(R.array.LetterToBeDrawn);
        tv.setText("Draw the letter: " + letters[i]);

        GestureOverlayView gestures = (GestureOverlayView)findViewById(R.id.gestures);
        gestures.addOnGesturePerformedListener(this);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) 
    { 
        ArrayList<Prediction> predictions = new ArrayList<Prediction>();  //this methods asks to recognize the gesture against loaded gesture library
        predictions = mLibrary.recognize(gesture);

        // We want at least one prediction
        if (predictions.size() > 0)
        {
            Prediction prediction = predictions.get(0);    //get the 1st prediction auto generated for you by Android
            // We want at least some confidence in the result
            if (prediction.score > 1.0 && prediction.name.equals(letters[i]))
            {
                // Show the spell
                Toast.makeText(MainActivity.this, prediction.name, Toast.LENGTH_SHORT).show();
            }
        }
    }
}

我的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"
    android:fadeOffset="1000"
    android:gestureStrokeType="multiple"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        />

</android.gesture.GestureOverlayView>

logcat:

04-30 14:36:45.716: D/AndroidRuntime(1558): Shutting down VM
04-30 14:36:45.716: W/dalvikvm(1558): threadid=1: thread exiting with uncaught exception (group=0xb2a27ba8)
04-30 14:36:45.776: E/AndroidRuntime(1558): FATAL EXCEPTION: main
04-30 14:36:45.776: E/AndroidRuntime(1558): Process: com.gmail.Sheridjohn.letterchecker, PID: 1558
04-30 14:36:45.776: E/AndroidRuntime(1558): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.gmail.Sheridjohn.letterchecker/com.gmail.Sheridjohn.letterchecker.MainActivity}: java.lang.NullPointerException
04-30 14:36:45.776: E/AndroidRuntime(1558):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
04-30 14:36:45.776: E/AndroidRuntime(1558):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-30 14:36:45.776: E/AndroidRuntime(1558):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-30 14:36:45.776: E/AndroidRuntime(1558):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-30 14:36:45.776: E/AndroidRuntime(1558):     at android.os.Handler.dispatchMessage(Handler.java:102)
04-30 14:36:45.776: E/AndroidRuntime(1558):     at android.os.Looper.loop(Looper.java:136)
04-30 14:36:45.776: E/AndroidRuntime(1558):     at android.app.ActivityThread.main(ActivityThread.java:5017)
04-30 14:36:45.776: E/AndroidRuntime(1558):     at java.lang.reflect.Method.invokeNative(Native Method)
04-30 14:36:45.776: E/AndroidRuntime(1558):     at java.lang.reflect.Method.invoke(Method.java:515)
04-30 14:36:45.776: E/AndroidRuntime(1558):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-30 14:36:45.776: E/AndroidRuntime(1558):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-30 14:36:45.776: E/AndroidRuntime(1558):     at dalvik.system.NativeStart.main(Native Method)
04-30 14:36:45.776: E/AndroidRuntime(1558): Caused by: java.lang.NullPointerException
04-30 14:36:45.776: E/AndroidRuntime(1558):     at android.app.Activity.findViewById(Activity.java:1884)
04-30 14:36:45.776: E/AndroidRuntime(1558):     at com.gmail.Sheridjohn.letterchecker.MainActivity.<init>(MainActivity.java:23)
04-30 14:36:45.776: E/AndroidRuntime(1558):     at java.lang.Class.newInstanceImpl(Native Method)
04-30 14:36:45.776: E/AndroidRuntime(1558):     at java.lang.Class.newInstance(Class.java:1208)
04-30 14:36:45.776: E/AndroidRuntime(1558):     at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
04-30 14:36:45.776: E/AndroidRuntime(1558):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
04-30 14:36:45.776: E/AndroidRuntime(1558):     ... 11 more

3 个答案:

答案 0 :(得分:2)

你有

 TextView tv = (TextView) findViewById(R.id.text);

在将布局设置为Activity之前。您的初始化失败tv为空。您需要先将布局设置为活动,然后初始化视图。

当你有

   tv.setText("Draw the letter: " + letters[i]);

当tv实际为空时,你正在调用setText

http://androidxref.com/4.4.2_r2/xref/frameworks/base/core/java/android/app/Activity.java#1884

从laalto提供的链接发布来源。感谢他,我纠正了我的帖子

1877    /**
1878     * Finds a view that was identified by the id attribute from the XML that
1879     * was processed in {@link #onCreate}.
1880     *
1881     * @return The view if found or null otherwise.
1882     */
1883    public View findViewById(int id) {
1884        return getWindow().findViewById(id);
1885    }
1886

你的堆栈跟踪确实表明你在标题

中发布的原因并不是无法解释的
  

引起:java.lang.NullPointerException 04-30 14:36:45.776:   E / AndroidRuntime(1558):at   android.app.Activity.findViewById(Activity.java:1884)04-30   14:36:45.776:E / AndroidRuntime(1558):at   com.gmail.Sheridjohn.letterchecker.MainActivity。(MainActivity.java:23)

答案 1 :(得分:2)

原因是您在初始化成员变量时在findViewById()之前调用onCreate()。您的活动尚未WindowfindViewById()需要它。

供参考,这里是导致NPE的代码行的平台源:http://androidxref.com/4.4.2_r2/xref/frameworks/base/core/java/android/app/Activity.java#1884 - getWindow()返回null

其他答案给出的setContentView()解释有效但不完全正确。如果您在Window之前有findViewById()并且是callind setContentView(),则会返回null而不是抛出NPE。

解决方案仍然相同:在onCreate()之后将初始化移至setContentView()

答案 2 :(得分:0)

您的活动在调用findViewById之前呼叫setContentView。将tv = (TextView) findViewById(R.id.text);移至onCreate。