我現在正在實現一個拖放操作。在這個操作中,從相對佈局拖到另一個是允許的。如果在佈局中有一個孩子,並且將其他圖像拖動到該圖像上,則圖像必須交換位置。這裏是我的問題,當我投視圖查看組,類拋出異常發生。我真的不知道如何解決它,因爲我是一個新的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;
}
}
發佈您的logcat輸出 – Hardik
您是否嘗試將變量v更改爲ViewGroup而不是View? – San
@ san。這是覆蓋方法。我認爲這不能改變。 –