Java 调色板(Android支持v22)不';不生成样例

Java 调色板(Android支持v22)不';不生成样例,java,android,android-activity,android-5.0-lollipop,palette,Java,Android,Android Activity,Android 5.0 Lollipop,Palette,首先,我从未使用过Palette,当我想开始使用它时,我看到所有教程和博客都在谈论v21而不是v22 我的问题是,我有一个样例(位于样例数组[palette.getSwatches().get(0)]的位置0),它总是返回黑色(标题和正文),下面是一个示例,我使用这个示例,可能是错误的,但我复制了这个示例 当我启动此应用程序时(我有一个包含textView和ImageView的活动),它会关闭并显示以下内容 Caused by: java.lang.NullPointerException:

首先,我从未使用过Palette,当我想开始使用它时,我看到所有教程和博客都在谈论v21而不是v22

我的问题是,我有一个样例(位于样例数组[palette.getSwatches().get(0)]的位置0),它总是返回黑色(标题和正文),下面是一个示例,我使用这个示例,可能是错误的,但我复制了这个示例

当我启动此应用程序时(我有一个包含textView和ImageView的活动),它会关闭并显示以下内容

 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.support.v7.graphics.Palette$Swatch.getBodyTextColor()' on a null object reference
        at com.alfondo.projectpalette.MainActivity.onCreate(MainActivity.java:33)"

编辑:这是我的绘图工具。(我不能发布imgs)

只需检查pallete是否为空即可

 public void onGenerated(Palette palette) {
        if(palette!=null){
            text.setTextColor(palette.getVibrantSwatch().getBodyTextColor());
            text.setBackgroundColor(palette.getVibrantSwatch().getTitleTextColor());
        }
}

在将颜色分配给视图之前,只需检查样例是否为空而不是调色板

Palette.from(BitmapFactory.decodeResource(getResources(),R.drawable.prueba2).generate(new Palette.PaletteAsyncListener() {
      public void onGenerated(Palette palette) {
           Palette.Swatch vibrantSwatch = palette.getVibrantSwatch();
           if(vibrantSwatch != null){
              text.setTextColor(vibrantSwatch.getBodyTextColor());
              text.setBackgroundColor(vibrantSwatch.getTitleTextColor())
           }
      }
});

首先创建图像的位图,然后使用毕加索调色板获取图像的颜色,并将该颜色设置为可折叠工具栏

  // get bitmap of image and generate toolbar color by passing bitmap to palette


     Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.nature);
                Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() {
        // get muted color from bitmap using palette and set this to collapsible toolbar

        @Override
                    public void onGenerated(Palette palette) {
                        collapsingToolbarLayout.setContentScrimColor(palette.getMutedColor(getResources().getColor(R.color.colorPrimary)));
                            collapsingToolbarLayout.setStatusBarScrimColor(palette.getMutedColor(getResources().getColor(R.color.colorPrimaryDark)));
                        }
                    });

嗨,我试过了,但没用,因为我有一个调色板和一个黑色主体和黑色文本的样例。我的问题是我需要生成样例,我想我的问题不是代码,而是图像
  // get bitmap of image and generate toolbar color by passing bitmap to palette


     Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.nature);
                Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() {
        // get muted color from bitmap using palette and set this to collapsible toolbar

        @Override
                    public void onGenerated(Palette palette) {
                        collapsingToolbarLayout.setContentScrimColor(palette.getMutedColor(getResources().getColor(R.color.colorPrimary)));
                            collapsingToolbarLayout.setStatusBarScrimColor(palette.getMutedColor(getResources().getColor(R.color.colorPrimaryDark)));
                        }
                    });