2012-05-17 82 views
1
public class TestActivity extends Activity implements OnGesturePerformedListener { 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    mLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures); 

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

} 

我的main.xml安卓:空指針異常在addOnGesturePerformedListener

<ImageView 
    android:id="@+id/imageView1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:src="@drawable/ic_launcher" /> 

<android.gesture.GestureOverlayView 
    android:id="@+id/gestureOverlayView1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 
</android.gesture.GestureOverlayView> 

Activity.java:

private GestureLibrary mLibrary; 

public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) { 
    ArrayList<Prediction> predictions = mLibrary.recognize(gesture); 
    Log.v("performed", "performed"); 
    // We want at least one prediction 
    if (predictions.size() > 0) { 
     Prediction prediction = predictions.get(0); 
     // We want at least some confidence in the result 
     if (prediction.score > 1.0) { 
      if (prediction.name.equalsIgnoreCase("right swipe")) { 
       // do you thing here// 
       Toast.makeText(TestActivity.this, "rigth", 
         Toast.LENGTH_SHORT); 
      } else if (prediction.name.equalsIgnoreCase("left swipe")) { 

       Toast.makeText(TestActivity.this, "lef", 
         Toast.LENGTH_SHORT); 
      } 
     } 
    } 
} 

運行此在日誌下面的崩潰:

05-17 15:18:52.124: E/AndroidRuntime(17266): FATAL EXCEPTION: main 
05-17 15:18:52.124: E/AndroidRuntime(17266): java.lang.RuntimeException: Unable to start activity ComponentInfo{t.e.st/t.e.st.TestActivity}: java.lang.NullPointerException 
05-17 15:18:52.124: E/AndroidRuntime(17266): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1768) 
05-17 15:18:52.124: E/AndroidRuntime(17266): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784) 
05-17 15:18:52.124: E/AndroidRuntime(17266): at android.app.ActivityThread.access$1500(ActivityThread.java:123) 
05-17 15:18:52.124: E/AndroidRuntime(17266): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939) 
05-17 15:18:52.124: E/AndroidRuntime(17266): at android.os.Handler.dispatchMessage(Handler.java:99) 
05-17 15:18:52.124: E/AndroidRuntime(17266): at android.os.Looper.loop(Looper.java:130) 
05-17 15:18:52.124: E/AndroidRuntime(17266): at android.app.ActivityThread.main(ActivityThread.java:3835) 
05-17 15:18:52.124: E/AndroidRuntime(17266): at java.lang.reflect.Method.invokeNative(Native Method) 
05-17 15:18:52.124: E/AndroidRuntime(17266): at java.lang.reflect.Method.invoke(Method.java:507) 
05-17 15:18:52.124: E/AndroidRuntime(17266): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847) 
05-17 15:18:52.124: E/AndroidRuntime(17266): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605) 
05-17 15:18:52.124: E/AndroidRuntime(17266): at dalvik.system.NativeStart.main(Native Method) 
05-17 15:18:52.124: E/AndroidRuntime(17266): Caused by: java.lang.NullPointerException 
**05-17 15:18:52.124: E/AndroidRuntime(17266): at t.e.st.TestActivity.onCreate(TestActivity.java:26)** 
05-17 15:18:52.124: E/AndroidRuntime(17266): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
05-17 15:18:52.124: E/AndroidRuntime(17266): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1722) 
05-17 15:18:52.124: E/AndroidRuntime(17266): ... 11 more 

請幫我解決這個問題。

疊加在xml中。所以這不是問題,它說錯誤是在第26行。所以它的abt addOnGesturePerformedListener();

+0

第26行在TestActivity.java –

+0

請發佈main.xml – MByD

+0

+1,因爲這是一個真實而且很常見的問題。另請參閱@Samir Mangroliya關於修改Toast消息的回答:您需要調用show()來顯示它們! – rekaszeru

回答

3

如果你確信你有一個View@+id = "gestureOverlayView1"main.xml佈局裏面,然後嘗試

gestures.addOnGesturePerformedListener(this); 

,而不是

gestures.addOnGesturePerformedListener(TestActivity.this); 

(從this刪除TestActivity符)

+1

是的,這工作。我沒有羨慕聲望,否則我會投票支持你:( –

+0

如果你覺得它是正確的,你仍然可以接受答案。有人拒絕你的問題,也許是因爲快速閱讀和誤解,不幸的是沒有任何關係: ( – rekaszeru

+0

哈哈,沒有probs ... –