我无法弄清楚这个问题的解决方案请帮助我。
我试图通过在 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.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)
请帮助解决这个问题。谢谢!..