调色板类在某些图像上崩溃我的应用程序

时间:2015-04-26 01:48:29

标签: android crash android-palette

我正在制作Google I / O 2014音乐播放器,而我在专辑封面中的色彩提取方面遇到了麻烦。这是我的专辑屏幕类:

package com.animbus.music;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PorterDuff;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.graphics.Palette;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageButton;
import android.widget.ImageView;

import java.security.spec.PSSParameterSpec;


public class albums_activity extends AppCompatActivity {

    public Bundle b;
    public String AlbumName;
    public String AlbumArtist;
    public int AlbumArt;
    public int PrimaryColor;
    public int AccentColor;
    public int TitleTextColor;
    public int SubtitleTextColor;
    public int fabIcon;
    public Palette palette;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_albums_activity);
        //Get intent data and set it to something easier to handle and use
        b = getIntent().getExtras();
        AlbumName = b.getString("ALBUM_NAME");
        AlbumArtist = b.getString("ALBUM_ARTIST");
        AlbumArt = b.getInt("ALBUM_ART");
        Drawable AlbumArtDrawable = getDrawable(AlbumArt);
        Bitmap albumArt = BitmapFactory.decodeResource(getResources(), AlbumArt);

        ImageView albumArtView = (ImageView) findViewById(R.id.albums_activity_albumart);
        ImageButton playAll = (ImageButton) findViewById(R.id.play_all_FAB);
        //Make sure that the colors aren't null
        PrimaryColor = getResources().getColor(R.color.accent);
        AccentColor = getResources().getColor(R.color.accent);
        TitleTextColor = getResources().getColor(R.color.accent);
        SubtitleTextColor = getResources().getColor(R.color.accent);
        fabIcon = getResources().getColor(R.color.accent);

        //Convert Palette to resources
        palette = Palette.from(albumArt).generate();

        PrimaryColor = palette.getVibrantSwatch().getRgb();
        AccentColor = palette.getLightVibrantSwatch().getRgb();
        TitleTextColor = palette.getVibrantSwatch().getTitleTextColor();
        SubtitleTextColor = palette.getVibrantSwatch().getBodyTextColor();
        fabIcon = palette.getLightVibrantSwatch().getTitleTextColor();

        //Toolbar, setting toolbar as Actionbar,Setting the back arrow to be shown, and setting the title to nothing
        android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.album_toolbar);
        android.support.v7.widget.Toolbar infoToolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.album_info_toolbar);
        setSupportActionBar(toolbar);
        //noinspection ConstantConditions
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setTitle("");
        //Sets the title to the intent's data
        infoToolbar.setTitle(AlbumName);
        infoToolbar.setSubtitle(AlbumArtist);
        //Sets the albumart
        albumArtView.setImageResource(AlbumArt);
        //Sets the color of the Info Toolbar based on the albumart
        infoToolbar.setBackgroundColor(PrimaryColor);
        infoToolbar.setTitleTextColor(TitleTextColor);
        infoToolbar.setSubtitleTextColor(SubtitleTextColor);
        //Sets accent color based on album art
        playAll.getBackground().setColorFilter(AccentColor, PorterDuff.Mode.SRC_ATOP);
        playAll.getDrawable().setColorFilter(fabIcon,PorterDuff.Mode.SRC_ATOP);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_albums_activity, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

这是我的问题。我的应用程序有三个按钮,Album,Album Alt和Album Alt 2.只有专辑alt打开。其他人扔了:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.animbus.music/com.animbus.music.albums_activity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.support.v7.graphics.Palette$Swatch.getRgb()' on a null object reference

并且应用程序崩溃了。它说问题出在:

PrimaryColor = palette.getVibrantSwatch().getRgb();

我认为可能是因为palette.getVibrantSwatch().getRgb()等于null。我不知道但请帮助,因为现在我必须禁用调色板并继续制作应用程序,但请帮助

3 个答案:

答案 0 :(得分:1)

Palette无需始终返回VibrantSwatch。在致电null之前检查getRgb()

Palette.Swatch vibrantSwatch = palette.getVibrantSwatch();
  if(vibrantSwatch != null) {
    //get rgb
  } else {
    //get another swatch
}

您还可以使用getSwatches()从Palette获取所有样本。然后,要获得最常用的颜色,请选择具有最大像素数的样本。您可以通过在样本上调用getPopulation()来获得像素数。

答案 1 :(得分:0)

调色板可能无法获得与您尝试获取的样本的条件匹配的颜色,因此您应该执行检查以确保样本在执行之前不为空

答案 2 :(得分:0)

我遇到了同样的问题,得出的结论是,palette!!.vibrantSwatch有时会为某些图像返回null:

添加详细代码段以供将来参考

 Palette.from(bitmap).generate { palette ->
            // Use generated instance
            val vibrant = palette!!.vibrantSwatch
            if (vibrant != null) {
                // If we have a vibrant color
                // update the title TextView
                val mutedColor = palette.getMutedColor(R.attr.colorPrimary); 
                //mutedColor = palette.vibrantSwatch!!.getRgb()
                collapseToolbar.setBackgroundColor(mutedColor)
                collapseToolbar.setStatusBarScrimColor(palette.getDarkMutedColor(mutedColor))
                collapseToolbar.setContentScrimColor(palette.getMutedColor(mutedColor))

            }