在CheckedtextView中更改drawable

时间:2017-07-24 02:15:17

标签: java android checkedtextview

是否可以在我的一个java代码中更改CheckedTextView的图像android:drawableRight?

我尝试使用一些checkedtextview.setCompoundDrawable但它一直在崩溃。

Xml:

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/text1"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/listPreferredItemHeightSmall"
    android:checkMark="?android:attr/listChoiceIndicatorMultiple"
    android:drawableRight="@drawable/test_draw"
    android:gravity="center_vertical"
    android:paddingLeft="10dip"
    android:paddingRight="10dip"
    android:text="Hello"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:theme="@style/WhiteCheck" />

编辑:使用int

的代码错误
st3 E/AndroidRuntime: FATAL EXCEPTION: main
                                                                Process: com.carte.alex.test3, PID: 8817
                                                                java.lang.RuntimeException: Unable to start activity ComponentInfo{com.carte.alex.test3/com.carte.alex.test3.Main2Activity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.CheckedTextView.setCompoundDrawablesWithIntrinsicBounds(int, int, int, int)' on a null object reference
                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
                                                                    at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
                                                                    at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                    at android.os.Looper.loop(Looper.java:154)
                                                                    at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
                                                                 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.CheckedTextView.setCompoundDrawablesWithIntrinsicBounds(int, int, int, int)' on a null object reference
                                                                    at com.carte.alex.test3.Main2Activity.onCreate(Main2Activity.java:85)
                                                                    at android.app.Activity.performCreate(Activity.java:6662)
                                                                    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
                                                                    at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
                                                                    at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                    at android.os.Looper.loop(Looper.java:154) 
                                                                    at android.app.ActivityThread.main(ActivityThread.java:6077) 
                                                                    at java.lang.reflect.Method.invoke(Native Method)

错误2使用
          checkedtextview.setCompoundDrawables(NULL,NULL,NULL,NULL);

FATAL EXCEPTION: main
                                                                      Process: com.carte.alex.test3, PID: 10772
                                                                      java.lang.RuntimeException: Unable to start activity ComponentInfo{com.carte.alex.test3/com.carte.alex.test3.Main2Activity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.CheckedTextView.setCompoundDrawables(android.graphics.drawable.Drawable, android.graphics.drawable.Drawable, android.graphics.drawable.Drawable, android.graphics.drawable.Drawable)' on a null object reference
                                                                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
                                                                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
                                                                          at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
                                                                          at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                          at android.os.Looper.loop(Looper.java:154)
                                                                          at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                                          at java.lang.reflect.Method.invoke(Native Method)
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
                                                                       Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.CheckedTextView.setCompoundDrawables(android.graphics.drawable.Drawable, android.graphics.drawable.Drawable, android.graphics.drawable.Drawable, android.graphics.drawable.Drawable)' on a null object reference
                                                                          at com.carte.alex.test3.Main2Activity.onCreate(Main2Activity.java:86)
                                                                          at android.app.Activity.performCreate(Activity.java:6662)
                                                                          at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                                                                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
                                                                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
                                                                          at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
                                                                          at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                          at android.os.Looper.loop(Looper.java:154) 
                                                                          at android.app.ActivityThread.main(ActivityThread.java:6077) 
                                                                          at java.lang.reflect.Method.invoke(Native Method) 
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) 
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) 

0 个答案:

没有答案