NullPointerException异常而与GestureDetector工作 [英] NullPointerException while working with GestureDetector
问题描述
下面是装载frameanimation的在不同events.First事件的发生两个不同的图像的code为而活动start.Others是 onTouch()
其中我正在为 onDown()
和 onScroll()使用
GestureDetector
的code>的问题是,我得到 NullPointerException异常
,而其在 OnScroll()
。我发现它没有得到的Y坐标有什么可以是问题。
包com.example.animationtry;进口android.os.Bundle;
进口android.app.Activity;
进口android.graphics.drawable.AnimationDrawable;
进口android.util.Log;
进口android.view.GestureDetector;
进口android.view.MotionEvent;
进口android.view.View;
进口android.view.GestureDetector.SimpleOnGestureListener;
进口android.widget.ImageView;公共类MainActivity扩展活动
{
ImageView的IMG,IMG1;
AnimationDrawable动画;
私人GestureDetector gestureDetector;
@覆盖
保护无效的onCreate(捆绑savedInstanceState)
{
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
IMG =(ImageView的)findViewById(R.id.image);
img.post(新的Runnable()
{
公共无效的run()
{
img.setBackgroundResource(R.anim.frameanimation);
AnimationDrawable frameAnimation =(AnimationDrawable)IMG .getBackground();
frameAnimation.setVisible(假,真);
frameAnimation.start();
}
});
img.setOnTouchListener(新View.OnTouchListener()
{
公共布尔onTouch(视图V,MotionEvent事件)
{
字符串IMG =img目录;
gestureDetector =新GestureDetector(新MyGestureDetector(IMG));
如果(gestureDetector.onTouchEvent(事件))
{
返回true;
}
返回false;
}
}); IMG1 =(ImageView的)findViewById(R.id.image1);
img.post(新的Runnable()
{
公共无效的run()
{
img1.setBackgroundResource(R.anim.frameanimation);
AnimationDrawable frameAnimation =(AnimationDrawable)img1.getBackground();
frameAnimation.setVisible(假,真);
frameAnimation.start();
}
});
img1.setOnTouchListener(新View.OnTouchListener()
{
公共布尔onTouch(视图V,MotionEvent事件)
{
字符串IMG1 =IMG1;
gestureDetector =新GestureDetector(新MyGestureDetector(IMG1));
如果(gestureDetector.onTouchEvent(事件))
{
返回true;
}
返回false;
}
});
} 类MyGestureDetector扩展SimpleOnGestureListener
{
私有静态最终诠释SWIPE_MAX_OFF_PATH = 40;
私有静态最终诠释SWIPE_THRESHOLD_VELOCITY = 30;
字符串视图名;
公共MyGestureDetector(字符串视图)
{
视图名=视图。
} @覆盖
公共布尔onFling(MotionEvent E1,E2 MotionEvent,浮velocityX,浮动velocityY)
{
返回true;
}
@覆盖
公共布尔onDown(MotionEvent五)
{
Log.i(视图名称,视图名);
如果(viewname.equalsIgnoreCase(IMG))
{
img.setBackgroundResource(R.anim.clickanimation);
动画=(AnimationDrawable)img.getBackground();
animation.setVisible(假,真);
animation.start();
}
否则如果(viewname.equalsIgnoreCase(IMG1))
{
img1.setBackgroundResource(R.anim.clickanimation);
动画=(AnimationDrawable)img1.getBackground();
animation.setVisible(假,真);
animation.start();
}
返回true;
} @覆盖
公共布尔onScroll(MotionEvent E1,E2 MotionEvent,浮distanceX,浮动distanceY)
{
Log.i(Y1,Float.toString(e1.getY()));
Log.i(Y2,Float.toString(e2.getY()));
Log.i(拖视图名称,视图名);
如果(Math.abs(e1.getY() - e2.getY())> SWIPE_MAX_OFF_PATH&放大器;&放大器; Math.abs(distanceY)GT; SWIPE_THRESHOLD_VELOCITY)
{
如果(viewname.equalsIgnoreCase(IMG))
{
img.setBackgroundResource(R.anim.draganimaton);
动画=(AnimationDrawable)img.getBackground();
animation.setVisible(假,真);
animation.start();
}
否则如果(viewname.equalsIgnoreCase(IMG1))
{
img1.setBackgroundResource(R.anim.draganimaton);
动画=(AnimationDrawable)img1.getBackground();
animation.setVisible(假,真);
animation.start();
}
返回true;
}
返回true;
}
}
}
和下方则是logcat的:
6月一日至12日:12:22.637:E / AndroidRuntime(1062):致命异常:主要
六月一日至12日:12:22.637:E / AndroidRuntime(1062):显示java.lang.NullPointerException
六月一日至12日:12:22.637:E / AndroidRuntime(1062):在com.example.animationtry.MainActivity $ MyGestureDetector.onScroll(MainActivity.java:117)
六月一日至12日:12:22.637:E / AndroidRuntime(1062),:在android.view.GestureDetector.onTouchEvent(GestureDetector.java:541)
六月一日至12日:12:22.637:E / AndroidRuntime(1062):在com.example.animationtry.MainActivity $ 4.onTouch(MainActivity.java:69)
六月一日至12日:12:22.637:E / AndroidRuntime(1062):在android.view.View.dispatchTouchEvent(View.java:3881)
六月一日至12日:12:22.637:E / AndroidRuntime(1062):在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
六月一日至12日:12:22.637:E / AndroidRuntime(1062):在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
六月一日至12日:12:22.637:E / AndroidRuntime(1062):在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
六月一日至12日:12:22.637:E / AndroidRuntime(1062):在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
六月一日至12日:12:22.637:E / AndroidRuntime(1062):在com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1691)
六月一日至12日:12:22.637:E / AndroidRuntime(1062):在com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1125)
六月一日至12日:12:22.637:E / AndroidRuntime(1062):在android.app.Activity.dispatchTouchEvent(Activity.java:2096)
六月一日至12日:12:22.637:E / AndroidRuntime(1062):在com.android.internal.policy.impl.PhoneWindow $ DecorView.dispatchTouchEvent(PhoneWindow.java:1675)
六月一日至12日:12:22.637:E / AndroidRuntime(1062):在android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2194)
六月一日至12日:12:22.637:E / AndroidRuntime(1062):在android.view.ViewRoot.handleMessage(ViewRoot.java:1878)
六月一日至12日:12:22.637:E / AndroidRuntime(1062):在android.os.Handler.dispatchMessage(Handler.java:99)
六月一日至12日:12:22.637:E / AndroidRuntime(1062):在android.os.Looper.loop(Looper.java:123)
六月一日至12日:12:22.637:E / AndroidRuntime(1062):在android.app.ActivityThread.main(ActivityThread.java:3683)
六月一日至12日:12:22.637:E / AndroidRuntime(1062):在java.lang.reflect.Method.invokeNative(本机方法)
六月一日至12日:12:22.637:E / AndroidRuntime(1062):在java.lang.reflect.Method.invoke(Method.java:507)
六月一日至12日:12:22.637:E / AndroidRuntime(1062):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839)
六月一日至12日:12:22.637:E / AndroidRuntime(1062):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
六月一日至12日:12:22.637:E / AndroidRuntime(1062):在dalvik.system.NativeStart.main(本机方法)
我有问题 e1.getY()
为返回null值 onScroll()
所以我删除了检查的一部分,只是检查 SWIPE_MAX_OFF_PATH
用的 distanceY
在 onScroll()
,现在它完美地工作。
包com.example.animationtry;进口android.os.Bundle;
进口android.app.Activity;
进口android.graphics.drawable.AnimationDrawable;
进口android.util.Log;
进口android.view.GestureDetector;
进口android.view.MotionEvent;
进口android.view.View;
进口android.view.GestureDetector.SimpleOnGestureListener;
进口android.widget.ImageView;公共类MainActivity扩展活动
{
ImageView的IMG,IMG1;
AnimationDrawable动画;
私人GestureDetector gestureDetector;
私有静态最终诠释SWIPE_MAX_OFF_PATH = 40;
私有静态最终诠释SWIPE_THRESHOLD_VELOCITY = 30; @覆盖
保护无效的onCreate(捆绑savedInstanceState)
{
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
IMG =(ImageView的)findViewById(R.id.image);
img.post(新的Runnable()
{
公共无效的run()
{
img.setBackgroundResource(R.anim.frameanimation);
AnimationDrawable frameAnimation =(AnimationDrawable)IMG .getBackground();
frameAnimation.setVisible(假,真);
frameAnimation.start();
}
});
img.setOnTouchListener(新View.OnTouchListener()
{
公共布尔onTouch(视图V,MotionEvent事件)
{
字符串IMG =img目录;
gestureDetector =新GestureDetector(新MyGestureDetector(IMG));
如果(gestureDetector.onTouchEvent(事件))
{
返回true;
}
返回false;
}
}); IMG1 =(ImageView的)findViewById(R.id.image1);
img.post(新的Runnable()
{
公共无效的run()
{
img1.setBackgroundResource(R.anim.frameanimation);
AnimationDrawable frameAnimation =(AnimationDrawable)img1.getBackground();
frameAnimation.setVisible(假,真);
frameAnimation.start();
}
});
img1.setOnTouchListener(新View.OnTouchListener()
{
公共布尔onTouch(视图V,MotionEvent事件)
{
字符串IMG1 =IMG1;
gestureDetector =新GestureDetector(新MyGestureDetector(IMG1));
如果(gestureDetector.onTouchEvent(事件))
{
返回true;
}
返回false;
}
});
} 类MyGestureDetector扩展SimpleOnGestureListener
{
字符串视图名;
公共MyGestureDetector(字符串视图)
{
视图名=视图。
} @覆盖
公共布尔onFling(MotionEvent E1,E2 MotionEvent,浮velocityX,浮动velocityY)
{ 返回true;
}
@覆盖
公共布尔onDown(MotionEvent五)
{
Log.i(视图名称,视图名);
如果(viewname.equalsIgnoreCase(IMG))
{
img.setBackgroundResource(R.anim.clickanimation);
动画=(AnimationDrawable)img.getBackground();
animation.setVisible(假,真);
animation.start();
}
否则如果(viewname.equalsIgnoreCase(IMG1))
{
img1.setBackgroundResource(R.anim.clickanimation);
动画=(AnimationDrawable)img1.getBackground();
animation.setVisible(假,真);
animation.start();
}
返回true;
} @覆盖
公共布尔onScroll(MotionEvent E1,E2 MotionEvent,浮distanceX,浮动distanceY)
{
Log.i(拖视图名称,视图名);
如果(Math.abs(distanceY)GT; SWIPE_MAX_OFF_PATH)
{
Log.i(内部,拖);
如果(viewname.equalsIgnoreCase(IMG))
{
img.setBackgroundResource(R.anim.draganimaton);
动画=(AnimationDrawable)img.getBackground();
animation.setVisible(假,真);
animation.start();
}
否则如果(viewname.equalsIgnoreCase(IMG1))
{
img1.setBackgroundResource(R.anim.draganimaton);
动画=(AnimationDrawable)img1.getBackground();
animation.setVisible(假,真);
animation.start();
}
}
返回true;
}
}
}
Below is the code of loading frameanimation for two different images on the occurrence of different events.First event is while the activity start.Others are the onTouch()
where i making use of GestureDetector
for the onDown()
and onScroll()
The problem is that i get NullPointerException
while its in the OnScroll()
.I found that it is not getting the Y coordinates what can be the issue.
package com.example.animationtry;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.widget.ImageView;
public class MainActivity extends Activity
{
ImageView img,img1;
AnimationDrawable animation;
private GestureDetector gestureDetector;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img=(ImageView)findViewById(R.id.image);
img.post(new Runnable()
{
public void run()
{
img.setBackgroundResource(R.anim.frameanimation);
AnimationDrawable frameAnimation = (AnimationDrawable) img .getBackground();
frameAnimation.setVisible(false, true);
frameAnimation.start();
}
});
img.setOnTouchListener(new View.OnTouchListener()
{
public boolean onTouch(View v, MotionEvent event)
{
String img="img";
gestureDetector = new GestureDetector(new MyGestureDetector(img));
if (gestureDetector.onTouchEvent(event))
{
return true;
}
return false;
}
});
img1=(ImageView)findViewById(R.id.image1);
img.post(new Runnable()
{
public void run()
{
img1.setBackgroundResource(R.anim.frameanimation);
AnimationDrawable frameAnimation = (AnimationDrawable) img1.getBackground();
frameAnimation.setVisible(false, true);
frameAnimation.start();
}
});
img1.setOnTouchListener(new View.OnTouchListener()
{
public boolean onTouch(View v, MotionEvent event)
{
String img1="img1";
gestureDetector = new GestureDetector(new MyGestureDetector(img1));
if (gestureDetector.onTouchEvent(event))
{
return true;
}
return false;
}
});
}
class MyGestureDetector extends SimpleOnGestureListener
{
private static final int SWIPE_MAX_OFF_PATH = 40;
private static final int SWIPE_THRESHOLD_VELOCITY = 30;
String viewname;
public MyGestureDetector(String view)
{
viewname=view;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY)
{
return true;
}
@Override
public boolean onDown(MotionEvent e)
{
Log.i("View Name",viewname);
if(viewname.equalsIgnoreCase("img"))
{
img.setBackgroundResource(R.anim.clickanimation);
animation = (AnimationDrawable)img.getBackground();
animation.setVisible(false, true);
animation.start();
}
else if(viewname.equalsIgnoreCase("img1"))
{
img1.setBackgroundResource(R.anim.clickanimation);
animation = (AnimationDrawable)img1.getBackground();
animation.setVisible(false, true);
animation.start();
}
return true;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2,float distanceX, float distanceY)
{
Log.i("y1",Float.toString(e1.getY()));
Log.i("y2",Float.toString(e2.getY()));
Log.i("Drag View Name",viewname);
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH && Math.abs(distanceY) > SWIPE_THRESHOLD_VELOCITY)
{
if(viewname.equalsIgnoreCase("img"))
{
img.setBackgroundResource(R.anim.draganimaton);
animation = (AnimationDrawable)img.getBackground();
animation.setVisible(false, true);
animation.start();
}
else if(viewname.equalsIgnoreCase("img1"))
{
img1.setBackgroundResource(R.anim.draganimaton);
animation = (AnimationDrawable)img1.getBackground();
animation.setVisible(false, true);
animation.start();
}
return true;
}
return true;
}
}
}
and below is the logcat:
01-12 06:12:22.637: E/AndroidRuntime(1062): FATAL EXCEPTION: main
01-12 06:12:22.637: E/AndroidRuntime(1062): java.lang.NullPointerException
01-12 06:12:22.637: E/AndroidRuntime(1062): at com.example.animationtry.MainActivity$MyGestureDetector.onScroll(MainActivity.java:117)
01-12 06:12:22.637: E/AndroidRuntime(1062): at android.view.GestureDetector.onTouchEvent(GestureDetector.java:541)
01-12 06:12:22.637: E/AndroidRuntime(1062): at com.example.animationtry.MainActivity$4.onTouch(MainActivity.java:69)
01-12 06:12:22.637: E/AndroidRuntime(1062): at android.view.View.dispatchTouchEvent(View.java:3881)
01-12 06:12:22.637: E/AndroidRuntime(1062): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
01-12 06:12:22.637: E/AndroidRuntime(1062): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
01-12 06:12:22.637: E/AndroidRuntime(1062): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
01-12 06:12:22.637: E/AndroidRuntime(1062): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
01-12 06:12:22.637: E/AndroidRuntime(1062): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1691)
01-12 06:12:22.637: E/AndroidRuntime(1062): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1125)
01-12 06:12:22.637: E/AndroidRuntime(1062): at android.app.Activity.dispatchTouchEvent(Activity.java:2096)
01-12 06:12:22.637: E/AndroidRuntime(1062): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1675)
01-12 06:12:22.637: E/AndroidRuntime(1062): at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2194)
01-12 06:12:22.637: E/AndroidRuntime(1062): at android.view.ViewRoot.handleMessage(ViewRoot.java:1878)
01-12 06:12:22.637: E/AndroidRuntime(1062): at android.os.Handler.dispatchMessage(Handler.java:99)
01-12 06:12:22.637: E/AndroidRuntime(1062): at android.os.Looper.loop(Looper.java:123)
01-12 06:12:22.637: E/AndroidRuntime(1062): at android.app.ActivityThread.main(ActivityThread.java:3683)
01-12 06:12:22.637: E/AndroidRuntime(1062): at java.lang.reflect.Method.invokeNative(Native Method)
01-12 06:12:22.637: E/AndroidRuntime(1062): at java.lang.reflect.Method.invoke(Method.java:507)
01-12 06:12:22.637: E/AndroidRuntime(1062): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-12 06:12:22.637: E/AndroidRuntime(1062): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-12 06:12:22.637: E/AndroidRuntime(1062): at dalvik.system.NativeStart.main(Native Method)
I got the problem e1.getY()
returning null value for the onScroll()
so i removed that checking part and just checked SWIPE_MAX_OFF_PATH
with the distanceY
of the onScroll()
and now its working perfectly..
package com.example.animationtry;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.widget.ImageView;
public class MainActivity extends Activity
{
ImageView img,img1;
AnimationDrawable animation;
private GestureDetector gestureDetector;
private static final int SWIPE_MAX_OFF_PATH = 40;
private static final int SWIPE_THRESHOLD_VELOCITY = 30;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img=(ImageView)findViewById(R.id.image);
img.post(new Runnable()
{
public void run()
{
img.setBackgroundResource(R.anim.frameanimation);
AnimationDrawable frameAnimation = (AnimationDrawable) img .getBackground();
frameAnimation.setVisible(false, true);
frameAnimation.start();
}
});
img.setOnTouchListener(new View.OnTouchListener()
{
public boolean onTouch(View v, MotionEvent event)
{
String img="img";
gestureDetector = new GestureDetector(new MyGestureDetector(img));
if (gestureDetector.onTouchEvent(event))
{
return true;
}
return false;
}
});
img1=(ImageView)findViewById(R.id.image1);
img.post(new Runnable()
{
public void run()
{
img1.setBackgroundResource(R.anim.frameanimation);
AnimationDrawable frameAnimation = (AnimationDrawable) img1.getBackground();
frameAnimation.setVisible(false, true);
frameAnimation.start();
}
});
img1.setOnTouchListener(new View.OnTouchListener()
{
public boolean onTouch(View v, MotionEvent event)
{
String img1="img1";
gestureDetector = new GestureDetector(new MyGestureDetector(img1));
if (gestureDetector.onTouchEvent(event))
{
return true;
}
return false;
}
});
}
class MyGestureDetector extends SimpleOnGestureListener
{
String viewname;
public MyGestureDetector(String view)
{
viewname=view;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY)
{
return true;
}
@Override
public boolean onDown(MotionEvent e)
{
Log.i("View Name",viewname);
if(viewname.equalsIgnoreCase("img"))
{
img.setBackgroundResource(R.anim.clickanimation);
animation = (AnimationDrawable)img.getBackground();
animation.setVisible(false, true);
animation.start();
}
else if(viewname.equalsIgnoreCase("img1"))
{
img1.setBackgroundResource(R.anim.clickanimation);
animation = (AnimationDrawable)img1.getBackground();
animation.setVisible(false, true);
animation.start();
}
return true;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2,float distanceX, float distanceY)
{
Log.i("Drag View Name",viewname);
if (Math.abs(distanceY)>SWIPE_MAX_OFF_PATH)
{
Log.i("inside","drag");
if(viewname.equalsIgnoreCase("img"))
{
img.setBackgroundResource(R.anim.draganimaton);
animation = (AnimationDrawable)img.getBackground();
animation.setVisible(false, true);
animation.start();
}
else if(viewname.equalsIgnoreCase("img1"))
{
img1.setBackgroundResource(R.anim.draganimaton);
animation = (AnimationDrawable)img1.getBackground();
animation.setVisible(false, true);
animation.start();
}
}
return true;
}
}
}
这篇关于NullPointerException异常而与GestureDetector工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!