I followed the tutorial here: http://developer.samsung.com/android/technical-docs/Gestures-in-Android It works if I do it in a separate project, but, when I try to implement it in another project, like this:
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();
}
}
}
The mView.addOnGesturePerformedListener(this); returns a nullPointerException. Even if I try to pass (Main.this), which is the solution for this similar question 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>
What do you think is the problem? thanks in advance.
Edit: It doesn't work if i pass a listener to mView.addOnGesturePerformedListener(this):
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);
}
}
You can use findViewById method in activity only if you have setContentView() which defines layout for that activity. In your particular case you do not call setContentView therefore
(GestureOverlayView) findViewById(R.id.gestures)
will return null and cause NullpointerException when calling any method on mView (in your case mView.addOnGesturePerformedListener(this);)