使用 Palette API getRgb() 方法时出现空指针异常

Getting Null Pointer exception while using Palette API getRgb() method

我无法找到解决此问题的方法请帮助我。

我正在尝试通过提取 collapseToolbarLayout 中图像中的颜色来更改工具栏的颜色,这是我的 java 代码

public class ToolbarColorDynamic extends AppCompatActivity {

private CollapsingToolbarLayout collapsingToolbarLayout;
ImageView mImageView;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_toolbar_color_dynamic);

    setupToolbar();

    collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar4);
    collapsingToolbarLayout.setTitle(getResources().getString(R.string.user_name));//to set the title
    mImageView = (ImageView) findViewById(R.id.profile_id1);


    Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.leodicaprio);
    mImageView.setImageBitmap(bm);


    setToolbarColor(bm);

}

private void setupToolbar() {
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar4);
    setSupportActionBar(toolbar);
    final ActionBar ab = getSupportActionBar();
    // Show menu icon
    if (ab != null) {

        ab.setDisplayHomeAsUpEnabled(true);
    }
}
/* This method is to set the color and to set the image*/

private void setToolbarColor( Bitmap bitmap) {

    Palette p = createPaletteSync(bitmap);
    Palette.Swatch vibrantSwatch = checkVibrantSwatch(p);

    Toolbar tb = (Toolbar) findViewById(R.id.toolbar);
    tb.setBackgroundColor(vibrantSwatch.getRgb());//this is the line am getting null pointer exception
    tb.setTitleTextColor(vibrantSwatch.getTitleTextColor());
}

private Palette createPaletteSync(Bitmap bitmap) {
    Palette p = Palette.from(bitmap).generate();
    return  p;
}

private Palette.Swatch checkVibrantSwatch(Palette p){
    Palette.Swatch vibrant = p.getVibrantSwatch();
    if (vibrant != null){
        return vibrant;
    }
    return null;
}

}

我尝试了几个步骤,例如断言值不等于 null

assert vibrantSwatch != null;

还有这一步--

if(vibrantSwatch != null){
   tb.setBackgroundColor(vibrantSwatch.getRgb());
}

但似乎没有任何效果!...我也尝试使用谷歌搜索并在此处查看,但找不到解决方案。

下面是我的logcat日志

04-21 12:06:32.420 20288-20288/basavaraj.com.librarytools E/dalvikvm: Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method android.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering
04-21 12:06:37.974 20288-20288/basavaraj.com.librarytools E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{basavaraj.com.librarytools/basavaraj.com.librarytools.diff_toolbar.ToolbarColorDynamic}: java.lang.NullPointerException
                                                                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2343)
                                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
                                                                            at android.app.ActivityThread.access0(ActivityThread.java:162)
                                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
                                                                            at android.os.Handler.dispatchMessage(Handler.java:107)
                                                                            at android.os.Looper.loop(Looper.java:194)
                                                                            at android.app.ActivityThread.main(ActivityThread.java:5371)
                                                                            at java.lang.reflect.Method.invokeNative(Native Method)
                                                                            at java.lang.reflect.Method.invoke(Method.java:525)
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
                                                                            at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
                                                                            at basavaraj.com.librarytools.diff_toolbar.ToolbarColorDynamic.setToolbarColor(ToolbarColorDynamic.java:64)
                                                                            at basavaraj.com.librarytools.diff_toolbar.ToolbarColorDynamic.onCreate(ToolbarColorDynamic.java:42)
                                                                            at android.app.Activity.performCreate(Activity.java:5122)
                                                                            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
                                                                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
                                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395) 
                                                                            at android.app.ActivityThread.access0(ActivityThread.java:162) 
                                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364) 
                                                                            at android.os.Handler.dispatchMessage(Handler.java:107) 
                                                                            at android.os.Looper.loop(Looper.java:194) 
                                                                            at android.app.ActivityThread.main(ActivityThread.java:5371) 
                                                                            at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                            at java.lang.reflect.Method.invoke(Method.java:525) 
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833) 
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) 
                                                                            at dalvik.system.NativeStart.main(Native Method)

请帮忙解决这个问题。 谢谢!...

您的工具栏为空。在 setupToolbar() 方法的开头,您使用 ID R.id.toolbar4 引用工具栏,但在 setToolbarColor() 内部,您通过 R.id.toolbar

引用它

我找到了解决方案。

在 setToolbarColor() 方法中,我引用了错误的工具栏 ID 所以我只是更改了代码:-

来自----

private void setToolbarColor( Bitmap bitmap) {

Palette p = createPaletteSync(bitmap);
Palette.Swatch vibrantSwatch = checkVibrantSwatch(p);

Toolbar tb = (Toolbar) findViewById(R.id.toolbar);//This is the line i did silly mistake
tb.setBackgroundColor(vibrantSwatch.getRgb());//this is the line am getting null pointer exception
tb.setTitleTextColor(vibrantSwatch.getTitleTextColor());

}

至---

private void setToolbarColor( Bitmap bitmap) {

Palette p = createPaletteSync(bitmap);
Palette.Swatch vibrantSwatch = checkVibrantSwatch(p);

Toolbar tb = (Toolbar) findViewById(R.id.toolbar4);
tb.setBackgroundColor(vibrantSwatch.getRgb());//this is the line am getting null pointer exception
tb.setTitleTextColor(vibrantSwatch.getTitleTextColor());

}