我有一个支持fab定义如下:
<android.support.design.widget.FloatingActionButton
android:id="@+id/somefab"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
app:elevation="6dp"
app:rippleColor="@android:color/white"
/>
如果我像这样以编程方式设置背景列表,那么它可以工作:
somefab.setBackgroundTintList(ColorStateList.valueOf(Color.GRAY));
但是,这是一个支持晶圆厂,应该在较低的 API 上可见,因此我想使用支持方法:
somefab.setSupportBackgroundTintList(ColorStateList.valueOf(Color.GRAY));
在这里我已经得到了一个空指针异常。
java.lang.RuntimeException: Unable to start activity ComponentInfo{myprojectpath}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.graphics.drawable.Drawable android.graphics.drawable.Drawable$ConstantState.newDrawable()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4077)
at android.app.ActivityThread.-wrap15(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1350)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.graphics.drawable.Drawable android.graphics.drawable.Drawable$ConstantState.newDrawable()' on a null object reference
at android.graphics.drawable.LayerDrawable$ChildDrawable.<init>(LayerDrawable.java:1723)
at android.graphics.drawable.LayerDrawable$LayerState.<init>(LayerDrawable.java:1792)
at android.graphics.drawable.RippleDrawable$RippleState.<init>(RippleDrawable.java:998)
at android.graphics.drawable.RippleDrawable.createConstantState(RippleDrawable.java:989)
at android.graphics.drawable.RippleDrawable.createConstantState(RippleDrawable.java:988)
at android.graphics.drawable.LayerDrawable.mutate(LayerDrawable.java:1652)
at android.graphics.drawable.RippleDrawable.mutate(RippleDrawable.java:975)
at android.support.v7.widget.AppCompatDrawableManager.tintDrawable(AppCompatDrawableManager.java:773)
at android.support.v7.widget.AppCompatBackgroundHelper.applySupportBackgroundTint(AppCompatBackgroundHelper.java:115)
at android.support.v7.widget.AppCompatBackgroundHelper.setSupportBackgroundTintList(AppCompatBackgroundHelper.java:90)
at android.support.v7.widget.AppCompatImageButton.setSupportBackgroundTintList(AppCompatImageButton.java:99)
at myproject.myActivity$override.onCreate(myActivity.java:126)
at myproject.myActivity$override.access$dispatch(myActivity.java)
at myproject.myActivity.onCreate(myActivity.java:0)
at android.app.Activity.performCreate(Activity.java:6251)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4077)
at android.app.ActivityThread.-wrap15(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1350)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
为什么第一种方法有效,而第二种方法无效?
这应该有效,我在 API 15、21 和 23 上对其进行了测试:
ViewCompat.setBackgroundTintList(somefab, ColorStateList.valueOf(Color.GRAY));
为什么?转到 setSupportBackgroundTintList() 的源代码会显示以下内容:
/**
* This should be accessed via
* {@link android.support.v4.view.ViewCompat#setBackgroundTintList(android.view.View, ColorStateList)}
*
* @hide
*/
@Override
public void setSupportBackgroundTintList(@Nullable ColorStateList tint) {
if (mBackgroundTintHelper != null) {
mBackgroundTintHelper.setSupportBackgroundTintList(tint);
}
}
似乎 setSupportBackgroundTintList() 方法应该使用 ViewCompat.setBackgroundTintList() 间接调用。我尝试直接调用setSupportBackgroundTintList(),它在API 15和21上运行,但在API 23上崩溃,给出了与你给出的相同的NullPointerException。