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());
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
Entschuldigung das war etwas was ich versuchte. es war android: background = "# 32cd32" zum Zeitpunkt des Fehlers und jetzt zeigt es nur auf –
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