2013-10-28 35 views
1

我在我的應用程序中使用拖放功能。一切工作正常,但得到這個錯誤ImageView cannot be cast to LinearLayout。 當我直接嘗試在圖像視圖上設置圖像,如我在我的代碼的註釋部分中提到的那樣,我也在imageview屏幕上變得空白。獲取錯誤ImageView無法轉換爲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) 

這是我的佈局看起來如何,

enter image description here

請幫助我..

謝謝。

+0

這行是嗎? – nio

+0

什麼是第121行。這是LinearLayout容器=(LinearLayout)v'嗎? – Raghunandan

+0

@Raghunandan:是的! – Shiv

回答

0

嘗試從XML

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

檢索您的LinearLayout或創建新的,但你必須添加一些額外的代碼

例如:

// 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); 
相關問題