Android:setBackgroundTintList适用于支持fab,setSupportBackgroundTintList导致npe

时间:2016-03-10 08:55:18

标签: android android-support-library floating-action-button android-support-design

我有一个支持工厂定义如下:

<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"
        />

如果我像这样以编程方式设置backgroundtintlist,那么它可以工作:

somefab.setBackgroundTintList(ColorStateList.valueOf(Color.GRAY));

然而,这是一个支持工厂,应该在较低的apis上可见,所以我想使用支持方法:

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) 

为什么第一种方法有效但不是第二种?

1 个答案:

答案 0 :(得分:6)

这应该有效,我在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);
    }
}

似乎应该使用ViewCompat.setBackgroundTintList()间接调用setSupportBackgroundTintList()方法。我试图直接调用setSupportBackgroundTintList(),它在API 15和21上运行但在API 23上崩溃,给出了你给出的相同的NullPointerException。