ViewFlipper - 点击图片

时间:2016-11-12 20:07:04

标签: java android viewflipper

我的ViewFlipper出现了奇怪的问题。除了一种特殊情况外,它的工作正常。当我第一次打开我的活动并点击第一张图片而不是滑动它时,应用程序显示错误。但是当我滑动它时,它的工作正常并显示下一个图像。在此之后,当我倒退到第一张图片并再次点击它时,它运作良好。

我的java代码:

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.widget.ViewFlipper;

public class MyActivity extends Activity {

    ViewFlipper my_flipper;
    private float lastX;

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

        my_flipper = (ViewFlipper) findViewById(R.id.my_flipper);
        showImages();
    }

    public void showImages () {
        my_flipper.setDisplayedChild(0);
        my_flipper.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                switch (motionEvent.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        lastX = motionEvent.getX();
                        return true;
                    case MotionEvent.ACTION_UP:
                        float currentX = motionEvent.getX();
                        if (lastX < currentX) {
                            if (my_flipper.getDisplayedChild() == 0)
                                break;
                            my_flipper.setInAnimation(getApplication(), R.anim.slide_in_from_left);
                            my_flipper.setOutAnimation(getApplication(), R.anim.slide_out_to_right);
                            my_flipper.showNext();
                        }
                        if (lastX > currentX) {
                            if (my_flipper.getDisplayedChild() == 1) {
                                break;
                            }
                            my_flipper.setInAnimation(getApplication(), R.anim.slide_in_from_right);
                            my_flipper.setOutAnimation(getApplication(), R.anim.slide_out_to_left);
                            my_flipper.showPrevious();
                        }

                        my_flipper.getOutAnimation().setAnimationListener(new Animation.AnimationListener() {

                            public void onAnimationStart(Animation animation) {
                            }

                            public void onAnimationRepeat(Animation animation) {
                            }

                            public void onAnimationEnd(Animation animation) {

                            }
                        });
                }
                return true;
            }
        });
    }
}

和XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:background="#0f4b75"
    tools:context=".MyActivity">

    <ViewFlipper
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/my_flipper"
        android:background="#000000"
        >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/atrakcja_0"
            android:adjustViewBounds="true"
            android:src="@drawable/foto"
            />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/atrakcja_1"
            android:adjustViewBounds="true"
            android:src="@drawable/foto"
            />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/atrakcja_2"
            android:adjustViewBounds="true"
            android:src="@drawable/foto"
            />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/atrakcja_3"
            android:adjustViewBounds="true"
            android:src="@drawable/foto"
            />

    </ViewFlipper>

</RelativeLayout>

一个动画示例:

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">
    <translate
        android:fromXDelta="-100%" android:toXDelta="0%"
        android:fromYDelta="0%" android:toYDelta="0%"
        android:duration="140" />
</set>

最后记录:

  

W / dalvikvm:threadid = 1:线程退出时未捕获异常(group = 0x41ed5e00)   E / AndroidRuntime:致命异常:主要                     过程:android.dedykowani_narciarze,PID:26403                     显示java.lang.NullPointerException                         在android.dedykowani_narciarze.MyActivity $ 1.onTouch(MyActivity.java:51)                         在android.view.View.dispatchTouchEvent(View.java:7729)                         在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2212)                         在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1945)                         在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2218)                         在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)                         在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2218)                         在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)                         在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2218)                         在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)                         在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2218)                         在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)                         在com.android.internal.policy.impl.PhoneWindow $ DecorView.superDispatchTouchEvent(PhoneWindow.java:2093)                         在com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1540)                         在android.app.Activity.dispatchTouchEvent(Activity.java:2500)                         在com.android.internal.policy.impl.PhoneWindow $ DecorView.dispatchTouchEvent(PhoneWindow.java:2041)                         在android.view.View.dispatchPointerEvent(View.java:7914)                         在android.view.ViewRootImpl $ ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4165)                         在android.view.ViewRootImpl $ ViewPostImeInputStage.onProcess(ViewRootImpl.java:4044)                         在android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:3556)                         在android.view.ViewRootImpl $ InputStage.onDeliverToNext(ViewRootImpl.java:3606)                         在android.view.ViewRootImpl $ InputStage.forward(ViewRootImpl.java:3575)                         在android.view.ViewRootImpl $ AsyncInputStage.forward(ViewRootImpl.java:3682)                         在android.view.ViewRootImpl $ InputStage.apply(ViewRootImpl.java:3583)                         在android.view.ViewRootImpl $ AsyncInputStage.apply(ViewRootImpl.java:3739)                         在android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:3556)                         在android.view.ViewRootImpl $ InputStage.onDeliverToNext(ViewRootImpl.java:3606)                         在android.view.ViewRootImpl $ InputStage.forward(ViewRootImpl.java:3575)                         在android.view.ViewRootImpl $ InputStage.apply(ViewRootImpl.java:3583)                         在android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:3556)                         在android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5750)                         在android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5730)                         在android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5701)                         在android.view.ViewRootImpl $ WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5830)                         在android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)                         在android.os.MessageQueue.nativePollOnce(本机方法)                         在android.os.MessageQueue.next(MessageQueue.java:138)                         在android.os.Looper.loop(Looper.java:123)                         在android.app.ActivityThread.main(ActivityThread.java:5333)                         at java.lang.reflect.Method.invokeNative(Native Method)                         在java.lang.reflect.Method.invoke(Method.java:515)                         在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:895)                         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:711)                         在dalvik.system.NativeStart.main(本地方法)

此行产生错误:

my_flipper.getOutAnimation().setAnimationListener(new Animation.AnimationListener() {

我将非常感谢您提供的任何帮助。

0 个答案:

没有答案