0

何らかの理由で、ユーザーが左から右に飛んでいくと、アプリが初めてクラッシュします。しかし、彼らが右から左へのフリングを行うと、うまく機能します。これは、私が構築した gridView でのみ行われます。私のレイアウトは基本的に

親: LinearLayout

子: GridView

画面全体にgestureListenerを実装しようとしていますが、gridViewが入っているlinearLayoutにgestureListenerを実装すると登録されません。だから私はそれらを別々にやらなければなりません。これを行うと、gridView の firstAction ジェスチャリスナーで null ポインターが取得されます。コードは次のとおりです。

//set swipe listener for the calendar view
        calendar.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View arg0, MotionEvent event) {
                // TODO Auto-generated method stub
                return gestures.onTouchEvent(event);
            }
        });

        //set swipe listener for rest of the view
        mainView.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View arg0, MotionEvent event) {
                // TODO Auto-generated method stub
                return gestures.onTouchEvent(event);
            }
        });

            @Override
        public boolean onFling(MotionEvent firstAction, MotionEvent secondAction, float velocityX,
                float velocityY) {
            //change the calendar images based on what the user flings. If they fling in between certain
            //months, change the background.

            Toast.makeText(CalendarDisplay.this, Float.toString(firstAction.getX()) + " : "
                    + Float.toString(secondAction.getX()), Toast.LENGTH_SHORT).show();
            //right to left fling
            if (firstAction.getX() - secondAction.getX() > 120 && Math.abs(velocityX) > 200) {
                mainView.setBackgroundDrawable(getResources().getDrawable(R.drawable.spring_bkgr));
                month -= 1;
                if (month < 0) {
                    month = 11;
                    year -= 1;
                    myCal.set(Calendar.YEAR, year);
                    myCal.set(Calendar.MONTH, month);
                    adapter = new CalendarAdapter(CalendarDisplay.this, year, month, currentDay);
                    calendar.setAdapter(adapter);
                    currentDate.setText(DateFormat.format(dateTemplate, myCal.getTimeInMillis()));
                }
                //sets the new month and resets the adapter
                myCal.set(Calendar.MONTH, month);
                adapter = new CalendarAdapter(CalendarDisplay.this, year, month, currentDay);
                calendar.setAdapter(adapter);
                currentDate.setText(DateFormat.format(dateTemplate, myCal.getTimeInMillis()));

                setBackGrounds();

            }
            //left to right fling
            else if (secondAction.getX() - firstAction.getX() > 120 && Math.abs(velocityX) > 200) {
                mainView.setBackgroundDrawable(getResources().getDrawable(R.drawable.summer_bkgr));
                month += 1;
                if (month > 11) {
                    month = 0;
                    year += 1;
                    myCal.set(Calendar.YEAR, year);
                    myCal.set(Calendar.MONTH, month);
                    adapter = new CalendarAdapter(CalendarDisplay.this, year, month, currentDay);
                    calendar.setAdapter(adapter);
                    currentDate.setText(DateFormat.format(dateTemplate, myCal.getTimeInMillis()));
                }
                //probably inefficient but...yeah it works.
                myCal.set(Calendar.MONTH, month);
                adapter = new CalendarAdapter(CalendarDisplay.this, year, month, currentDay);
                calendar.setAdapter(adapter);
                currentDate.setText(DateFormat.format(dateTemplate, myCal.getTimeInMillis()));

                setBackGrounds();
            }

            return true;
        }

ここにログキャットがあります

05-18 17:41:53.585: E/AndroidRuntime(23793): FATAL EXCEPTION: main
05-18 17:41:53.585: E/AndroidRuntime(23793): java.lang.NullPointerException
05-18 17:41:53.585: E/AndroidRuntime(23793):    at com.calendar.CalendarDisplay$myGestureListener.onFling(CalendarDisplay.java:223)
05-18 17:41:53.585: E/AndroidRuntime(23793):    at android.view.GestureDetector.onTouchEvent(GestureDetector.java:606)
05-18 17:41:53.585: E/AndroidRuntime(23793):    at com.calendar.CalendarDisplay$1.onTouch(CalendarDisplay.java:95)
05-18 17:41:53.585: E/AndroidRuntime(23793):    at android.view.View.dispatchTouchEvent(View.java:3881)
05-18 17:41:53.585: E/AndroidRuntime(23793):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:903)
05-18 17:41:53.585: E/AndroidRuntime(23793):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
05-18 17:41:53.585: E/AndroidRuntime(23793):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
05-18 17:41:53.585: E/AndroidRuntime(23793):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
05-18 17:41:53.585: E/AndroidRuntime(23793):    at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1930)
05-18 17:41:53.585: E/AndroidRuntime(23793):    at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1205)
05-18 17:41:53.585: E/AndroidRuntime(23793):    at android.app.Activity.dispatchTouchEvent(Activity.java:2155)
05-18 17:41:53.585: E/AndroidRuntime(23793):    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1914)
05-18 17:41:53.585: E/AndroidRuntime(23793):    at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2249)
05-18 17:41:53.585: E/AndroidRuntime(23793):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1933)
05-18 17:41:53.585: E/AndroidRuntime(23793):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-18 17:41:53.585: E/AndroidRuntime(23793):    at android.os.Looper.loop(Looper.java:130)
05-18 17:41:53.585: E/AndroidRuntime(23793):    at android.app.ActivityThread.main(ActivityThread.java:3906)
05-18 17:41:53.585: E/AndroidRuntime(23793):    at java.lang.reflect.Method.invokeNative(Native Method)
05-18 17:41:53.585: E/AndroidRuntime(23793):    at java.lang.reflect.Method.invoke(Method.java:507)
05-18 17:41:53.585: E/AndroidRuntime(23793):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:840)
05-18 17:41:53.585: E/AndroidRuntime(23793):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:598)
05-18 17:41:53.585: E/AndroidRuntime(23793):    at dalvik.system.NativeStart.main(Native Method)
4

1 に答える 1

2

これを試して

 private final GestureDetector gestureDetector = new GestureDetector(new GestureListener());

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        page= (TestLayout) findViewById(R.id.mainpage);
        page.setOnTouchListener(this);

    }   
    @Override
    protected void onPause() {
        super.onPause();
        onStop();
    }
    private final class GestureListener extends SimpleOnGestureListener {

        private static final int SWIPE_THRESHOLD = 100;
        private static final int SWIPE_VELOCITY_THRESHOLD = 100;

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

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            boolean result = false;
            try {
                float diffY = e2.getY() - e1.getY();
                float diffX = e2.getX() - e1.getX();
                //if (Math.abs(diffX) > Math.abs(diffY)) {
                    if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                        if (diffX > 0) {
                            page.pre();
                        } else {
                           page.next();
                        }
                    }
                //} 

            } catch (Exception exception) {
                exception.printStackTrace();
            }
            return result;
        }

メソッドpage.pre()page.post()ビジネス ロジックを変更します。

それが役に立てば幸い。

于 2013-05-18T07:42:21.247 に答える