2014-03-06 38 views
2

我有一個自定義視圖,基本上是一個FrameLayout與一個Imageview和一個文本視圖。我在這個視圖上使用了拖放功能,並且完美地工作。然後我在這個視圖上執行RotateAnimation。我可以再一次提出這個觀點,但下一次我碰認爲,這與以下異常消失:android:NullpointerException on startDrag()動畫後​​

03-06 21:18:26.367: E/View(21124): Unable to initiate drag 
03-06 21:18:26.367: E/View(21124): java.lang.NullPointerException 
03-06 21:18:26.367: E/View(21124): at android.view.View.startDrag(View.java:16331) 
03-06 21:18:26.367: E/View(21124): at com.example.annotations.AnnotationView.onTouchEvent(AnnotationView.java:51) 
03-06 21:18:26.367: E/View(21124): at android.view.View.dispatchTouchEvent(View.java:7392) 
03-06 21:18:26.367: E/View(21124): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2229) 
03-06 21:18:26.367: E/View(21124): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1964) 
03-06 21:18:26.367: E/View(21124): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2235) 
03-06 21:18:26.367: E/View(21124): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1936) 
03-06 21:18:26.367: E/View(21124): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2235) 
03-06 21:18:26.367: E/View(21124): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1936) 
03-06 21:18:26.367: E/View(21124): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2235) 
03-06 21:18:26.367: E/View(21124): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1936) 
03-06 21:18:26.367: E/View(21124): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2235) 
03-06 21:18:26.367: E/View(21124): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1936) 
03-06 21:18:26.367: E/View(21124): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2235) 
03-06 21:18:26.367: E/View(21124): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1936) 
03-06 21:18:26.367: E/View(21124): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2177) 
03-06 21:18:26.367: E/View(21124): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1482) 
03-06 21:18:26.367: E/View(21124): at android.app.Activity.dispatchTouchEvent(Activity.java:2483) 
03-06 21:18:26.367: E/View(21124): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2125) 
03-06 21:18:26.367: E/View(21124): at android.view.View.dispatchPointerEvent(View.java:7577) 
03-06 21:18:26.367: E/View(21124): at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3376) 
03-06 21:18:26.367: E/View(21124): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3308) 
03-06 21:18:26.367: E/View(21124): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4421) 
03-06 21:18:26.367: E/View(21124): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4399) 
03-06 21:18:26.367: E/View(21124): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4505) 
03-06 21:18:26.367: E/View(21124): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:178) 
03-06 21:18:26.367: E/View(21124): at android.os.MessageQueue.nativePollOnce(Native Method) 
03-06 21:18:26.367: E/View(21124): at android.os.MessageQueue.next(MessageQueue.java:125) 
03-06 21:18:26.367: E/View(21124): at android.os.Looper.loop(Looper.java:124) 
03-06 21:18:26.367: E/View(21124): at android.app.ActivityThread.main(ActivityThread.java:4949) 
03-06 21:18:26.367: E/View(21124): at java.lang.reflect.Method.invokeNative(Native Method) 
03-06 21:18:26.367: E/View(21124): at java.lang.reflect.Method.invoke(Method.java:511) 
03-06 21:18:26.367: E/View(21124): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1043) 
03-06 21:18:26.367: E/View(21124): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:810) 
03-06 21:18:26.367: E/View(21124): at dalvik.system.NativeStart.main(Native Method) 

下面的代碼來旋轉視圖都寫在搜索條的onProgressChanged()

    RotateAnimation r = new RotateAnimation(mCustomView.ROTATION, progress - 180, 
          RotateAnimation.RELATIVE_TO_SELF, 0.5f, 
          RotateAnimation.RELATIVE_TO_SELF, 0.5f); 
        // r.setStartOffset(1000); 
        // r.setDuration(1000); 
        r.setFillAfter(true); // HERE 
        mCustomView.ROTATION = progress - 180; 
        mCustomView.startAnimation(r); 

onTouchEvent()定製View類中:

@Override 
public boolean onTouchEvent(MotionEvent event) { 

    if (event.getAction() == MotionEvent.ACTION_DOWN) { 
     ClipData data = ClipData.newPlainText("", ""); 
     DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(this); 
     if (data.equals(null)) 
      Log.d("Nully", "data + " + data.toString()); 
     if (shadowBuilder.equals(null)) 
      Log.d("Nully", "shad" + shadowBuilder.toString()); 
     this.startDrag(data, shadowBuilder, this, 0); 
     this.setVisibility(View.INVISIBLE); 
     return true; 
    } else { 
     return false; 
    } 

我調試的應用程序使用斷點和對象確實ñ在這個例外之前的命中似乎是空的。

+1

把你的問題放在這裏,而不是鏈接到他們。如果您要尋求幫助,請不要讓人們聯繫到任何地方。 –

+0

對不起,我認爲在pastebin中查看它會比較容易,因爲堆棧跟蹤很大。 –

+1

@VishnuMohanG友情提示,越詳細越好(代碼,堆棧跟蹤,日誌,清晰描述)。如果他們需要更多的信息,人們就會被製造出來並且倒下。 – samosaris

回答

1

我有同樣的問題。您基本上需要從視圖中選擇cancel()動畫或clearAnimation(),然後才能再次拖動。您可以在AnimationListener的onAnimationEnd()中觸發此清理。我從視圖中清除了動畫,因爲取消正在創建我的視圖的多個「圖像」。

正是你爲什麼需要這樣做,我不知道。