Gesture Android App Development #21

0 rajat agrawal · February 13, 2015

package com.example.rajat.gesture_app;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
import android.view.MotionEvent;
import android.view.GestureDetector;




public class MainActivity extends ActionBarActivity {


    private TextView my_text = (TextView)findViewById(R.id.my_text);
    private GestureDetector my_gesture_detector;
/////// our class for implementing onlistener and on doubletaplistener ///////////////


    public class CustomDetector implements GestureDetector.OnGestureListener , GestureDetector.OnDoubleTapListener
    {
        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {
            my_text.setText("onSingleTapConfirmed");
            return true;
        }

        @Override
        public boolean onDoubleTap(MotionEvent e) {
            my_text.setText("onDoubleTap");
            return true;        }

        @Override
        public boolean onDoubleTapEvent(MotionEvent e) {
            my_text.setText("onDoubleTapEvent");
            return true;        }

        @Override
        public boolean onDown(MotionEvent e) {
            my_text.setText("onDown");
            return true;        }

        @Override
        public void onShowPress(MotionEvent e) {
            my_text.setText("onShowPress");
        }

        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            my_text.setText("onSingleTapUp");
            return true;        }

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            my_text.setText("onScroll");
            return true;        }

        @Override
        public void onLongPress(MotionEvent e) {
            my_text.setText("onLongPress");
        }


        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            my_text.setText("onFling");
            return true;        }
    }


///////////// class ends /////////////////////////////////////////////////


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        CustomDetector my_detector =new CustomDetector();
        my_gesture_detector =new GestureDetector(this,my_detector);   ////// reason for this???
        my_gesture_detector.setOnDoubleTapListener(my_detector);

        my_text.setOnTouchListener(
                new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        my_gesture_detector.onTouchEvent(event);
                        return true;
                    }
                }

        );
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

I am creating a separate class for implementing OnGestureListener ,but on running this app it shows error  "Unfortunately Gesture_app has stopped. " .So please any suggestion ??

Post a Reply

Replies

Oldest  Newest  Rating
+1 rajat agrawal · February 16, 2015
It worked 

package com.example.rajat.gesture_app;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
import android.view.MotionEvent;
import android.view.GestureDetector;




public class MainActivity extends ActionBarActivity {


    private TextView my_text ;
    private GestureDetector my_gesture_detector;
/////// our class for implementing onlistener and on doubletaplistener ///////////////


    public class CustomDetector implements GestureDetector.OnGestureListener , GestureDetector.OnDoubleTapListener
    {
        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {
            my_text.setText("onSingleTapConfirmed");
            return true;
        }

        @Override
        public boolean onDoubleTap(MotionEvent e) {
            my_text.setText("onDoubleTap");
            return true;        }

        @Override
        public boolean onDoubleTapEvent(MotionEvent e) {
            my_text.setText("onDoubleTapEvent");
            return true;        }

        @Override
        public boolean onDown(MotionEvent e) {
            my_text.setText("onDown");
            return true;        }

        @Override
        public void onShowPress(MotionEvent e) {
            my_text.setText("onShowPress");
        }

        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            my_text.setText("onSingleTapUp");
            return true;        }

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            my_text.setText("onScroll");
            return true;        }

        @Override
        public void onLongPress(MotionEvent e) {
            my_text.setText("onLongPress");
        }


        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            my_text.setText("onFling");
            return true;        }
    }


///////////// class ends /////////////////////////////////////////////////


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        my_text= (TextView)findViewById(R.id.my_text);
        CustomDetector my_detector =new CustomDetector();
        my_gesture_detector =new GestureDetector(this,my_detector);   ////// reason for this???
        my_gesture_detector.setOnDoubleTapListener(my_detector);

        my_text.setOnTouchListener(
                new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        my_gesture_detector.onTouchEvent(event);
                        return true;
                    }
                }

        );
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
0 Nicholas Eason · February 13, 2015
Yeah, in Android Studio, check your Logcat for the Stack Trace and post it ;) It's in the Android tab at the bottom.
0 rajat agrawal · February 14, 2015
02-14 14:24:31.736    1945-1945/com.example.rajat.gesture_app I/art? Not late-enabling -Xcheck:jni (already on)
02-14 14:24:33.032    1945-1945/com.example.rajat.gesture_app D/AndroidRuntime? Shutting down VM
    --------- beginning of crash
02-14 14:24:33.033    1945-1945/com.example.rajat.gesture_app E/AndroidRuntime? FATAL EXCEPTION: main
    Process: com.example.rajat.gesture_app, PID: 1945
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.rajat.gesture_app/com.example.rajat.gesture_app.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
            at android.app.Activity.findViewById(Activity.java:2071)
            at com.example.rajat.gesture_app.MainActivity.<init>(MainActivity.java:20)
            at java.lang.reflect.Constructor.newInstance(Native Method)
            at java.lang.Class.newInstance(Class.java:1572)
            at android.app.Instrumentation.newActivity(Instrumentation.java:1065)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2199)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

Thanx for help :D
I m a beginner in android 
0 Paramjeet SIngh Nigam · June 17, 2015
How did that work? 
Even I am getting the same error in my device.
0 rajat agrawal · February 16, 2015
Please anyone can find the mistake.
  • 1

Java / Android Development

107,237 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator