片段中的 GestorDetector 不工作
GestorDetector in Fragment not working
我是 Android 开发的新手 我正在尝试将 activity 转换为片段,它在 Activity 中运行良好,除了是我经历过的旧教程之外。我已经解决了一些错误,我确信还有更多的事情需要纠正,所以欢迎任何额外的提示。
这是代码,我的日志也在下面。我只是在我的 MainActivity 中使用 navigationView 来启动这个片段。
categorySelectFragment.java
public class categorySelectorFragment extends Fragment {
private ViewFlipper mViewFlipper;
private int mSpeed;
private int mCount;
private int mFactor;
private boolean mAnimating;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.category_selector, container, false);
mViewFlipper = (ViewFlipper)getActivity().findViewById(R.id.view_flipper);
//mDetector = new GestureDetector(getActivity(), this);
mAnimating = false;
mCount = 0;
mSpeed = 0;
final GestureDetector gesture = new GestureDetector(getActivity(), new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
return true;
}
private Runnable r1 = new Runnable() {
@Override
public void run() {
up();
if (mCount<1) {
mAnimating = false;
} else {
Handler h = new Handler();
h.postDelayed(r1, mSpeed);
}
}
};
private Runnable r2 = new Runnable() {
@Override
public void run() {
down();
if (mCount<1) {
mAnimating = false;
} else {
Handler h = new Handler();
h.postDelayed(r2, mSpeed);
}
}
};
@Override
public boolean onFling(MotionEvent start, MotionEvent finish, float xVelocity, float yVelocity) {
try {
if (mAnimating) return true;
mAnimating = true;
mCount = (int) Math.abs(yVelocity) / 300;
mFactor = (int) 300 / mCount;
mSpeed = mFactor;
if (yVelocity>0) {
//down
Handler h = new Handler();
h.postDelayed(r2, mSpeed);
} else {
//up
Handler h = new Handler();
h.postDelayed(r1, mSpeed);
}
((TextView)getActivity().findViewById(R.id.velocity)).setText("VELOCITY => "+Float.toString(yVelocity));
} catch (ArithmeticException e) {
//swiped too slow doesn't register
mAnimating = false;
}
return true;
}
private void up() {
mCount--;
mSpeed+=mFactor;
Animation inFromBottom = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 1.0f,
Animation.RELATIVE_TO_PARENT, 0.0f);
inFromBottom.setInterpolator(new AccelerateInterpolator());
inFromBottom.setDuration(mSpeed);
Animation outToTop = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, -1.0f);
outToTop.setInterpolator(new AccelerateInterpolator());
outToTop.setDuration(mSpeed);
mViewFlipper.clearAnimation();
mViewFlipper.setInAnimation(inFromBottom);
mViewFlipper.setOutAnimation(outToTop);
if (mViewFlipper.getDisplayedChild()==0) {
mViewFlipper.setDisplayedChild(2);
} else {
mViewFlipper.showPrevious();
}
((TextView)getActivity().findViewById(R.id.counter)).setText("COUNTER => "+Integer.toString(mCount));
((TextView)getActivity().findViewById(R.id.speed)).setText("SPEED => "+Integer.toString(mSpeed));
}
private void down() {
mCount--;
mSpeed+=mFactor;
Animation outToBottom = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 1.0f);
outToBottom.setInterpolator(new AccelerateInterpolator());
outToBottom.setDuration(mSpeed);
Animation inFromTop = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, -1.0f,
Animation.RELATIVE_TO_PARENT, 0.0f);
inFromTop.setInterpolator(new AccelerateInterpolator());
inFromTop.setDuration(mSpeed);
mViewFlipper.clearAnimation();
mViewFlipper.setInAnimation(inFromTop);
mViewFlipper.setOutAnimation(outToBottom);
if (mViewFlipper.getDisplayedChild()==0) {
mViewFlipper.setDisplayedChild(2);
} else {
mViewFlipper.showPrevious();
}
((TextView)getActivity().findViewById(R.id.counter)).setText("COUNTER => "+Integer.toString(mCount));
((TextView)getActivity().findViewById(R.id.speed)).setText("SPEED => "+Integer.toString(mSpeed));
}
@Override
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
public void onSwipeRight() {
Toast.makeText(getActivity(), " RIGHT ", Toast.LENGTH_LONG).show();
}
public void onSwipeLeft() {
Toast.makeText(getActivity(), " LEFT ", Toast.LENGTH_LONG).show();
}
public void onSwipeTop() {
Toast.makeText(getActivity(), " TOP ", Toast.LENGTH_LONG).show();
}
public void onSwipeBottom() {
Toast.makeText(getActivity(), " BOTTOM ", Toast.LENGTH_LONG).show();
}
});
view.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return gesture.onTouchEvent(event);
}
});
return view;
}
}
Logcat
FATAL EXCEPTION: main
Process: com.example.game, PID: 9403
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ViewFlipper.clearAnimation()' on a null object reference
at com.example.game.categorySelectorFragment.up(categorySelectorFragment.java:122)
at com.example.game.categorySelectorFragment.access[=11=]0(categorySelectorFragment.java:42)
at com.example.game.categorySelectorFragment.run(categorySelectorFragment.java:54)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
感谢您的帮助!
1)尝试更换
mViewFlipper = (ViewFlipper)getActivity().findViewById(R.id.view_flipper);
和
mViewFlipper = (ViewFlipper)view.findViewById(R.id.view_flipper);
2) 我建议你在onActivityCreated() 方法中初始化视图。
我是 Android 开发的新手 我正在尝试将 activity 转换为片段,它在 Activity 中运行良好,除了是我经历过的旧教程之外。我已经解决了一些错误,我确信还有更多的事情需要纠正,所以欢迎任何额外的提示。
这是代码,我的日志也在下面。我只是在我的 MainActivity 中使用 navigationView 来启动这个片段。
categorySelectFragment.java
public class categorySelectorFragment extends Fragment {
private ViewFlipper mViewFlipper;
private int mSpeed;
private int mCount;
private int mFactor;
private boolean mAnimating;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.category_selector, container, false);
mViewFlipper = (ViewFlipper)getActivity().findViewById(R.id.view_flipper);
//mDetector = new GestureDetector(getActivity(), this);
mAnimating = false;
mCount = 0;
mSpeed = 0;
final GestureDetector gesture = new GestureDetector(getActivity(), new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
return true;
}
private Runnable r1 = new Runnable() {
@Override
public void run() {
up();
if (mCount<1) {
mAnimating = false;
} else {
Handler h = new Handler();
h.postDelayed(r1, mSpeed);
}
}
};
private Runnable r2 = new Runnable() {
@Override
public void run() {
down();
if (mCount<1) {
mAnimating = false;
} else {
Handler h = new Handler();
h.postDelayed(r2, mSpeed);
}
}
};
@Override
public boolean onFling(MotionEvent start, MotionEvent finish, float xVelocity, float yVelocity) {
try {
if (mAnimating) return true;
mAnimating = true;
mCount = (int) Math.abs(yVelocity) / 300;
mFactor = (int) 300 / mCount;
mSpeed = mFactor;
if (yVelocity>0) {
//down
Handler h = new Handler();
h.postDelayed(r2, mSpeed);
} else {
//up
Handler h = new Handler();
h.postDelayed(r1, mSpeed);
}
((TextView)getActivity().findViewById(R.id.velocity)).setText("VELOCITY => "+Float.toString(yVelocity));
} catch (ArithmeticException e) {
//swiped too slow doesn't register
mAnimating = false;
}
return true;
}
private void up() {
mCount--;
mSpeed+=mFactor;
Animation inFromBottom = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 1.0f,
Animation.RELATIVE_TO_PARENT, 0.0f);
inFromBottom.setInterpolator(new AccelerateInterpolator());
inFromBottom.setDuration(mSpeed);
Animation outToTop = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, -1.0f);
outToTop.setInterpolator(new AccelerateInterpolator());
outToTop.setDuration(mSpeed);
mViewFlipper.clearAnimation();
mViewFlipper.setInAnimation(inFromBottom);
mViewFlipper.setOutAnimation(outToTop);
if (mViewFlipper.getDisplayedChild()==0) {
mViewFlipper.setDisplayedChild(2);
} else {
mViewFlipper.showPrevious();
}
((TextView)getActivity().findViewById(R.id.counter)).setText("COUNTER => "+Integer.toString(mCount));
((TextView)getActivity().findViewById(R.id.speed)).setText("SPEED => "+Integer.toString(mSpeed));
}
private void down() {
mCount--;
mSpeed+=mFactor;
Animation outToBottom = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 1.0f);
outToBottom.setInterpolator(new AccelerateInterpolator());
outToBottom.setDuration(mSpeed);
Animation inFromTop = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, -1.0f,
Animation.RELATIVE_TO_PARENT, 0.0f);
inFromTop.setInterpolator(new AccelerateInterpolator());
inFromTop.setDuration(mSpeed);
mViewFlipper.clearAnimation();
mViewFlipper.setInAnimation(inFromTop);
mViewFlipper.setOutAnimation(outToBottom);
if (mViewFlipper.getDisplayedChild()==0) {
mViewFlipper.setDisplayedChild(2);
} else {
mViewFlipper.showPrevious();
}
((TextView)getActivity().findViewById(R.id.counter)).setText("COUNTER => "+Integer.toString(mCount));
((TextView)getActivity().findViewById(R.id.speed)).setText("SPEED => "+Integer.toString(mSpeed));
}
@Override
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
public void onSwipeRight() {
Toast.makeText(getActivity(), " RIGHT ", Toast.LENGTH_LONG).show();
}
public void onSwipeLeft() {
Toast.makeText(getActivity(), " LEFT ", Toast.LENGTH_LONG).show();
}
public void onSwipeTop() {
Toast.makeText(getActivity(), " TOP ", Toast.LENGTH_LONG).show();
}
public void onSwipeBottom() {
Toast.makeText(getActivity(), " BOTTOM ", Toast.LENGTH_LONG).show();
}
});
view.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return gesture.onTouchEvent(event);
}
});
return view;
}
}
Logcat
FATAL EXCEPTION: main
Process: com.example.game, PID: 9403
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ViewFlipper.clearAnimation()' on a null object reference
at com.example.game.categorySelectorFragment.up(categorySelectorFragment.java:122)
at com.example.game.categorySelectorFragment.access[=11=]0(categorySelectorFragment.java:42)
at com.example.game.categorySelectorFragment.run(categorySelectorFragment.java:54)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
感谢您的帮助!
1)尝试更换 mViewFlipper = (ViewFlipper)getActivity().findViewById(R.id.view_flipper); 和 mViewFlipper = (ViewFlipper)view.findViewById(R.id.view_flipper);
2) 我建议你在onActivityCreated() 方法中初始化视图。