2016-08-17 106 views
1

我是一名初學者,我正在嘗試做一個簡單的單詞爭奪遊戲。目前,我只是想弄清楚如何使用拖放功能。我瀏覽了一些教程並嘗試應用我學到的知識,但是當我試圖將其中一個字母片放入其目標時,我的程序不斷崩潰。Android拖放實現

運行時錯誤我不斷收到是

java.lang.ClassCastException: android.widget.ImageView cannot be cast to android.widget.LinearLayout 

它說,錯誤發生在這條線的java代碼

LinearLayout newParent = (LinearLayout)v;

,但在我以前做的教程線做得很好。顯然我做錯了什麼。我從昨天開始就一直堅持這一點,並會非常感謝一些幫助。

這裏是我的xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:layout_alignParentTop="true" 
android:layout_alignParentLeft="true" 
android:layout_alignParentStart="true" 
android:layout_marginLeft="10dp" 
android:layout_marginStart="10dp" 
android:layout_marginTop="41dp" 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:weightSum="1" 
android:paddingLeft="0dp" 
android:paddingRight="10dp"> 


<TextView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:text="0:59" 
    android:id="@+id/textViewTimer" 
    android:editable="false" 
    android:gravity="center_horizontal" 
    android:textSize="50dp" 
    android:textStyle="bold" /> 

<LinearLayout 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_weight="1" 
    android:paddingTop="60dp"> 

    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1"> 

     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/imageView_target1" 
      android:src="@drawable/blank_space" 
      android:cropToPadding="false" 
      android:baselineAlignBottom="false" 
      android:layout_weight="1" 
      android:padding="3dp" /> 
    </LinearLayout> 

    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1"> 

     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/imageView_target2" 
      android:src="@drawable/blank_space" 
      android:cropToPadding="false" 
      android:baselineAlignBottom="false" 
      android:layout_weight="1" 
      android:padding="3dp" /> 
    </LinearLayout> 

    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1"> 

     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/imageView_target3" 
      android:src="@drawable/blank_space" 
      android:cropToPadding="false" 
      android:baselineAlignBottom="false" 
      android:layout_weight="1" 
      android:padding="3dp" /> 
    </LinearLayout> 

    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1"> 

     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/imageView_target4" 
      android:src="@drawable/blank_space" 
      android:cropToPadding="false" 
      android:baselineAlignBottom="false" 
      android:layout_weight="1" 
      android:padding="3dp" /> 
    </LinearLayout> 

    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1"> 

     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/imageView_target5" 
      android:src="@drawable/blank_space" 
      android:cropToPadding="false" 
      android:baselineAlignBottom="false" 
      android:layout_weight="1" 
      android:padding="3dp" /> 
    </LinearLayout> 
</LinearLayout> 

<LinearLayout 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="156dp" 
    android:paddingBottom="0dp"> 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/imageView_letter1" 
     android:src="@drawable/letter_a" 
     android:layout_weight="1" 
     android:paddingLeft="5dp" 
     android:paddingRight="5dp" /> 
    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/imageView_letter2" 
     android:src="@drawable/letter_g" 
     android:layout_weight="1" 
     android:paddingLeft="5dp" 
     android:paddingRight="5dp" /> 
    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/imageView_letter3" 
     android:src="@drawable/letter_r" 
     android:layout_weight="1" 
     android:paddingLeft="5dp" 
     android:paddingRight="5dp" /> 
    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/imageView_letter4" 
     android:src="@drawable/letter_e" 
     android:layout_weight="1" 
     android:paddingLeft="5dp" 
     android:paddingRight="5dp" /> 
    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/imageView_letter5" 
     android:src="@drawable/letter_v" 
     android:layout_weight="1" 
     android:paddingLeft="5dp" 
     android:paddingRight="5dp" /> 
</LinearLayout> 
</LinearLayout> 

這是我的Java代碼

public class MainActivity extends Activity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    //set OnTouchListeners for the letters 
    findViewById(R.id.imageView_letter1).setOnTouchListener(new  MyTouchListener()); 
    findViewById(R.id.imageView_letter2).setOnTouchListener(new MyTouchListener()); 
    findViewById(R.id.imageView_letter3).setOnTouchListener(new MyTouchListener()); 
    findViewById(R.id.imageView_letter4).setOnTouchListener(new MyTouchListener()); 
    findViewById(R.id.imageView_letter5).setOnTouchListener(new MyTouchListener()); 

    //set OnDragListeners for the drop targets 
    findViewById(R.id.imageView_target1).setOnDragListener(new MyDragListener()); 
    findViewById(R.id.imageView_target2).setOnDragListener(new MyDragListener()); 
    findViewById(R.id.imageView_target3).setOnDragListener(new MyDragListener()); 
    findViewById(R.id.imageView_target4).setOnDragListener(new MyDragListener()); 
    findViewById(R.id.imageView_target5).setOnDragListener(new MyDragListener()); 
} 

private final class MyTouchListener implements View.OnTouchListener { 
    public boolean onTouch(View view, MotionEvent motionEvent) { 
     if(motionEvent.getAction() == MotionEvent.ACTION_DOWN) { 
      ClipData data = ClipData.newPlainText("",""); 
      View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view); 
      view.startDrag(data, shadowBuilder, view, 0); 
      view.setVisibility(View.INVISIBLE); 
      return true; 
     } 
     else{ 
      return false; 
     } 
    } 
} 

class MyDragListener implements View.OnDragListener { 
    Drawable emptySpace = getResources().getDrawable(R.drawable.blank_space); 

    @Override 
    public boolean onDrag(View v, DragEvent event) { 
     int action = event.getAction(); 
     switch (action) { 
      case DragEvent.ACTION_DRAG_STARTED: 
       //do nothing 
       break; 
      case DragEvent.ACTION_DRAG_ENTERED: 
      // //could highlight drop target box 
       break; 
      case DragEvent.ACTION_DRAG_EXITED: 
       //un-highlight 
       break; 
      case DragEvent.ACTION_DROP: 
       //dropped 
       View view = (View)event.getLocalState(); 
       LinearLayout oldParent = (LinearLayout)view.getParent(); 
       oldParent.removeView(view); 
       LinearLayout newParent = (LinearLayout)v; 
       newParent.addView(view); 
       break; 
      case DragEvent.ACTION_DRAG_ENDED: 
       v.setBackgroundDrawable(emptySpace); 
       break; 
      default: 
       break; 
     } 
     return true; 
    } 
} 

}

回答

1

要調用setOnDragListener()ImageView部件。因此,傳入onDrag()View將是其中一個ImageView小部件,而不是LinearLayout

+0

Ahhhh就是這樣!這也是總體感覺。非常感謝! –