Note that there are some explanatory texts on larger screens.

plurals
  1. PONull Pointer Exception when assigned a View to an Activity?
    primarykey
    data
    text
    <p>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:</p> <pre><code>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) </code></pre> <p>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. </p> <p>How would I properly assign the 'gesture' view to this activity to prevent the errors?</p> <p>I do hope someone knows the answer and can help me!! Many thanks</p> <p>I have defined my activity in my code like this:</p> <pre><code>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){ ...} } </code></pre> <p>I have created the activity in the main thread in it's doStart() method by saying:</p> <pre><code>GestureActivity mGestureActivity = new GestureActivity(); mGestureActivity.onCreate(null); </code></pre> <p>And my XML layout looks like this:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" &gt; &lt;com.darius.android.distractions.DistractionsView android:id="@+id/distractions_layout" android:layout_width="match_parent" android:layout_height="match_parent"/&gt; &lt;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"/&gt; &lt;RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" &gt; &lt;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"/&gt; &lt;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" &gt;&lt;/TextView&gt; &lt;/RelativeLayout&gt; </code></pre> <p></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload