2017-02-15 2 views
0

So habe ich diesen Code und ich versuche, aktuelle Hintergrundfarbe der TextView, die angeklickt wurde erhalten.Getting Hintergrundfarbe von textView (Ich habe keine Ahnung, was los ist)

bekomme ich folgende Fehlermeldung:

java.lang.ClassCastException: android.graphics.drawable.GradientDrawable cannot be cast to android.graphics.drawable.ColorDrawable

private final class ChoiceTouchListener implements OnTouchListener { 
    public boolean onTouch(View view, MotionEvent motionEvent) { 
     if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) { 

      TextView textViewTouched = (TextView) view; 

***error at this line*** ColorDrawable textViewBackground = (ColorDrawable)textViewTouched.getBackground(); 


      //change color of textView quickly so that the shadow dragged object made down below 
      //will have a different colour then textView 
      view.setBackgroundColor(0xfff00000); 

      //setup drag 
      ClipData data = ClipData.newPlainText("", ""); 
      DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view); 
      //start dragging the item touched 
      view.startDrag(data, shadowBuilder, view, 0); 

      //change the textView colour back so the user doesn't see a change so that the 
      //shadow object is a different colour then the textView 
      view.setBackgroundColor(0xf32cd320); 
      return true; 
     } 
     else { 
      return false; 
     } 
    } 
} 

Aber wenn ich

ColorDrawable textViewBackground = (ColorDrawable)textViewTouched.getBackground(); 

Um unter

view.setBackgroundColor(0xfff00000); 

Es funktioniert ganz gut bewegen.

Ich habe keine Ahnung, warum es tut, was es tut, aber ich brauche die Hintergrundfarbe, bevor ich die neue Farbe anwende.

bearbeiten

ich auch erwähnen sollte ich habe dies in einer Funktion niedriger in meinem Code und es funktioniert in dieser Funktion nur in Ordnung.

Mehr Bearbeiten

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:id="@+id/DisplayConfig" 
    android:keepScreenOn="true"> 


    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:padding="0dp" 
    android:paddingLeft="0dp" 
    android:paddingRight="0dp"> 

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="18.5dp" 
     android:gravity="center" 
     android:paddingBottom="0dp" 
     android:text="@string/intro" /> 

    <TextView 
     android:id="@+id/option_1" 
     android:layout_width="fill_parent" 
     android:layout_margin="0dp" 
     android:background="@drawable/drag_drop_option" 
     android:gravity="left" 
     android:text="@string/option_1" 
     android:textStyle="normal" 
     android:textColor="#FFFFFF" 
     android:layout_height="150dp" /> 

    <TextView 
     android:id="@+id/option_2" 
     android:layout_width="fill_parent" 
     android:layout_margin="0dp" 
     android:background="@drawable/drag_drop_option" 
     android:gravity="left" 
     android:text="@string/option_2" 
     android:textColor="#FFFFFF" 
     android:textStyle="normal" 
     android:layout_height="150dp" /> 

    <TextView 
     android:id="@+id/option_3" 
     android:layout_width="fill_parent" 
     android:layout_margin="0dp" 
     android:background="@drawable/drag_drop_option" 
     android:gravity="left" 
     android:text="@string/option_3" 
     android:textColor="#FFFFFF" 
     android:textStyle="normal" 
     android:layout_height="151dp" /> 


    </LinearLayout> 

    <LinearLayout android:id="@+id/bottom_button_bar" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="bottom|center_horizontal" 
    android:background="#000000" 
    android:orientation="horizontal"> 

    <Button android:id="@+id/occupancy_button" 
     style="?android:attr/borderlessButtonStyle" 
     android:layout_width="wrap_content" 
     android:layout_height="72dp" 
     android:layout_weight="1" 
     android:text="Room Mode" 
     android:textColor="#FFFFFF"/> 

    </LinearLayout> 
</FrameLayout> 

und Einstellung der Ansicht für den Hörer ist so gemacht.

option1.setOnTouchListener(new ChoiceTouchListener()); 
+0

wenn' option1' von XML-ID 'android aufgeblasen zu lösen ... du hast in prev kommentiert, dass du 'android: background =" # 32cd32 "' gesetzt hast, aber ich sehe diese Zeile nirgends.speziell für 'TextViews' – snachmsm

+0

Entschuldigung das war etwas was ich versuchte. es war android: background = "# 32cd32" zum Zeitpunkt des Fehlers und jetzt zeigt es nur auf

+0

Ich glaube nicht, es war ... show code, der wirft Sie Ausnahme, excagly Code. dies mit der Zeile 'android: background =" @ drawable/drag_drop_option "' kannst du eins bekommen, mit '" # 32cd32 "' Ich glaube nicht ... – snachmsm

Antwort

0

Vielleicht haben Sie nicht ColorDrawable aber eine andere Art, so Sie bekommen ClassCast?

Blick HERE, Drawable haben viele Subklassen

Known Direct Subclasses

AnimatedVectorDrawable, AnimatedVectorDrawableCompat, BitmapDrawable, ColorDrawable, DrawableContainer, DrawableWrapper, DrawerArrowDrawable, GradientDrawable, LayerDrawable, NinePatchDrawable, PictureDrawable, RoundedBitmapDrawable, ShapeDrawable, VectorDrawable, VectorDrawableCompat

Known Indirect Subclasses

AnimatedStateListDrawable, AnimationDrawable, ClipDrawable, InsetDrawable, LevelListDrawable, PaintDrawable, RippleDrawable, RotateDrawable, ScaleDrawable, StateListDrawable, TransitionDrawable

edit:

getBackground() methodDrawable kehrt und Sie immediatelly dies (ColorDrawable) Gießen. stacktrace sagt, dass dies nicht ColorDrawable, but GradientDrawable ist, eine andere Unterklasse von Drawable

setBackgroundColor(int color) ist ColorDrawable für den Hintergrund zu schaffen, so dass, wenn Sie unten Ihre Linie mit dem Gießen zu ColorDrawable bewegen dann arbeitet

kann sie werfen keine Fehler für andere Ansichten, weil diese Hintergrundfarbe haben könnte früher in einer anderen Art und Weise eingestellt

edit2:

Sie mit instanceOf überprüfen können, aber dies ist nicht eine effiziente und optimale Art und Weise id = "@ + id/option_1" `dann hat es` Drawable` Ressource `drag_drop_option`, nicht eine Farbe: Ihr Problem ...

if(! textViewTouched.getBackground() instanceOf ColorDrawable) 
    textViewTouched.setBackgroundColor(Color.parseColor("#32cd32")); 
ColorDrawable textViewBackground = (ColorDrawable)textViewTouched.getBackground(); 
+0

haben ** importieren android.graphics.drawable.ColorDrawable; ** aber auch versucht ** importieren android.graphics.drawable. *; ** –

+0

importiert werden automatisch und spielt hier überhaupt keine Rolle ... siehe bearbeiten – snachmsm

+0

ist Gibt es eine andere Möglichkeit, ein ColorDrawable ohne Farbeinstellung zu erstellen? –

Verwandte Themen