2013-10-11 77 views
1

我跟着這裏的教程:http://developer.samsung.com/android/technical-docs/Gestures-in-Android 如果我在一個單獨的項目做它的工作原理,但是,當我嘗試實現它在另一個項目中,像這樣:的Android addOnGesturePerformedListener返回NullPointerException異常

public class Main extends Activity implements OnGesturePerformedListener { 
    int mDay = Calendar.getInstance().get(Calendar.DAY_OF_MONTH); 
    int mMonth = Calendar.getInstance().get(Calendar.MONTH); 
    int mYear = Calendar.getInstance().get(Calendar.YEAR); 
    boolean listHasItems = false; 
    public static String date = ""; 
    ArrayList<String> listNames = new ArrayList<String>(); 
    List<ActivityToDo> acts = System.activities; 
    GestureLibrary gLibrary; 
    GestureOverlayView mView; 

     @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     // Remove title bar 
     this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
     removeActivitesBeforeCurrentDate(); 
     readDB(); 

     gLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures); 
     if (gLibrary != null) { 
      if (!gLibrary.load()) { 
       Log.e("GestureSample", "Gesture library was not loaded…"); 
       finish(); 
      } else { 
       mView = (GestureOverlayView) findViewById(R.id.gestures); 
       mView.addOnGesturePerformedListener(this); 
      } 
     } 

     setContentView(R.layout.main); 
     final Button btnNewAct = (Button) findViewById(R.id.btnNewAct); 

     final DatePicker datep = (DatePicker) findViewById(R.id.datePicker); 
     datep.init(mYear, mMonth, mDay, new OnDateChangedListener() { 
      @Override 
      public void onDateChanged(DatePicker view, int year, 
        int monthOfYear, int dayOfMonth) { 
       mDay = datep.getDayOfMonth(); 
       mMonth = datep.getMonth(); 
       mYear = datep.getYear(); 
       buildList(mDay, mMonth + 1, mYear); 
      } 
     }); 

     btnNewAct.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Intent i = new Intent(); 
       i.setClass(getApplicationContext(), NewActivity.class); 
       startActivity(i); 
      } 
     }); 

     final ListView listview = (ListView) findViewById(R.id.listview); 

     // listNames.add("Pick date to search activity."); 
     buildList(mDay, mMonth + 1, mYear); 
     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
       android.R.layout.simple_list_item_1, listNames); 
     listview.setAdapter(adapter); 
     registerForContextMenu(listview); 

    } 

    @Override 
    public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) { 
     ArrayList<Prediction> predictions = gLibrary.recognize(gesture); 
     // one prediction needed 
     if (predictions.size() > 0) { 
      Prediction prediction = predictions.get(0); 
      // checking prediction 
      if (prediction.score > 1.0) { 
       // and action 
       Toast.makeText(this, prediction.name,Toast.LENGTH_SHORT).show(); 
      } 
     }  
    } 

的MVIEW .addOnGesturePerformedListener(本);返回一個nullPointerException。即使我嘗試通過(Main.this),這是類似問題Android: Nullpointer Exception in addOnGesturePerformedListener的解決方案。

XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="center_horizontal" 
    android:orientation="vertical" 
    tools:context=".Main" > 

<android.gesture.GestureOverlayView 
    android:id="@+id/gestures" 
    android:layout_width="50dp" 
    android:layout_height="0dip" 
    android:layout_weight="0.5" /> 

<ListView 
    android:id="@+id/listview" 
    android:layout_width="250dp" 
    android:layout_height="200dp" 
    android:layout_weight="0.5" 
    android:background="@drawable/spinnerst" > 
</ListView> 
</LinearLayout> 

你認爲是什麼問題?提前致謝。

編輯: 如果我通過一個監聽器mView.addOnGesturePerformedListener它不工作,(這):

OnGesturePerformedListener listener=new OnGesturePerformedListener() { 
     @Override 
     public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) { 
      ArrayList<Prediction> predictions = gLibrary.recognize(gesture); 
      // one prediction needed 
      if (predictions.size() > 0) { 
       Prediction prediction = predictions.get(0); 
       // checking prediction 
       if (prediction.score > 1.0) { 
        // and action 
        mostrarToast(prediction.name); 
       } 
      } 
     } 
    }; 
    gLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures); 
    if (gLibrary != null) { 
     if (!gLibrary.load()) { 
      Log.e("GestureSample", "Gesture library was not loaded…"); 
      finish(); 
     } else { 
      mView = (GestureOverlayView) findViewById(R.id.gestures); 
      mView.addOnGesturePerformedListener(listener); 
     } 
    } 
+0

後堆棧跟蹤 – njzk2

回答

0

您可以在活動中使用findViewById方法只有當你有的setContentView()定義佈局那個活動。 在您的特定情況下,你不叫的setContentView因此

(GestureOverlayView) findViewById(R.id.gestures)

將返回null,並呼籲MView的任何方法時,會導致空指針異常(你的情況mView.addOnGesturePerformedListener(本);)

+0

超級愚蠢的錯誤:P非常感謝! – RominaV

相關問題