2016-08-17 4 views
1

Ich bin ein Anfänger mit Android und ich versuche, ein einfaches Wort Scramble-Spiel zu machen. Im Moment versuche ich nur herauszufinden, wie man die Drag-and-Drop-Funktionalität nutzt. Ich habe ein paar Tutorials durchgesehen und versucht, das anzuwenden, was ich gelernt habe, aber mein Programm stürzt immer ab, wenn ich versuche, einen der Buchstabenkacheln in sein Ziel zu legen.Android Drag and Drop Implementierung

Die Laufzeitfehler ich immer halten ist

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

Es ist zu sagen, dass der Fehler in dieser Zeile des Java-Code auftritt

LinearLayout newParent = (LinearLayout)v;

Aber diese Linie in den Tutorials Ich habe fertig funktioniert gut. Offensichtlich mache ich etwas falsch. Ich bin gerade seit gestern daran fest und würde wirklich etwas Hilfe zu schätzen wissen.

Hier ist meine 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> 

Und hier ist mein Java-Code

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; 
    } 
} 

}

Antwort

1

Sie rufen setOnDragListener() auf ImageView Widgets. Daher wird die View, die in onDrag() übergeben wurde, einer dieser ImageView Widgets sein, nicht ein LinearLayout.

+0

Ahhhh Das war es! Das macht auch Sinn. Ich danke dir sehr! –