views:

93

answers:

1

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>

A: 

findViewById() requires either the use of setContentView() or to be called on a View. so I would advise you to, either called setContentView(R.layout.nameOfYourXmlfile) or inflate it with a Layout inflater.

Sephy
Thanks, so if I change it to: public void onCreate(Bundle savedInstanceState){ setContentView(R.layout.main); How would I go about attaching the listener which I previously did using this code: GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gestures); gestures.addOnGesturePerformedListener(this);
Greenhouse Gases
you should be able to do it just like that, as the so called id is present in the xml you have in `setContentView`
Sephy
Yeah I hoped that would work but I've noticed this line keeps popping up when it builds and then sure enough the nullpointerexception follows when I attempt interaction with the program: 07-27 14:58:06.048: WARN/InputManagerService(65): Starting input on non-focused client com.android.internal.view.IInputMethodClient$Stub$Proxy@43ea09c0 (uid=10000 pid=122)
Greenhouse Gases
why do you instantiate the activity in onStart? Isn't this done by the onCreate method already? I tried to do this 2 lines : GestureActivity mGestureActivity = new GestureActivity();mGestureActivity.onCreate(null); but I get a null pointer exception... Try removing them?
Sephy
There is a DistractionsView class that creates a DistractionsThread to run all the physics and drawing to the canvas, but in the DistractionsThread doStart() method I put those 2 lines you mentioned as I dont know where else to create and start the GestureActivity. I have defined the class GestureActivity as an inner class in the DistractionsThread class to avoid access issues to private variables. could this be the reason for the error? I also want to thank you for spending this time helping an android newbie!
Greenhouse Gases
No problem for helping. Actually, right now, I'm mostly blunderring around to understand what's wrong.However, I was under the impression that an Activity is started with an Intent no?
Sephy
OK just to update you I have it compiling without those nasty errors. You're quite right when you say the activity has to be put in the Android Manifest, however it didnt like the fact it was an inner-activity, so I literally built a whole new file and transported my class to that new file, updated the manifest and intent and now it works.However the GestureOverlayView is obstructing the program at the moment by displaying over the top whilst nothing carries on but I think this is to do with where I've started it from within the main thread so the main thread just hangs behind it.
Greenhouse Gases
I'm lost with your threads... UIthread is the main thread, in which you create Activities. An Activity generally supports a screen or part of it, and if you have lenghty work you use background threads to avoid making the UI slugish.
Sephy

related questions