我正在开发一个android应用程序,用户被告知要绘制什么字母并使用手势处理字母识别。我不知道为什么这会突破。在eclipse中没有检测到错误,但logcat说它是一个空指针异常。这里是我的应用程序代码:无法解释的空指针错误
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
谢谢,我原来就是这样,把它改回原处,但它仍然没有工作,我得到了logcat结果。 – user2316046
@ user2316046它会工作的崩溃可能是因为不同的原因,如果你更新的建议 – Raghunandan