2013-10-28 3 views
1

내 응용 프로그램에서 드래그 앤 드롭 기능을 사용하고 있습니다. 모든 것이 잘 작동하지만이 오류가 발생합니다 ImageView cannot be cast to LinearLayout. 그리고 내가 직접 이미지 뷰에 이미지를 설정하려고 할 때 코드의 주석 부분에서 언급 한 것처럼 이미지 뷰 화면에서도 공백이 생깁니다. 이건 내 로그 캣입니다오류 발생 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; 
} 

다음

내 코드는,

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

line 121 무엇입니까?이 LinearLayout container = (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); 
관련 문제