0
我對Android開發非常陌生我試圖將一個活動轉換爲一個片段,除了作爲我經歷的老教程之外,它在活動中運行良好。我已經找出了一些錯誤,並且我確信有更多的東西需要整理,因此歡迎任何額外的提示。GestorDetector在片段不能正常工作
這裏是代碼,我的日誌也在下面。我只是在我的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$1.up(categorySelectorFragment.java:122)
at com.example.game.categorySelectorFragment$1.access$000(categorySelectorFragment.java:42)
at com.example.game.categorySelectorFragment$1$1.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並讓你知道。對於#2,我將如何使用onActivityCreated()方法?對不起,仍然在學習,我用它在我的其他碎片,但在這種情況下,我用onCreateView()像這樣 '公共視圖onCreateView(LayoutInflater inflater,ViewGroup容器,Bundle savedInstanceState){ 返回inflater.inflate(R.layout。 q_multi_four,容器,false); } @Override public void onActivityCreated(Bundle savedInstanceState){ super.onActivityCreated(savedInstanceState); //其他代碼... } ' – Xenocide122
謝謝! #1工作,但我似乎無法讓#2與它的設置方式一起工作。如果我在onCreateView()中膨脹我的視圖並將其他所有內容放在我的onActivityCreated()中,我該如何調用view.setOnTouchListener()和mViewFlipper =(ViewFlipper)view.findViewById(R.id.view_flipper);它們都稱之爲視圖。 -謝謝您的幫助! – Xenocide122
你能幫我一些嗎? – Xenocide122