Search code examples
javaandroidandroid-palette

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


Am not able figure out the solution for this problem Please help me.

Am trying to change the color of the toolbar by extracting the color in an image inside the collapseToolbarLayout here is my java code

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;
}

}

I tried several steps like assert the value to not equal to null

assert vibrantSwatch != null;

and this step too--

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

But Nothing seems to work!... I tried googling and check here too but couldn't find a solution for this.

Below is my logcat log

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.access$600(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.access$600(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)

Please help solve this issue. Thank you!..


Solution

  • Your Toolbar is null. At the beginning in your setupToolbar() Method you reference the toolbar with the ID R.id.toolbar4 but inside of setToolbarColor() you reference it by R.id.toolbar