点击图片时出错
我的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:线程ID = 1:致命异常:主要过程:android.dedykowani_narciarze,PID:26403显示java.lang.NullPointerException在android.dedykowani_narciarze.MyActivity $ 1.onTouch(螺纹(组= 0x41ed5e00)E / AndroidRuntime与未捕获的异常退出在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1945)处的android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2212)处的android.view.View.dispatchTouchEvent(View.java:7729)处的MyActivity.java:51) )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)在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2218)在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)在android.view.ViewGroup.dispatchTransformedTouchEvent( ViewGroup.java:2218)在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)在android.view.ViewRootImpl $ ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4165)在android.view.ViewRootImpl $ ViewPostImeInputStage.onProcess(ViewRootImpl.java:4044)在android.view.ViewRootImpl $ InputStage。在android.view.ViewRootImpl上提供(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)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。在android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5750)在android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5730)在android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java递送(ViewRootImpl.java:3556) :5701)在android.view.ViewRootImpl $ WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5830)在android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)在android.os.MessageQueue.nativePollOnce(本机方法)的机器人。 os.MessageQueue.next(MessageQueue.java:138)在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)
这一行产生错误:
my_flipper.getOutAnimation().setAnimationListener(new Animation.AnimationListener() {
我会感谢您提供的任何帮助。
链接地址: http://www.djcxy.com/p/18357.html上一篇: error while tapping image
下一篇: tabhost along with tabs goes when navigation drawer pulled