Android轻扫手势无效

时间:2013-03-15 11:29:27

标签: android swipe gesture galaxy

在我的应用中,滑动手势的代码在模拟器上正常工作。但是当我在物理设备(Galaxy选项卡)上测试时,相同的代码崩溃了。我正在分享一些代码和日志猫的结果。任何帮助表示赞赏。

这是我的活动中实现滑动的类。

    package com.example.testflashfile;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import android.view.MotionEvent;
import com.example.testflashfile.GlobalVariables;


public class MainActivity extends Activity
{
    Button nextButton;
    Button playButton;
    Button backButton;
    Context contextObject;
    GestureDetector gestureDetector;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
        setContentView(R.layout.activity_main);

        ReadText readText=new ReadText(this);
        TextView helloTxt = (TextView)findViewById(R.id.displaytext);

        String fileName="textone";
        helloTxt.setText(readText.readTxt(fileName));

        gestureDetector = new GestureDetector(this.getApplicationContext(),new MyGestureDetector());
        View mainview = (View) findViewById(R.id.mainView);

        // Set the touch listener for the main view to be our custom gesture listener
        mainview.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                if (gestureDetector.onTouchEvent(event)) {
                    return true;
                }
                return false;
            }
        });


        playButton=(Button)findViewById(R.id.play);
        playButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent startAnimation=new Intent(MainActivity.this,PlayAnimationActivity.class);
                startAnimation.putExtra("SWF_NAME","a");
                startActivity(startAnimation);
            }
        });



        nextButton=(Button)findViewById(R.id.next);
        nextButton.setOnClickListener(new OnClickListener() {


            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent=new Intent(MainActivity.this,SecondActivity.class);
                startActivity(intent);

            }
        });

        backButton=(Button)findViewById(R.id.back);
        backButton.setOnClickListener(new OnClickListener() {


            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent=new Intent(MainActivity.this,FifthActivity.class);
                startActivity(intent);

            }
        });


    }

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


    public class MyGestureDetector extends SimpleOnGestureListener
    {


        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {


            if (Math.abs(e1.getY() - e2.getY()) > GlobalVariables.SWIPE_MAX_OFF_PATH) {
                return false;
            }

            // right to left swipe
            if(e1.getX() - e2.getX() > GlobalVariables.SWIPE_MIN_DISTANCE && Math.abs(velocityX) > GlobalVariables.SWIPE_THRESHOLD_VELOCITY) {

                Intent i= new Intent(MainActivity.this,SecondActivity.class);
                startActivity(i);

                //  left to right  swipe
            }  else if (e2.getX() - e1.getX() > GlobalVariables.SWIPE_MIN_DISTANCE && Math.abs(velocityX) > GlobalVariables.SWIPE_THRESHOLD_VELOCITY) {


                Intent i= new Intent(MainActivity.this,FifthActivity.class);
                startActivity(i);

            }

            return false;
        }


        @Override
        public boolean onDown(MotionEvent e) {
            return true;
        }



    }

}

log cat结果是:

03-15 11:25:39.369: D/OpenGLRenderer(10179): Enabling debug mode 0
03-15 11:25:42.611: E/InputEventReceiver(10179): Exception dispatching input event.
03-15 11:25:42.611: D/AndroidRuntime(10179): Shutting down VM
03-15 11:25:42.611: W/dalvikvm(10179): threadid=1: thread exiting with uncaught exception (group=0x419442a0)
03-15 11:25:42.619: E/AndroidRuntime(10179): FATAL EXCEPTION: main
03-15 11:25:42.619: E/AndroidRuntime(10179): java.lang.IllegalArgumentException: pointerIndex out of range
03-15 11:25:42.619: E/AndroidRuntime(10179):    at android.view.MotionEvent.nativeGetAxisValue(Native Method)
03-15 11:25:42.619: E/AndroidRuntime(10179):    at android.view.MotionEvent.getY(MotionEvent.java:2090)
03-15 11:25:42.619: E/AndroidRuntime(10179):    at android.widget.ScrollView.onTouchEvent(ScrollView.java:848)
03-15 11:25:42.619: E/AndroidRuntime(10179):    at android.view.View.dispatchTouchEvent(View.java:7340)
03-15 11:25:42.619: E/AndroidRuntime(10179):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2181)
03-15 11:25:42.619: E/AndroidRuntime(10179):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1914)
03-15 11:25:42.619: E/AndroidRuntime(10179):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2187)
03-15 11:25:42.619: E/AndroidRuntime(10179):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1929)
03-15 11:25:42.619: E/AndroidRuntime(10179):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2187)
03-15 11:25:42.619: E/AndroidRuntime(10179):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1929)
03-15 11:25:42.619: E/AndroidRuntime(10179):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2187)
03-15 11:25:42.619: E/AndroidRuntime(10179):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1929)
03-15 11:25:42.619: E/AndroidRuntime(10179):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2187)
03-15 11:25:42.619: E/AndroidRuntime(10179):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1929)
03-15 11:25:42.619: E/AndroidRuntime(10179):    at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2113)
03-15 11:25:42.619: E/AndroidRuntime(10179):    at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1466)
03-15 11:25:42.619: E/AndroidRuntime(10179):    at android.app.Activity.dispatchTouchEvent(Activity.java:2468)
03-15 11:25:42.619: E/AndroidRuntime(10179):    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2061)
03-15 11:25:42.619: E/AndroidRuntime(10179):    at android.view.View.dispatchPointerEvent(View.java:7525)
03-15 11:25:42.619: E/AndroidRuntime(10179):    at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3370)
03-15 11:25:42.619: E/AndroidRuntime(10179):    at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3302)
03-15 11:25:42.619: E/AndroidRuntime(10179):    at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4394)
03-15 11:25:42.619: E/AndroidRuntime(10179):    at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4372)
03-15 11:25:42.619: E/AndroidRuntime(10179):    at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4476)
03-15 11:25:42.619: E/AndroidRuntime(10179):    at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:171)
03-15 11:25:42.619: E/AndroidRuntime(10179):    at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
03-15 11:25:42.619: E/AndroidRuntime(10179):    at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:163)
03-15 11:25:42.619: E/AndroidRuntime(10179):    at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:4444)
03-15 11:25:42.619: E/AndroidRuntime(10179):    at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:4495)
03-15 11:25:42.619: E/AndroidRuntime(10179):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
03-15 11:25:42.619: E/AndroidRuntime(10179):    at android.view.Choreographer.doCallbacks(Choreographer.java:555)
03-15 11:25:42.619: E/AndroidRuntime(10179):    at android.view.Choreographer.doFrame(Choreographer.java:523)
03-15 11:25:42.619: E/AndroidRuntime(10179):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
03-15 11:25:42.619: E/AndroidRuntime(10179):    at android.os.Handler.handleCallback(Handler.java:615)
03-15 11:25:42.619: E/AndroidRuntime(10179):    at android.os.Handler.dispatchMessage(Handler.java:92)
03-15 11:25:42.619: E/AndroidRuntime(10179):    at android.os.Looper.loop(Looper.java:137)
03-15 11:25:42.619: E/AndroidRuntime(10179):    at android.app.ActivityThread.main(ActivityThread.java:4895)
03-15 11:25:42.619: E/AndroidRuntime(10179):    at java.lang.reflect.Method.invokeNative(Native Method)
03-15 11:25:42.619: E/AndroidRuntime(10179):    at java.lang.reflect.Method.invoke(Method.java:511)
03-15 11:25:42.619: E/AndroidRuntime(10179):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
03-15 11:25:42.619: E/AndroidRuntime(10179):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
03-15 11:25:42.619: E/AndroidRuntime(10179):    at dalvik.system.NativeStart.main(Native Method)

3 个答案:

答案 0 :(得分:3)

mainview.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                if (gestureDetector.onTouchEvent(event)) {
                    return false;
                }
                return true;
            }
        });

这是正确的做法。它现在可以在设备上运行。但是我仍然不明白它是如何在模拟器上工作的。如果任何人都能解决这个问题请告诉我。 感谢。

答案 1 :(得分:0)

您将null作为输入传递给Read类中的readTxt(null);,而是在构造函数中发送文件名,并将文件名传递给readTxt,或者删除readTxt(null)阅读课程

答案 2 :(得分:0)

问题出在这里

  public class ReadText {
    Context context;
    public ReadText(Context c) {
        context = c;
        readTxt(null);
    }

null是java中的一个关键字..所以你需要首先更改你的assts文件夹(null.txt)的文件名,然后在你的代码中使用该文件名......