error while tapping image

I've got strange problem with my ViewFlipper. It's working perfectly, besides one particular situation. When I open my activity for the first time and tap first image instead of slide it, application shows error. But when I slide it it's works fine and shows next image. After this, when I go backwards to first image and tap it again, it's working good.

My java code:

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;
            }
        });
    }
}

and 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>

one of animation example:

<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>

and finally logs:

W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x41ed5e00) E/AndroidRuntime: FATAL EXCEPTION: main Process: android.dedykowani_narciarze, PID: 26403 java.lang.NullPointerException at android.dedykowani_narciarze.MyActivity$1.onTouch(MyActivity.java:51) at android.view.View.dispatchTouchEvent(View.java:7729) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2212) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1945) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2218) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2218) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2218) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2218) at android.view.Vie wGroup.dispatchTouchEvent(ViewGroup.java:1959) at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2093) at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1540) at android.app.Activity.dispatchTouchEvent(Activity.java:2500) at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2041) at android.view.View.dispatchPointerEvent(View.java:7914) at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4165) at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4044) at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3556) at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3606) at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3575) at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3682) at android.view.ViewRootImpl$InputStage.apply(ViewR ootImpl.java:3583) at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3739) at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3556) at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3606) at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3575) at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3583) at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3556) at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5750) at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5730) at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5701) at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5830) at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185) at android.os.MessageQueue.nativePollOnce(Native Method) at android.os.MessageQueue.next(MessageQueue.java:138) at android.os.Looper.loop(Looper.java: 123) at android.app.ActivityThread.main(ActivityThread.java:5333) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:895) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:711) at dalvik.system.NativeStart.main(Native Method)

This line produces error:

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

I will be grateful for any help you can provide.

链接地址: http://www.djcxy.com/p/18358.html

上一篇: OutOfMemoryError android m

下一篇: 点击图片时出错