我正在尝试在 Android 中构建一个手势应用程序。我已成功从 sd 卡中提取手势文件并将其放入我的 res/raw 文件夹中。
但是当我启动应用程序时,它意外终止。
我的代码是:
package com.example.gest;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.GestureOverlayView.OnGesturePerformedListener;
import android.gesture.Prediction;
import android.view.Menu;
import android.widget.Toast;
public class MainActivity extends Activity implements OnGesturePerformedListener {
GestureLibrary mLibrary;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);
if (!mLibrary.load()) {
finish();
}
GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gestures);
gestures.addOnGesturePerformedListener(this);
}
public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
ArrayList<Prediction> predictions = mLibrary.recognize(gesture);
if (predictions.size() > 0 && predictions.get(0).score > 1.0) {
String result = predictions.get(0).name;
if ("o".equalsIgnoreCase(result)) {
Toast.makeText(this, "Opening the document", Toast.LENGTH_LONG).show();
} else if ("s".equalsIgnoreCase(result)) {
Toast.makeText(this, "Saving the document", Toast.LENGTH_LONG).show();
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
我的 main.xml 文件是:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.gest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<android.gesture.GestureOverlayView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gestures"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:eventsInterceptionEnabled="true"
android:gestureStrokeType="multiple"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</android.gesture.GestureOverlayView>
</manifest>
日志猫错误是:
11-30 09:55:05.584: E/AndroidRuntime(5031): FATAL EXCEPTION: main
11-30 09:55:05.584: E/AndroidRuntime(5031): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gest/com.example.gest.MainActivity}: java.lang.NullPointerException
11-30 09:55:05.584: E/AndroidRuntime(5031): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
11-30 09:55:05.584: E/AndroidRuntime(5031): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
11-30 09:55:05.584: E/AndroidRuntime(5031): at android.app.ActivityThread.access$600(ActivityThread.java:130)
11-30 09:55:05.584: E/AndroidRuntime(5031): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
11-30 09:55:05.584: E/AndroidRuntime(5031): at android.os.Handler.dispatchMessage(Handler.java:99)
11-30 09:55:05.584: E/AndroidRuntime(5031): at android.os.Looper.loop(Looper.java:137)
11-30 09:55:05.584: E/AndroidRuntime(5031): at android.app.ActivityThread.main(ActivityThread.java:4745)
11-30 09:55:05.584: E/AndroidRuntime(5031): at java.lang.reflect.Method.invokeNative(Native Method)
11-30 09:55:05.584: E/AndroidRuntime(5031): at java.lang.reflect.Method.invoke(Method.java:511)
11-30 09:55:05.584: E/AndroidRuntime(5031): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-30 09:55:05.584: E/AndroidRuntime(5031): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-30 09:55:05.584: E/AndroidRuntime(5031): at dalvik.system.NativeStart.main(Native Method)
11-30 09:55:05.584: E/AndroidRuntime(5031): Caused by: java.lang.NullPointerException
11-30 09:55:05.584: E/AndroidRuntime(5031): at com.example.gest.MainActivity.onCreate(MainActivity.java:27)
11-30 09:55:05.584: E/AndroidRuntime(5031): at android.app.Activity.performCreate(Activity.java:5008)
11-30 09:55:05.584: E/AndroidRuntime(5031): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
11-30 09:55:05.584: E/AndroidRuntime(5031): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
11-30 09:55:05.584: E/AndroidRuntime(5031): ... 11 more
11-30 09:55:10.323: I/Process(5031): Sending signal. PID: 5031 SIG: 9
如果有人能找出原因,那就太棒了。