我的Android应用程序中的致命异常

时间:2014-04-30 15:00:44

标签: java android eclipse exception

我得到了一个似乎由nullpointer异常引起的致命异常。 Logcat指向我在onCreate方法中的java第39行。这是我的onCreate和我的全局变量。

GestureLibrary mLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);
    Resources res;
    String [] letters;
    int i = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (!mLibrary.load())
        {
           finish();
        }


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

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

这对我来说似乎很好,所以我不知道它为什么会给出一个nullpointer异常。我的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 10:48:48.418: D/dalvikvm(1465): GC_FOR_ALLOC freed 248K, 10% free 3137K/3452K, paused 86ms, total 96ms
04-30 10:48:48.418: D/AndroidRuntime(1465): Shutting down VM
04-30 10:48:48.428: W/dalvikvm(1465): threadid=1: thread exiting with uncaught exception (group=0xb1a84ba8)
04-30 10:48:48.468: E/AndroidRuntime(1465): FATAL EXCEPTION: main
04-30 10:48:48.468: E/AndroidRuntime(1465): Process: com.gmail.Sheridjohn.letterchecker, PID: 1465
04-30 10:48:48.468: E/AndroidRuntime(1465): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gmail.Sheridjohn.letterchecker/com.gmail.Sheridjohn.letterchecker.MainActivity}: java.lang.NullPointerException
04-30 10:48:48.468: E/AndroidRuntime(1465):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-30 10:48:48.468: E/AndroidRuntime(1465):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-30 10:48:48.468: E/AndroidRuntime(1465):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-30 10:48:48.468: E/AndroidRuntime(1465):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-30 10:48:48.468: E/AndroidRuntime(1465):     at android.os.Handler.dispatchMessage(Handler.java:102)
04-30 10:48:48.468: E/AndroidRuntime(1465):     at android.os.Looper.loop(Looper.java:136)
04-30 10:48:48.468: E/AndroidRuntime(1465):     at android.app.ActivityThread.main(ActivityThread.java:5017)
04-30 10:48:48.468: E/AndroidRuntime(1465):     at java.lang.reflect.Method.invokeNative(Native Method)
04-30 10:48:48.468: E/AndroidRuntime(1465):     at java.lang.reflect.Method.invoke(Method.java:515)
04-30 10:48:48.468: E/AndroidRuntime(1465):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-30 10:48:48.468: E/AndroidRuntime(1465):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-30 10:48:48.468: E/AndroidRuntime(1465):     at dalvik.system.NativeStart.main(Native Method)
04-30 10:48:48.468: E/AndroidRuntime(1465): Caused by: java.lang.NullPointerException
04-30 10:48:48.468: E/AndroidRuntime(1465):     at com.gmail.Sheridjohn.letterchecker.MainActivity.onCreate(MainActivity.java:39)
04-30 10:48:48.468: E/AndroidRuntime(1465):     at android.app.Activity.performCreate(Activity.java:5231)
04-30 10:48:48.468: E/AndroidRuntime(1465):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-30 10:48:48.468: E/AndroidRuntime(1465):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-30 10:48:48.468: E/AndroidRuntime(1465):     ... 11 more
04-30 10:48:54.378: I/Process(1465): Sending signal. PID: 1465 SIG: 9

编辑:我已将mLibrary更改为null然后在我的OnCreate中初始化它,但它仍在第39行打破,这是:

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

1 个答案:

答案 0 :(得分:3)

第一个问题在这里:

GestureLibrary mLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);

正确的方法是:

GestureLibrary mLibrary = null;
Resources res;
String [] letters;
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);
    TextView tv = (TextView) findViewById(R.id.text);
    tv.setText("Draw the letter: "+letters[i]);

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