1
 GrallocAllocator unsupport colortype 6
 GrallocAllocator decode bitmap failed, reset to heapAllocator

这是我得到的信息。你能告诉我我做错了什么吗?

private static final String KEY_INDEX = "index";
private static final String KEY_INDEX2 = "index2";
private static final String KEY_INDEX3 = "index3";
private static final String KEY_INDEX4 = "index4";
private static final String KEY_INDEX5 = "index5";




 @Override
 protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.easy_quiz);
        correctAnswerColor = getResources().getColor(R.color.correctAnswerButton);
        incorrectAnswerColor = getResources().getColor(R.color.incorrectAnswerButton);

    if(savedInstanceState != null) {
        mCurrentIndex = savedInstanceState.getInt(KEY_INDEX, 0);

        if(savedInstanceState.getBoolean(KEY_INDEX2, false)) {
            mTrueButton.getBackground().setColorFilter(correctAnswerColor, PorterDuff.Mode.MULTIPLY);
        }

        if(savedInstanceState.getBoolean(KEY_INDEX3, false)) {
            mTrueButton.getBackground().setColorFilter(incorrectAnswerColor, PorterDuff.Mode.MULTIPLY);
        }

        if(savedInstanceState.getBoolean(KEY_INDEX4, false)) {
            mFalseButton.getBackground().setColorFilter(correctAnswerColor, PorterDuff.Mode.MULTIPLY);
        }

        if(savedInstanceState.getBoolean(KEY_INDEX5, false)) {
            mFalseButton.getBackground().setColorFilter(incorrectAnswerColor, PorterDuff.Mode.MULTIPLY);
        }
    } (...)

 @Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    Log.i(TAG, "OnSaveInstanceState");
    outState.putInt(KEY_INDEX, mCurrentIndex);
    outState.putBoolean(KEY_INDEX2, mIsTrueButtonGreen);
    outState.putBoolean(KEY_INDEX3, mIsTrueButtonRed);
    outState.putBoolean(KEY_INDEX4, mIsFalseButtonGreen);
    outState.putBoolean(KEY_INDEX5, mIsFalseButtonRed);

}

我读到设置颜色最好使用getBackground.setColorFilter。不幸的是,在这种情况下它似乎不起作用。我该怎么做是问题。

4

1 回答 1

3

您应该基于此修改您的代码:

private int correctAnswerColor, incorrectAnswerColor;
private String PREFS_NAME = "test";
private boolean isColored = false;

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

    correctAnswerColor = getResources().getColor(R.color.correct);

    //Sets color
    findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            findViewById(R.id.button).getBackground().setColorFilter(correctAnswerColor, PorterDuff.Mode.MULTIPLY);
            isColored = true;
        }
    });


    //Reads color
    SharedPreferences sp =  getSharedPreferences(PREFS_NAME,0 );
    isColored = sp.getBoolean("isValid", isColored);

    //Sets color of btt
    if(isColored) {
        findViewById(R.id.button).getBackground().setColorFilter(correctAnswerColor, PorterDuff.Mode.MULTIPLY);
    }
}

@Override
protected void onStop() {
    super.onStop();

    //Defines SharedPreferences & Saves data
    SharedPreferences sp =  getSharedPreferences(PREFS_NAME,0 );
    SharedPreferences.Editor ed = sp.edit();
    ed.putBoolean("isValid", isColored);

    //Commits edits
    ed.commit();
}
于 2017-06-10T14:14:19.230 回答