Flatlyn
Flatlyn

Reputation: 2050

Android Crashing After Adding Gesture Support

My app was running fine, albiet with no functionality, but since I added support for gestures (code adapted from a tutorial) it now just crashes on running.

package com.mystraldesign.memorable;

import android.app.Activity;
//import android.gesture.GestureOverlayView;
import android.gesture.GestureOverlayView;
import android.gesture.GestureOverlayView.OnGestureListener;
import android.graphics.Typeface;
import android.os.Bundle;
//import android.widget.RelativeLayout;
import android.view.GestureDetector;
import android.view.GestureDetector.OnDoubleTapListener;
import android.view.MotionEvent;
import android.view.Window;
import android.widget.TextView;

public class MemorableActivity extends Activity implements OnGestureListener,OnDoubleTapListener 
{
    //Define text views
    private TextView textView1;
    private TextView textView2;
    private TextView textView3;
    private TextView textView4;

    //Gesture Detectors
    private GestureDetector gTap; 

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        gTap  = new GestureDetector(this,(android.view.GestureDetector.OnGestureListener) this);

        //Remove title bar
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);




        //Define textView
        textView1 = (TextView) findViewById(R.id.textView1);
        textView2 = (TextView) findViewById(R.id.textView2);
        textView3 = (TextView) findViewById(R.id.textView3);
        textView4 = (TextView) findViewById(R.id.textView4);

        //Load font file
        Typeface type = Typeface.createFromAsset(getAssets(),"fonts/optima.ttf"); 

        //Set various textViews to font
        textView1.setTypeface(type);
        textView2.setTypeface(type);
        textView3.setTypeface(type);
        textView4.setTypeface(type);


    }



    public boolean onTouchEvent(MotionEvent me){ 
      this.gTap.onTouchEvent(me);
     return super.onTouchEvent(me); 
    }

    public boolean onDown(MotionEvent e) {
      textView1.setText("Down");  
      return false;
    }

    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
      float velocityY) {
        textView1.setText("Fling");
      return false;
    }


    public void onLongPress(MotionEvent e) {
        textView1.setText("Long Press"); 
    }


    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
      float distanceY) {
        textView1.setText("Scroll");
     return false;
    }


    public void onShowPress(MotionEvent e) {
        textView1.setText("Show Press");
    }

    public boolean onSingleTapUp(MotionEvent e) {
        textView1.setText("Sing Tap Up");
     return false;
    }

    public boolean onDoubleTap(MotionEvent e) {
        textView1.setText("Double Tap");
     return false;
    }

    public boolean onDoubleTapEvent(MotionEvent e) {
        textView1.setText("Double Tap Event");
      return false;
    }

    public boolean onSingleTapConfirmed(MotionEvent e) {
        textView1.setText("Single Tap Confirmed");
     return false;
    }




}

I removed a @override statment from the start of each of the methods as Eclipse was stating an Error with them and gave the auto-fix to be remove the @override statements.

It also gave an error on the public class MemorableActivity extends Activity implements OnGestureListener,OnDoubleTapListener line which is:

Multiple markers at this line
    - The type MemorableActivity must implement the inherited abstract method 
     GestureOverlayView.OnGestureListener.onGestureEnded(GestureOverlayView, MotionEvent)
    - The type MemorableActivity must implement the inherited abstract method 
     GestureOverlayView.OnGestureListener.onGestureCancelled(GestureOverlayView, MotionEvent)
    - The type MemorableActivity must implement the inherited abstract method 
     GestureOverlayView.OnGestureListener.onGestureStarted(GestureOverlayView, MotionEvent)
    - The type MemorableActivity must implement the inherited abstract method 
     GestureOverlayView.OnGestureListener.onGesture(GestureOverlayView, MotionEvent)

It suggested the fixes of

Add unimplemented methods
Make MemorableActivity abstract
Rename in file
Rename in workspace

I tried the first two but it still crashes and I'm not sure what the second and third one are/do.

The doesn't seem to be anything in the log but here is the output from console

[2012-04-30 03:54:37 - Memorable] ------------------------------
[2012-04-30 03:54:37 - Memorable] Android Launch!
[2012-04-30 03:54:37 - Memorable] adb is running normally.
[2012-04-30 03:54:38 - Memorable] Performing com.mystraldesign.memorable.MemorableActivity activity launch
[2012-04-30 03:54:38 - Memorable] Automatic Target Mode: launching new emulator with compatible AVD 'Memorable'
[2012-04-30 03:54:38 - Memorable] Launching a new emulator with Virtual Device 'Memorable'
[2012-04-30 03:54:44 - Emulator] 2012-04-30 03:54:44.155 emulator-arm[2796:1107] Warning once: This application, or a library it uses, is using NSQuickDrawView, which has been deprecated. Apps should cease use of QuickDraw and move to Quartz.
[2012-04-30 03:54:44 - Memorable] New emulator found: emulator-5554
[2012-04-30 03:54:44 - Memorable] Waiting for HOME ('android.process.acore') to be launched...
[2012-04-30 03:55:36 - Memorable] HOME is up on device 'emulator-5554'
[2012-04-30 03:55:36 - Memorable] Uploading Memorable.apk onto device 'emulator-5554'
[2012-04-30 03:55:41 - Memorable] Installing Memorable.apk...
[2012-04-30 03:55:57 - Memorable] Success!
[2012-04-30 03:55:57 - Memorable] Starting activity com.mystraldesign.memorable.MemorableActivity on device emulator-5554
[2012-04-30 03:56:00 - Memorable] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.mystraldesign.memorable/.MemorableActivity }

EDIT:

Added log from Eclipse

04-30 05:43:25.339: D/AndroidRuntime(333): Shutting down VM
04-30 05:43:25.339: W/dalvikvm(333): threadid=1: thread exiting with uncaught exception (group=0x40015560)
04-30 05:43:25.369: E/AndroidRuntime(333): FATAL EXCEPTION: main
04-30 05:43:25.369: E/AndroidRuntime(333): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mystraldesign.memorable/com.mystraldesign.memorable.MemorableActivity}: java.lang.ClassCastException: com.mystraldesign.memorable.MemorableActivity
04-30 05:43:25.369: E/AndroidRuntime(333):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
04-30 05:43:25.369: E/AndroidRuntime(333):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
04-30 05:43:25.369: E/AndroidRuntime(333):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-30 05:43:25.369: E/AndroidRuntime(333):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
04-30 05:43:25.369: E/AndroidRuntime(333):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-30 05:43:25.369: E/AndroidRuntime(333):  at android.os.Looper.loop(Looper.java:123)
04-30 05:43:25.369: E/AndroidRuntime(333):  at android.app.ActivityThread.main(ActivityThread.java:3683)
04-30 05:43:25.369: E/AndroidRuntime(333):  at java.lang.reflect.Method.invokeNative(Native Method)
04-30 05:43:25.369: E/AndroidRuntime(333):  at java.lang.reflect.Method.invoke(Method.java:507)
04-30 05:43:25.369: E/AndroidRuntime(333):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-30 05:43:25.369: E/AndroidRuntime(333):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-30 05:43:25.369: E/AndroidRuntime(333):  at dalvik.system.NativeStart.main(Native Method)
04-30 05:43:25.369: E/AndroidRuntime(333): Caused by: java.lang.ClassCastException: com.mystraldesign.memorable.MemorableActivity
04-30 05:43:25.369: E/AndroidRuntime(333):  at com.mystraldesign.memorable.MemorableActivity.onCreate(MemorableActivity.java:33)
04-30 05:43:25.369: E/AndroidRuntime(333):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-30 05:43:25.369: E/AndroidRuntime(333):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
04-30 05:43:25.369: E/AndroidRuntime(333):  ... 11 more

Upvotes: 0

Views: 1651

Answers (1)

Femi
Femi

Reputation: 64700

You are implementing one type of OnGestureListener but casting to another. Change your Activity class statement to this:

public class MemorableActivity extends Activity implements android.view.GestureDetector.OnGestureListener,OnDoubleTapListener 

and then implement the necessary methods and your crash should stop happening.

Upvotes: 1

Related Questions