2016-10-13 13 views
1

アンドロイドで単純なドラッグアンドドロップを実行しています。ログメッセージにClassCastExceptionが表示され、Javaコードの行番号70のエラーが表示されます。 行番号70は、onDrag(View layoutview、DragEvent dragEvent)メソッドの内側にあり、DragEvent.ACTION_DROPの場合です。その行は** **以内です。私は何が間違っているのか分からない。前もって感謝します。アンドロイドでドラッグアンドドロップを実行中にClassCastExceptionを表示します

これは、これはXMLコード

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/activity_main" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
android:orientation="vertical" 
tools:context="com.example.anandzoom.draganddrop.MainActivity"> 

<LinearLayout 
    android:id="@+id/layout_top" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:padding="16dp"> 

    <TextView 
     android:id="@+id/text_drag" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Drag me" 
     android:textSize="19sp"/> 

</LinearLayout> 

<LinearLayout 
    android:id="@+id/layout_bottom" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:padding="16dp"> 

    <TextView 
     android:id="@+id/text_drop" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Drop here" 
     /> 
</LinearLayout> 

は、ログメッセージはエラーが表示されているJavaコード

package com.example.anandzoom.draganddrop; 

import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.util.Log; 
import android.view.DragEvent; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.LinearLayout; 
import android.widget.TextView; 

public class MainActivity extends AppCompatActivity implements View.OnTouchListener,View.OnDragListener { 

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

    findViewById(R.id.text_drag).setOnTouchListener(this); 
    findViewById(R.id.text_drop).setOnDragListener(this); 

} 

public boolean onTouch(View view, MotionEvent motionEvent) 
{ 
    if(motionEvent.getAction() == MotionEvent.ACTION_DOWN) 
    { 
     View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view); 
     view.startDrag(null, shadowBuilder, view, 0); 
     view.setVisibility(View.INVISIBLE); 
     return true; 
    } 
    else 
    { 
     return false; 
    } 
} 


public boolean onDrag(View layoutview, DragEvent dragEvent) 
{ 
    int action = dragEvent.getAction(); 
    switch (action) 
    { 
     case DragEvent.ACTION_DRAG_STARTED: 
     { 
      Log.v("DragAndDrop", "ACTION_DRAG_STARTED"); 
      break; 
     } 
     case DragEvent.ACTION_DRAG_ENTERED: 
     { 
      Log.v("DragAndDrop", "ACTION_DRAG_ENTERED"); 
      break; 
     } 
     case DragEvent.ACTION_DRAG_EXITED: 
     { 
      Log.v("DragAndDrop", "ACTION_DRAG_EXITED"); 
      break; 
     } 
     case DragEvent.ACTION_DROP: 
     { 
      Log.v("DragAndDrop", "ACTION_DROP"); 

      TextView view = (TextView) dragEvent.getLocalState(); 
      ViewGroup owner = (ViewGroup) layoutview.getParent(); 
      owner.removeView(view); 
      **LinearLayout container = (LinearLayout) layoutview;** 
      container.addView(view); 
      view.setVisibility(View.VISIBLE); 
      break; 
     } 
     case DragEvent.ACTION_DRAG_ENDED: 
     { 
      Log.v("DragAndDrop", "ACTION_DRAG_ENDED"); 
      break; 
     } 
     default: 
     { 
      break; 
     } 
    } 
    return true; 
} 

} 

IS:ClassCastExceptionが

D/AndroidRuntime: Shutting down VM 
10-13 14:58:35.635 1270-1270/com.example.anandzoom.draganddrop E/AndroidRuntime: FATAL EXCEPTION: main 
                      Process: com.example.anandzoom.draganddrop, PID: 1270 
                      Theme: themes:{default=overlay:com.resurrectionremix.pitchblack, iconPack:org.cyanogenmod.hexolibre, fontPkg:com.resurrectionremix.pitchblack, com.android.systemui=overlay:com.resurrectionremix.pitchblack, com.android.systemui.navbar=overlay:com.resurrectionremix.pitchblack} 
                      java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to android.widget.LinearLayout 
                       at com.example.anandzoom.draganddrop.MainActivity.onDrag(MainActivity.java:70) 
                       at android.view.View.dispatchDragEvent(View.java:19702) 
                       at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1518) 
                       at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1518) 
                       at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1518) 
                       at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1518) 
                       at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1518) 
                       at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1518) 
                       at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1518) 
                       at android.view.ViewRootImpl.handleDragEvent(ViewRootImpl.java:5290) 
                       at android.view.ViewRootImpl.-wrap6(ViewRootImpl.java) 
                       at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:3465) 
                       at android.os.Handler.dispatchMessage(Handler.java:102) 
                       at android.os.Looper.loop(Looper.java:148) 
                       at android.app.ActivityThread.main(ActivityThread.java:5461) 
                       at java.lang.reflect.Method.invoke(Native Method) 
                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
+0

エラーログを投稿してください –

+0

これは意味がありますか? – Ichthyocentaurs

答えて

0

あなたはしましたをセットする テキストビューのリスナーをドラッグして、メソッドの線形レイアウトにテキストビューをキャストしようとしています。

+0

ありがとうございました...それはうまくいきました... btw ACTION_DROPのケースで何が起こっているのか教えてください。次の4行のコードは何ですか? –

+0

ViewGroupの所有者=(ViewGroup)view.getParent(); owner.removeView(view); LinearLayoutコンテナ=(LinearLayout)layoutview; container.addView(view); –

+0

view.getParent()ビューの親ビューを返します。 owner.removeView(view)はparentおよびcontainer.addView(view)からビューを削除し、そのビューをコンテナに追加します。しかし、意味をなさない。 –

0

XMLのTextViewは次のとおりです。これはドラッグリスナーを設定する場所です。

<TextView 
     android:id="@+id/text_drop" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Drop here" 
     /> 

は今、あなたは、したがって、あなたのドラッグ&ドロップのコードを例外とは何の関係もありませんLinearLayoutへのTextViewを型キャストされています。

+0

私はあなたに同じ質問をしています。これらの4行のコードを私に説明してください。 ViewGroupの所有者=(ViewGroup)view.getParent(); owner.removeView(view); LinearLayoutコンテナ=(LinearLayout)レイアウトビュー。 container.addView(view); –

関連する問題