Hi there
I have built an activity to handle gestures in my Android game and want it to respond to a gesture anywhere on the screen but I am getting this error on the Log:
07-27 13:55:07.268: ERROR/AndroidRuntime(751): java.lang.NullPointerException
07-27 13:55:07.268: ERROR/AndroidRuntime(751): at android.app.Activity.findViewById(Activity.java:1637)
07-27 13:55:07.268: ERROR/AndroidRuntime(751): at com.darius.android.distractions.DistractionsView$DistractionsThread$GestureActivity.onCreate(DistractionsView.java:282)
07-27 13:55:07.268: ERROR/AndroidRuntime(751): at com.darius.android.distractions.DistractionsView$DistractionsThread.doStart(DistractionsView.java:559)
07-27 13:55:07.268: ERROR/AndroidRuntime(751): at com.darius.android.distractions.DistractionsView$DistractionsThread.doKeyDown(DistractionsView.java:829)
07-27 13:55:07.268: ERROR/AndroidRuntime(751): at com.darius.android.distractions.DistractionsView.onKeyDown(DistractionsView.java:1278)
I understand I need to allocate a view to the activity and as you will see in my XML, a view called gestures will serve this purpose but I keep getting Inflation errors when trying to inflate the view. Before when this happened to me it was a misspelling of the view name in the XML but that's not it this time I don't think.
How would I properly assign the 'gesture' view to this activity to prevent the errors?
I do hope someone knows the answer and can help me!! Many thanks
I have defined my activity in my code like this:
public class GestureActivity extends Activity implements GestureOverlayView.OnGesturePerformedListener{
public void onCreate(Bundle savedInstanceState){
GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gestures);
gestures.addOnGesturePerformedListener(this);
Log.w(getClass().getName(), "GESTURE ACTIVITY CREATED");
// Load the gesture library
Log.w(getClass().getName(), "LOADING GESTURE LIBRARY");
mLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);
if(!mLibrary.load()){
finish();
}
/*
GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gestures);
gestures.addOnGesturePerformedListener(this);
Log.w(getClass().getName(), "GESTURE ACTIVITY CREATED");
*/
}
public void onStart(){
//I replicated the code here from onCreate but will change this!!
GestureOverlayView gestures = (GestureOverlayView) findViewById (R.id.gestures);
gestures.addOnGesturePerformedListener(this);
Log.w(getClass().getName(), "GESTURE ACTIVITY CREATED");
// Load the gesture library
Log.w(getClass().getName(), "LOADING GESTURE LIBRARY");
mLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);
if(!mLibrary.load()){
finish();
}
}
public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture){ ...}
}
I have created the activity in the main thread in it's doStart() method by saying:
GestureActivity mGestureActivity = new GestureActivity();
mGestureActivity.onCreate(null);
And my XML layout looks like this:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<com.darius.android.distractions.DistractionsView
android:id="@+id/distractions_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<android.gesture.GestureOverlayView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gestures"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gestureStrokeType="multiple"
android:eventsInterceptionEnabled="true"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/text"
android:text="Hello"
android:visibility="visible"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center_horizontal"
android:textColor="#88ffffff"
android:textSize="24sp"/>
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/concentration_bar"
android:textColor = "#EB0000"
android:textSize="10sp"
android:id="@+id/conbartext"
android:visibility="visible"
></TextView>
</RelativeLayout>