2014-01-09 211 views
2

我現在正在實現一個拖放操作。在這個操作中,從相對佈局拖到另一個是允許的。如果在佈局中有一個孩子,並且將其他圖像拖動到該圖像上,則圖像必須交換位置。這裏是我的問題,當我投視圖查看組,類拋出異常發生。我真的不知道如何解決它,因爲我是一個新的android。日誌貓的錯誤行指向此((ViewGroup)父).addView(nextChild)。請給我建議。對不起,如果我的問題打擾你。java.lang.ClassCastException:android.view.View無法轉換爲android.view.ViewGroup

這是我的日誌貓輸出:

01-09 01:57:27.829: E/AndroidRuntime(2021): FATAL EXCEPTION: main 
01-09 01:57:27.829: E/AndroidRuntime(2021): Process: com.example.barnyar, PID: 2021 
01-09 01:57:27.829: E/AndroidRuntime(2021): java.lang.ClassCastException: android.view.View cannot be cast to android.view.ViewGroup 
01-09 01:57:27.829: E/AndroidRuntime(2021):  at com.example.barnyar.MainActivity$MyDragListener.onDrag(MainActivity.java:751) 
01-09 01:57:27.829: E/AndroidRuntime(2021):  at android.view.View.dispatchDragEvent(View.java:17371) 
01-09 01:57:27.829: E/AndroidRuntime(2021):  at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1300) 
01-09 01:57:27.829: E/AndroidRuntime(2021):  at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1286) 
01-09 01:57:27.829: E/AndroidRuntime(2021):  at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1286) 
01-09 01:57:27.829: E/AndroidRuntime(2021):  at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1286) 
01-09 01:57:27.829: E/AndroidRuntime(2021):  at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1286) 
01-09 01:57:27.829: E/AndroidRuntime(2021):  at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1286) 
01-09 01:57:27.829: E/AndroidRuntime(2021):  at android.view.ViewRootImpl.handleDragEvent(ViewRootImpl.java:5026) 
01-09 01:57:27.829: E/AndroidRuntime(2021):  at android.view.ViewRootImpl.access$800(ViewRootImpl.java:96) 
01-09 01:57:27.829: E/AndroidRuntime(2021):  at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:3213) 
01-09 01:57:27.829: E/AndroidRuntime(2021):  at android.os.Handler.dispatchMessage(Handler.java:102) 
01-09 01:57:27.829: E/AndroidRuntime(2021):  at android.os.Looper.loop(Looper.java:136) 
01-09 01:57:27.829: E/AndroidRuntime(2021):  at android.app.ActivityThread.main(ActivityThread.java:5017) 
01-09 01:57:27.829: E/AndroidRuntime(2021):  at java.lang.reflect.Method.invokeNative(Native Method) 
01-09 01:57:27.829: E/AndroidRuntime(2021):  at java.lang.reflect.Method.invoke(Method.java:515) 
01-09 01:57:27.829: E/AndroidRuntime(2021):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
01-09 01:57:27.829: E/AndroidRuntime(2021):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
01-09 01:57:27.829: E/AndroidRuntime(2021):  at dalvik.system.NativeStart.main(Native Method) 

這是我的代碼:

@Override 
public boolean onDrag(View v, DragEvent event) { 

    View parent = new View(MainActivity.this); 
     switch (event.getAction()) { 

     case DragEvent.ACTION_DRAG_STARTED: 
       Toast.makeText(getApplicationContext(), "Start Drag ", Toast.LENGTH_LONG).show(); 
       parent = v; 
       Log.i("class",parent+""); 
      break; 

     case DragEvent.ACTION_DRAG_ENTERED: 
      v.setBackground(normalShape); //change the shape of the view 
      break; 


     case DragEvent.ACTION_DRAG_EXITED: 
      v.setBackground(normalShape); //change the shape of the view back to normal 
      break; 

     case DragEvent.ACTION_DROP: 

      if(v.getClass().toString().equals("class android.widget.RelativeLayout")){ 
       Log.i("class","Relative"); 



       if(((ViewGroup)v).getChildCount()!=0){ 

       View nextChild = ((ViewGroup)v).getChildAt(0); 
       Log.i("child",((ViewGroup)v).getChildCount()+""); 
         ((ViewGroup)parent).addView(nextChild);//the error line 
       View view = (View) event.getLocalState(); 
        ViewGroup viewgroup = (ViewGroup) view.getParent(); 
        viewgroup.removeView(view); 
        RelativeLayout containView = (RelativeLayout) v; 
        containView.addView(view); 
        view.setVisibility(View.VISIBLE); 

       } 


       else { 
        View view = (View) event.getLocalState(); 
        ViewGroup viewgroup = (ViewGroup) view.getParent(); 
        viewgroup.removeView(view); 
        RelativeLayout containView = (RelativeLayout) v; 
        containView.addView(view); 
        view.setVisibility(View.VISIBLE); 
       } 
       } 

      else { 

        Log.i("CLass", v.getClass()+"Cant drop"); 
        View view = (View) event.getLocalState(); 
        view.setVisibility(View.VISIBLE); 

        break; 
       } 
       break; 

     case DragEvent.ACTION_DRAG_ENDED: 
      v.setBackground(normalShape); //go back to normal shape 

     default: 
      break; 
     } 
     return true; 
    } 
} 
+1

發佈您的logcat輸出 – Hardik

+0

您是否嘗試將變量v更改爲ViewGroup而不是View? – San

+0

@ san。這是覆蓋方法。我認爲這不能改變。 –

回答

3

的ViewGroup VS查看鑄造

ViewGroup也是View(延伸View)這樣反而鑄造ViewGroup讓他們爲View對象,只有投在真正需要(即訪問該AREN方法時不在View中可用,僅在ViewGroup)。

// childCount is for ViewGroup only, if problem still occurs you can 
// check first if v object really is a ViewGroup instance with the 
// below instanceof check 
if(v instanceof ViewGroup && parent instanceof ViewGroup && ((ViewGroup)v).getChildCount()!=0){ 

    // perform the cast only once, also now we know it's safe thanks to the instanceof 
    ViewGroup vg = (ViewGroup)v 

    View nextChild = vg.getChildAt(0); 
    Log.i("child", vg.getChildCount()+""); 

    // no guarantee here whatever that parent is a more specific ViewGroup 
    // so if we need this to be a ViewGroup we'll have to cast but check 
    // with instanceof first 
    ((ViewGroup)parent).addView(nextChild); //this was the error line 

    View view = (View) event.getLocalState(); 
    ViewGroup viewgroup = (ViewGroup) view.getParent(); 
    viewgroup.removeView(view); 
    RelativeLayout containView = (RelativeLayout) v; 
    containView.addView(view); 
    view.setVisibility(View.VISIBLE); 

} else { 
    // PERFORM SOME LOGGING HERE 
    // so you know what views got dropped in here that didn't match your requirements in the first place 
    // TODO 

} 

的instanceof

也避免檢查Class的名稱與一個String.equals(String):boolean就像你在下面的代碼片段一樣。

在你的情況下,這甚至會失敗,因爲前面的「類」不會是結果的一部分。

if(v.getClass().toString().equals("class android.widget.RelativeLayout")){ 

檢查Java中的Object的類型,正確的方法是使用isntanceof就像我在下面的代碼一樣。

if(v instanceof android.widget.RelativeLayout){ 

有關instanceof的更多信息,請參見this StackOverflow question

+0

它會要求我在添加「parent.addView(nextChild)」時輸入轉換父項「 –

+0

好吧我看到,addView僅適用於ViewGroup,我會更新代碼 – hcpl

+0

感謝您的幫助 –

0

問題是在這裏if(((ViewGroup)v).getChildCount()!=0)View nextChild = ((ViewGroup)v).getChildAt(0);

刪除鑄造,並嘗試這種方式if(v.getChildCount()!=0)

+0

我看不到getChildCount()查看。:( –

+0

那是因爲getChildCount()是ViewGroup的一種方法。 – San

1

減少鑄造和使用ins檢查類型。

if(v instanceof ViewGroup){ 
    ViewGroup vg = (ViewGroup)v; 

    // TODO your code that needs a ViewGroup. 
} 
0

查看的ViewGroup的父母,所以你不能強制類型轉換ViewGroup中,孩子其父是查看。那就是拋出ClassCastException的原因。

相關問題