在另一个类中实现函数

时间:2014-12-14 22:58:24

标签: android class implementation gesture

我在创建此功能时遇到问题:enter(linearLayout, llp)。我在班级Game中创建了它,但我正在尝试在班级MyTouchListener中实现它。

正如您在下面看到的,我将课程MyTouchListener扩展为课程Game,以便它可以获得该课程的功能,包括enter(linearLayout, llp)

目前enter(linearLayout, llp)位于课程onFling的{​​{1}}方法中,但不起作用。当我在屏幕上翻动时,我的应用程序崩溃了。

有人可以告诉我这里有什么问题并提供解决方案吗?

班级MyTouchListener

MyTouchListener

班级public class MyTouchListener extends Game implements OnTouchListener{ protected static final String TAG = "MyTouchListener"; protected static final boolean D = true; public interface MyListener{ public void onTouch(); } private MyListener mListener; private GestureDetector mGestureDetector; private class MyClickListener extends SimpleOnGestureListener{ @Override public boolean onDown(MotionEvent e) { return true; } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { if (D){ enter(linearLayout, llp); //ERROR HERE Log.d(TAG, "2 - onFling event"); } return true; } } public MyTouchListener(Context context, MyListener listener){ mGestureDetector = new GestureDetector(context, new MyClickListener()); mListener = listener; } @Override public boolean onTouch(View v, MotionEvent event) { if (D){ Log.d(TAG, "2 - onTouch event"); } mListener.onTouch(); mGestureDetector.onTouchEvent(event); return false; } }

Game

这是错误日志。一旦我穿过屏幕,就会发生这种情况:

public class Game extends Activity   {

    protected static LinearLayout linearLayout;
    protected static LinearLayout.LayoutParams llp; 

    public void enter(String[] w, LinearLayout ll, LinearLayout.LayoutParams params){

    ...//code
    ...//code
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.game_activity);

        mListener = new MyListener() {

            @Override
            public void onTouch() {
                if (D){
                    Log.d(TAG, "2 - onTouch event");
                }
            }
        };

        for(Button b:button_list){
            b.setOnTouchListener(new MyTouchListener(getApplicationContext(), mListener));
        }

        linearLayout = (LinearLayout) findViewById(R.id.ll_bttn_words);

        llp = new LinearLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);

        llp.gravity = Gravity.TOP;

    }

我尝试了很多东西,包括为我的12-14 20:09:28.342: D/MyTouchListener(18212): 2 - onTouch event 12-14 20:09:28.342: D/MyTouchListener(18212): 2 - onTouch event 12-14 20:09:28.342: E/InputEventReceiver(18212): Exception dispatching input event. 12-14 20:09:28.342: E/MessageQueue-JNI(18212): at android.view.GestureDetector.onTouchEvent(GestureDetector.java:1405) 12-14 20:09:28.342: E/MessageQueue-JNI(18212): at android.view.View.dispatchTouchEvent(View.java:7456) 12-14 20:09:28.342: E/MessageQueue-JNI(18212): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2270) 12-14 20:09:28.342: E/MessageQueue-JNI(18212): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2010) 12-14 20:09:28.342: E/MessageQueue-JNI(18212): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2270) 12-14 20:09:28.342: E/MessageQueue-JNI(18212): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2010) 12-14 20:09:28.342: E/MessageQueue-JNI(18212): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2270) 12-14 20:09:28.342: E/MessageQueue-JNI(18212): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2010) 12-14 20:09:28.342: E/MessageQueue-JNI(18212): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2270) 12-14 20:09:28.342: E/MessageQueue-JNI(18212): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2010) 12-14 20:09:28.342: E/MessageQueue-JNI(18212): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2270) 12-14 20:09:28.342: E/MessageQueue-JNI(18212): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2010) 12-14 20:09:28.342: E/MessageQueue-JNI(18212): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2270) 12-14 20:09:28.342: E/MessageQueue-JNI(18212): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2010) 12-14 20:09:28.342: E/MessageQueue-JNI(18212): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2186) 12-14 20:09:28.342: E/MessageQueue-JNI(18212): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1516) 12-14 20:09:28.342: E/MessageQueue-JNI(18212): at android.app.Activity.dispatchTouchEvent(Activity.java:2466) 12-14 20:09:28.342: E/MessageQueue-JNI(18212): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2134) 12-14 20:09:28.342: E/MessageQueue-JNI(18212): at android.view.View.dispatchPointerEvent(View.java:7641) 12-14 20:09:28.342: E/MessageQueue-JNI(18212): at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3676) 12-14 20:09:28.342: E/MessageQueue-JNI(18212): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3607) 12-14 20:09:28.342: E/MessageQueue-JNI(18212): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4802) 12-14 20:09:28.342: E/MessageQueue-JNI(18212): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4781) 12-14 20:09:28.342: E/MessageQueue-JNI(18212): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4873) 12-14 20:09:28.342: E/MessageQueue-JNI(18212): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185) 12-14 20:09:28.352: E/AndroidRuntime(18212): at android.view.GestureDetector.onTouchEvent(GestureDetector.java:1405) 12-14 20:09:28.352: E/AndroidRuntime(18212): at android.view.View.dispatchTouchEvent(View.java:7456) 12-14 20:09:28.352: E/AndroidRuntime(18212): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2270) 12-14 20:09:28.352: E/AndroidRuntime(18212): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2010) 12-14 20:09:28.352: E/AndroidRuntime(18212): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2270) 12-14 20:09:28.352: E/AndroidRuntime(18212): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2010) 12-14 20:09:28.352: E/AndroidRuntime(18212): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2270) 12-14 20:09:28.352: E/AndroidRuntime(18212): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2010) 12-14 20:09:28.352: E/AndroidRuntime(18212): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2270) 12-14 20:09:28.352: E/AndroidRuntime(18212): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2010) 12-14 20:09:28.352: E/AndroidRuntime(18212): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2270) 12-14 20:09:28.352: E/AndroidRuntime(18212): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2010) 12-14 20:09:28.352: E/AndroidRuntime(18212): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2270) 12-14 20:09:28.352: E/AndroidRuntime(18212): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2010) 12-14 20:09:28.352: E/AndroidRuntime(18212): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2186) 12-14 20:09:28.352: E/AndroidRuntime(18212): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1516) 12-14 20:09:28.352: E/AndroidRuntime(18212): at android.app.Activity.dispatchTouchEvent(Activity.java:2466) 12-14 20:09:28.352: E/AndroidRuntime(18212): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2134) 12-14 20:09:28.352: E/AndroidRuntime(18212): at android.view.View.dispatchPointerEvent(View.java:7641) 12-14 20:09:28.352: E/AndroidRuntime(18212): at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3676) 12-14 20:09:28.352: E/AndroidRuntime(18212): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3607) 12-14 20:09:28.352: E/AndroidRuntime(18212): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4802) 12-14 20:09:28.352: E/AndroidRuntime(18212): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4781) 12-14 20:09:28.352: E/AndroidRuntime(18212): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4873) 12-14 20:09:28.352: E/AndroidRuntime(18212): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185) 类创建构造函数,并将它应用到我的其他类,但是没有用。所以我认为最好的策略是让类Game扩展类MyTouchListener并使全局变量成为静态。

0 个答案:

没有答案