问题
I read about the new SwitchCompat
that has been introduced to implement the Switch widget in Android 5.0. I tried using the same but I am not able to see the drawable thumb image as seen in below image.
My XML code is as follows,
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp">
<android.support.v7.widget.SwitchCompat
android:id="@+id/sampleSwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:showText="false"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:text="@string/action" />
<TextView
android:id="@+id/switchStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/sampleSwitch"
android:layout_marginTop="22dp"
android:text="@string/status"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
I am able to see the thumb image in preview design (graphical layout tab in eclipse) for the above layout but when I run my code I dont see the image.
Preview design
This is the exception I get
java.lang.NullPointerException: Attempt to invoke virtual method '
boolean android.graphics.drawable.Drawable.getPadding(android.graphics.Rect)
' on a null object reference
Please can someone help solve the problem?
回答1:
This is a known issue and you should provide the thumb and track:
android:thumb="@drawable/thumb"
android:track="@drawable/bg"
or
SwitchCompat switchCompat = (SwitchCompat)findViewById(R.id.sampleSwitch);
switchCompat.setThumbResource(R.drawable.apptheme_switch_thumb_holo_light);
switchCompat.setTrackResource(R.drawable.apptheme_switch_track_holo_light);
you can use this link to customize it.
Layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp">
<android.support.v7.widget.SwitchCompat
android:id="@+id/sampleSwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:textOff="OFF"
android:textOn="ON"
android:text="Toggle Me"
android:clickable="true"
android:checked="true" />
</RelativeLayout>
And the codes
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SwitchCompat switchCompat = (SwitchCompat)findViewById(R.id.sampleSwitch);
switchCompat.setThumbResource(R.drawable.apptheme_switch_thumb_holo_light);
switchCompat.setTrackResource(R.drawable.apptheme_switch_track_holo_light);
}
}
回答2:
It's a bug with corrupt file in drawable-hdpi on AppCompat https://code.google.com/p/android/issues/detail?id=78262
To fix it, juste override it with this 2 files https://github.com/lopespm/quick-fix-switchcompat-resources Add it on your directory drawable-hdpi
XML
<android.support.v7.widget.SwitchCompat
android:id="@+id/dev_switch_show_dev_only"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
And nothing was necessary on Java
来源:https://stackoverflow.com/questions/27568560/switchcompat-not-showing-thumb-image-on-android-5-0-lollipop-emulator