2015-06-20 17 views
19

Problem, który mam, jest w zasadzie to, co można znaleźć w tym answer, ale nadal będę wyjaśniał poniżej. Testuję na urządzeniu z Androidem 5.0.1 i innym z 4.2.2. Na moim urządzeniu z 5.0.1 nie dostaję błędów i wszystko działa zgodnie z oczekiwaniami. W moim urządzeniu 4.2.2 pojawia się następujący błąd:ColorDrawable nie można przesłać do android.support.v7.widget.RoundRectDrawableWithShadow

java.lang.ClassCastException: android.graphics.drawable.ColorDrawable cannot be cast to android.support.v7.widget.RoundRectDrawableWithShadow 
     at android.support.v7.widget.CardViewEclairMr1.getShadowBackground(CardViewEclairMr1.java:159) 
     at android.support.v7.widget.CardViewEclairMr1.getMinWidth(CardViewEclairMr1.java:150) 
     at android.support.v7.widget.CardView.onMeasure(CardView.java:181) 
     at android.view.View.measure(View.java:15696) 
     at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:681) 
     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461) 
     at android.view.View.measure(View.java:15696) 
     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4851) 
     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 
     at android.view.View.measure(View.java:15696) 
     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4851) 
     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404) 
     at android.widget.LinearLayout.measureVertical(LinearLayout.java:695) 
     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588) 
     at android.view.View.measure(View.java:15696) 
     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4851) 
     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 
     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2261) 
     at android.view.View.measure(View.java:15696) 
     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2225) 
     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1290) 
     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1527) 
     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1190) 
     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4860) 
     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:766) 
     at android.view.Choreographer.doCallbacks(Choreographer.java:575) 
     at android.view.Choreographer.doFrame(Choreographer.java:542) 
     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:751) 
     at android.os.Handler.handleCallback(Handler.java:725) 
     at android.os.Handler.dispatchMessage(Handler.java:92) 
     at android.os.Looper.loop(Looper.java:158) 
     at android.app.ActivityThread.main(ActivityThread.java:5751) 
     at java.lang.reflect.Method.invokeNative(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:511) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1083) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:850) 
     at dalvik.system.NativeStart.main(Native Method) 

z błędem występującym w tej linii: card1.setBackgroundColor(getResources().getColor(R.color.White));

Teraz jak sugeruje połączonej odpowiedzi Próbowałem, używając setCardBackgroundColor jednak powoduje to błąd do być wyprodukowane na obu moich urządzeniach. Jakieś sugestie?

CardView Ja próbuje użyć jest android.support.v7.widget.CardView

Jest to układ wytwarzania błąd:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/MainLayout" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:background="#bbbbb5" 
android:orientation="vertical" 
android:weightSum="100"> 


<ImageView 
    android:id="@+id/imageView" 
    android:layout_width="fill_parent" 
    android:layout_height="400dp" 
    android:layout_gravity="center_vertical|top" 
    android:layout_marginBottom="50dp" 
    android:layout_marginTop="-65dp" 
    android:layout_toRightOf="@+id/prem_BACK" 
    android:layout_weight="0" 
    android:clickable="true" 
    android:paddingBottom="0dp" 
    android:src="@drawable/plane1" /> 


<android.support.design.widget.FloatingActionButton 
    android:id="@+id/fab" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignBottom="@+id/imageView" 
    android:layout_alignEnd="@+id/imageView" 
    android:layout_alignRight="@+id/imageView" 
    android:layout_marginBottom="40dp" 
    android:layout_marginRight="5dp" 
    android:src="@drawable/ic_navigate_next_white_48dp" /> 

<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/card_view1" 
    android:layout_width="fill_parent" 
    android:layout_height="50dp" 
    android:layout_gravity="center" 
    android:layout_marginLeft="10dp" 
    android:layout_marginRight="10dp" 
    android:layout_marginTop="280dp" 
    android:clickable="true" 
    android:onClick="card1ClickMethod" 
    card_view:cardCornerRadius="4dp"> 

    <TextView 
     android:id="@+id/choice_text1" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:gravity="center" 
     android:onClick="card1ClickMethod" 
     android:text="Loret ipsum sample" 
     android:textColor="#212121" /> 
</android.support.v7.widget.CardView> 

<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/card_view2" 
    android:layout_width="fill_parent" 
    android:layout_height="50dp" 
    android:layout_gravity="center" 
    android:layout_marginLeft="10dp" 
    android:layout_marginRight="10dp" 
    android:layout_marginTop="340dp" 
    android:clickable="true" 
    android:onClick="card2ClickMethod" 
    card_view:cardCornerRadius="4dp"> 

    <TextView 
     android:id="@+id/choice_text2" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:clickable="true" 
     android:gravity="center" 
     android:onClick="card2ClickMethod" 
     android:textColor="#212121" /> 
</android.support.v7.widget.CardView> 

<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/card_view3" 
    android:layout_width="fill_parent" 
    android:layout_height="50dp" 
    android:layout_gravity="center" 
    android:layout_marginLeft="10dp" 
    android:layout_marginRight="10dp" 
    android:layout_marginTop="400dp" 
    android:clickable="true" 
    android:onClick="card3ClickMethod" 
    card_view:cardCornerRadius="4dp"> 

    <TextView 
     android:id="@+id/choice_text3" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:clickable="true" 
     android:gravity="center" 
     android:onClick="card3ClickMethod" 
     android:textColor="#212121" /> 
</android.support.v7.widget.CardView> 

<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/card_view4" 
    android:layout_width="fill_parent" 
    android:layout_height="50dp" 
    android:layout_gravity="center" 
    android:layout_marginLeft="10dp" 
    android:layout_marginRight="10dp" 
    android:layout_marginTop="460dp" 
    android:clickable="true" 
    android:onClick="card4ClickMethod" 
    card_view:cardCornerRadius="4dp"> 

    <TextView 
     android:id="@+id/choice_text4" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:clickable="true" 
     android:gravity="center" 
     android:onClick="card4ClickMethod" 
     android:textColor="#212121" /> 
</android.support.v7.widget.CardView> 


<TextView 
    android:id="@+id/resulttext3" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/progressBar1" 
    android:layout_alignParentStart="true" 
    android:layout_gravity="center_vertical|left" 
    android:layout_marginBottom="5dp" 
    android:layout_marginLeft="5dp" 
    android:textSize="15dp" /> 

<ProgressBar 
    android:id="@+id/progressBar1" 
    style="?android:attr/progressBarStyleHorizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:layout_alignTop="@+id/fab" 
    android:layout_gravity="right|center_vertical" 
    android:layout_marginTop="20dp" /> 

<ImageView 
    android:id="@+id/prem_BACK" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_marginLeft="0dp" 
    android:layout_marginTop="0dp" 
    android:src="@drawable/background4" 
    android:visibility="gone" /> 

Po przeczytaniu przez issue described here zauważyłem, że został już zamknięty . O ile mi wiadomo, używam najnowszej wersji narzędzi platformy i narzędzi sdk i nadal mam ten problem.

Po kilku dalszych testach odkryłem, że emulator, którego używam, nie ma GooglePlayServices. Obecnie nie mam dostępu do fizycznego urządzenia z mniej niż Androidem 6.0 do przetestowania. Czy jest możliwe, że brak usługi GooglePlayServices może spowodować ten problem?

+0

można zakładać błędu, który otrzymasz po użyciu setCardBackgroundColor – nandeesh

+0

Ten sam błąd co już opublikowany – ez4nick

+0

czy możesz opublikować xml z kartą? – nandeesh

Odpowiedz

1

Można użyć:

card1.setBackgroundColor(Color.rgb(255, 255, 255)); 

zamiast:

card1.setBackgroundColor(getResources().getColor(R.color.White)); 
+0

Powoduje to ten sam błąd co poprzednio na moim urządzeniu 4.2.2, ale nie na moim urządzeniu 5.0.1. Przepraszam za literówkę. – ez4nick

+0

Mhmm, próbowałem go na moim urządzeniu 5.1.1, działa! –

+0

Przepraszamy za literówkę. Odwróć się. Działa na 5.0.1, nie w wersji 4.2.2. – ez4nick

2

To działa dla mnie cardview wewnątrz recycleview

((CardView)viewholder.itemView).setCardBackgroundColor(Color.WHITE); 
1

najprostszy sposób, aby zrobić to, co chcesz używając filtra koloru, chyba. Próbowałem to na 4.2.2 i działa dobrze.

cardView.getBackground().setColorFilter(Color.BLUE, PorterDuff.Mode.MULTIPLY); 
1

Korzystając setBackgroundColor(int color) jesteś rzeczywiście wywołanie metody na View, przełączanie tła. CardView spodziewa się, że po prostu nadpisałeś numer RoundRectDrawableWithShadow. Stąd wyjątek.

CardView view = (CardView) findViewById(R.id.card); 
view.setCardBackgroundColor(Color.RED); 

Pamiętaj, aby zadzwonić pod numer cardView.setCardBackgroundColor(int) i to zadziała. Działa w wersji 4.2.2, a przeglądając kod źródłowy cardView, jestem przekonany o innych apisach. Jeśli to również prowadzi do awarii, musi to być inny powód.

+0

Testowałeś to na urządzeniu fizycznym lub na emulatorze? Testowałem tylko na emulatorze i * nie * urządzenie fizyczne, ponieważ nie mam do niego dostępu. Na moim emulatorze to nie rozwiązuje problemu. – ez4nick

+0

@ ez4nick testowane na Samsung Galaxy S2 z Api 19 ... i uważam, że jest to dość osobliwe, że to nie zadziała w emulatorze, zwłaszcza że nie ma "ColorDrawable", który mógłby spowodować awarię przy użyciu tej metody. Czy na pewno nie ustawiasz koloru w wielu pozycjach? –

1

Prawdopodobnie zostało to rozwiązane za pomocą funkcji PublicRoundRrawDrawableWithShadow.
Utwórz PublicRoundRectDrawableWithShadow.

package android.support.v7.widget; 

import android.content.res.Resources; 

public class PublicRoundRectDrawableWithShadow extends RoundRectDrawableWithShadow { 
    public PublicRoundRectDrawableWithShadow(Resources resources, CardView cardView, int backgroundColor) { 
     super(resources, backgroundColor, cardView.getRadius(), cardView.getCardElevation(), cardView.getMaxCardElevation()); 
    } 
} 

I tak jak w setBackground().

final CardView cardView = (CardView) findViewById(R.id.card_view); 
    if (Build.VERSION_CODES.LOLLIPOP <= Build.VERSION.SDK_INT) { 
     cardView.setBackgroundColor(Color.RED); 
    } else { 
     cardView.setBackgroundDrawable(new PublicRoundRectDrawableWithShadow(getResources(), cardView, Color.RED)); 
    } 
2

Można też zachować obie wersje android szczęśliwy, dając im to, co chcą zamiast próbować zmusić i rzucić coś:

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP_MR1) { 
     // below lillipop 
     view.setCardBackgroundColor(Color.WHITE); 
     // working for lower questions as per another answer posted here 
    } else { 
     // lollipop and above 
     card1.setBackgroundColor(getResources().getColor(R.color.White)); 
     // known to be working for lillipop as per your question 
} 

Zasadniczo wezwanie różne i metody pracy dla każdego wersje Androida:.

Aby prawidłowo zidentyfikować wersje android powyżej i poniżej użytku lizak LILLIPOP_MR1 (Soruce)

0

Spróbuj poniżej określonej linii ustawić background color z card view object:

cardView.setCardBackgroundColor(Color.WHITE); 
Powiązane problemy