我在我的应用程序中使用拖放功能。一切正常,但出现此错误ImageView cannot be cast to LinearLayout
。当我直接尝试在图像视图上设置图像时,正如我在代码的注释部分中提到的那样,我在 imageview 屏幕上也变得空白。
这是我的代码,
img1.setOnTouchListener(new OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent e)
{
if (e.getAction() == MotionEvent.ACTION_DOWN)
{
status=1;
ClipData data = ClipData.newPlainText("foo","bar");
DragShadowBuilder shadowBuilder = new DragShadowBuilder(v);
v.startDrag(data, shadowBuilder, v, 0);
return true;
}
else
{
return false;
}
}
});
iv1.setOnDragListener(DragAndDropGameActivity.this);
}
@Override
public boolean onDrag(View v, DragEvent event)
{
// TODO Auto-generated method stub
Drawable image=null;
Drawable normalborder = getResources().getDrawable(R.drawable.border);
Drawable greenborder = getResources().getDrawable(R.drawable.greenborder);
if(status==1)
{
image = v.getBackground();
if (event.getAction() == DragEvent.ACTION_DRAG_STARTED)
{
Toast.makeText(getApplicationContext(), "Clicked & Started" , Toast.LENGTH_SHORT).show();
image = v.getBackground();
//self.setBackgroundDrawable(redBorder);
}
else if (event.getAction() == DragEvent.ACTION_DRAG_ENTERED)
{
//insideOfMe = true;
//border=v.getBackground();
//v.setBackground(greenborder);
Toast.makeText(getApplicationContext(), "Entered" , Toast.LENGTH_SHORT).show();
}
else if (event.getAction() == DragEvent.ACTION_DRAG_EXITED)
{
Toast.makeText(getApplicationContext(), "Exited" , Toast.LENGTH_SHORT).show();
//insideOfMe = false;
}
else if (event.getAction() == DragEvent.ACTION_DROP)
{
Toast.makeText(getApplicationContext(), "Dropped" , Toast.LENGTH_SHORT).show();
View view = (View) event.getLocalState();
ViewGroup owner = (ViewGroup) view.getParent();
owner.removeView(view);
LinearLayout container = (LinearLayout) v;
container.addView(view);
//image = v.getBackground();
//iv1.setBackground(image);
//iv1.setBackgroundDrawable(image);
}
else if (event.getAction() == DragEvent.ACTION_DRAG_ENDED)
{
Toast.makeText(getApplicationContext(), "Ended" , Toast.LENGTH_SHORT).show();
}
return true;
}
return false;
}
这是我的 LogCat,
10-28 11:53:35.137: E/AndroidRuntime(3095): FATAL EXCEPTION: main
10-28 11:53:35.137: E/AndroidRuntime(3095): java.lang.ClassCastException: android.widget.ImageView cannot be cast to android.widget.LinearLayout
10-28 11:53:35.137: E/AndroidRuntime(3095): at com.example.draganddropgame.DragAndDropGameActivity.onDrag(DragAndDropGameActivity.java:121)
10-28 11:53:35.137: E/AndroidRuntime(3095): at android.view.View.dispatchDragEvent(View.java:16371)
10-28 11:53:35.137: E/AndroidRuntime(3095): at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1237)
10-28 11:53:35.137: E/AndroidRuntime(3095): at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1237)
10-28 11:53:35.137: E/AndroidRuntime(3095): at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1237)
10-28 11:53:35.137: E/AndroidRuntime(3095): at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1237)
10-28 11:53:35.137: E/AndroidRuntime(3095): at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1237)
10-28 11:53:35.137: E/AndroidRuntime(3095): at android.view.ViewRootImpl.handleDragEvent(ViewRootImpl.java:3838)
10-28 11:53:35.137: E/AndroidRuntime(3095): at android.view.ViewRootImpl.access$600(ViewRootImpl.java:95)
10-28 11:53:35.137: E/AndroidRuntime(3095): at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:2999)
10-28 11:53:35.137: E/AndroidRuntime(3095): at android.os.Handler.dispatchMessage(Handler.java:99)
10-28 11:53:35.137: E/AndroidRuntime(3095): at android.os.Looper.loop(Looper.java:137)
10-28 11:53:35.137: E/AndroidRuntime(3095): at android.app.ActivityThread.main(ActivityThread.java:5041)
10-28 11:53:35.137: E/AndroidRuntime(3095): at java.lang.reflect.Method.invokeNative(Native Method)
10-28 11:53:35.137: E/AndroidRuntime(3095): at java.lang.reflect.Method.invoke(Method.java:511)
10-28 11:53:35.137: E/AndroidRuntime(3095): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-28 11:53:35.137: E/AndroidRuntime(3095): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-28 11:53:35.137: E/AndroidRuntime(3095): at dalvik.system.NativeStart.main(Native Method)
这就是我的布局的样子,
请帮我..
谢谢。