【问题标题】:Getting error ImageView cannot be cast to LinearLayout获取错误 ImageView 无法转换为 LinearLayout
【发布时间】:2013-10-28 11:57:22
【问题描述】:

我在我的应用程序中使用拖放功能。一切正常,但出现此错误ImageView cannot be cast to LinearLayout。 当我直接尝试在图像视图上设置图像时,正如我在代码的注释部分中提到的那样,我在图像视图屏幕上也变得空白。

这是我的代码,

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)

这就是我的布局的样子,

请帮帮我..

谢谢。

【问题讨论】:

  • 第 121 行是什么。是LinearLayout container = (LinearLayout) v吗?
  • LinearLayout container = (LinearLayout) v; 虽然iv1 是一个ImageView! (我打赌)
  • @Sherif elKhatib:是的 iv1 也是一个 ImageView。
  • 如果您的 xml 定义正确并且您没有错误地转换,只需进行清理并构建。
  • 您将 ImageView 投射到 LinearLayout 这是错误的。这就是为什么你得到 excpetion

标签: android android-layout drag-and-drop drag


【解决方案1】:

尝试从您的 xml 中检索您的 LinearLayout

LinearLayout container = (LinearLayout) findViewById(your linear layout id);

或者创建一个新的,但是你必须添加一些额外的代码

示例:

 // creating LinearLayout
        LinearLayout linLayout = new LinearLayout(this);
        // specifying vertical orientation
        linLayout.setOrientation(LinearLayout.VERTICAL);
        // creating LayoutParams  
        LayoutParams linLayoutParam = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); 
        // set LinearLayout as a root element of the screen 
        setContentView(linLayout, linLayoutParam);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多