SharedPreferences保存bg颜色错误

时间:2017-01-19 10:46:13

标签: java android android-sharedpreferences

嗨我有保存bg颜色的问题idont知道whay他没有得到代码:

        mlinearLayout.setBackgroundColor(Color.colourValue);
他说红色的“colorvalue”。我在这里需要你的帮助java代码:

import android.content.SharedPreferences;
import android.graphics.Color;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button btnred,btngreen,btnblue,btnpluse,btnsave;
    private LinearLayout mlinearLayout;
    private TextView tv;
    int point = 0;
    public SharedPreferences sp,prefs;
    private int progress;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        SharedPreferences prefs = getSharedPreferences("save", MODE_PRIVATE);
        String textValue= prefs.getString("textValue", "0");

        mlinearLayout=(LinearLayout)findViewById(R.id.activity_main);
        SharedPreferences sp = getSharedPreferences("saveColour", MODE_PRIVATE);
        String colourValue = sp.getString("colourValue", "WHITE");
        mlinearLayout.setBackgroundColor(Color.colourValue);

        btnred = (Button) findViewById(R.id.cred);
        btnblue = (Button) findViewById(R.id.cblue);
        btngreen = (Button) findViewById(R.id.cgreen);
        btnpluse = (Button)findViewById(R.id.btnadd);
        btnsave = (Button)findViewById(R.id.save);
        tv=(TextView)findViewById(R.id.tv) ;
        tv.setText(textValue);
        btnred.setOnClickListener(this);
        btnsave.setOnClickListener(this);
        btnpluse.setOnClickListener(this);
        btnblue.setOnClickListener(this);
        btngreen.setOnClickListener(this);


        }



    @Override
    public void onClick(View v) {
        if (btnred == v){
            mlinearLayout.setBackgroundColor(Color.RED);
            SharedPreferences.Editor editor = getSharedPreferences("saveColour", MODE_PRIVATE).edit();
            editor.putString("colourValue","RED");
            editor.commit();

        }
        else if (btngreen == v){
            mlinearLayout.setBackgroundColor(Color.GREEN);
            SharedPreferences.Editor editor = getSharedPreferences("saveColour", MODE_PRIVATE).edit();
            editor.putString("colourValue","GREEN");
            editor.commit();
        }
        else if (btnblue == v){
            mlinearLayout.setBackgroundColor(Color.BLUE);
            SharedPreferences.Editor editor = getSharedPreferences("saveColour", MODE_PRIVATE).edit();
            editor.putString("colourValue","BLUE");
            editor.commit();


        }
        else if (btnpluse== v){
            point++;
            tv.setText("" + point);

        }
        else if (btnsave == v){
            Toast.makeText(this,"btn clickd",Toast.LENGTH_LONG).show();
            SharedPreferences.Editor editor = getSharedPreferences("save", MODE_PRIVATE).edit();
            editor.putString("textValue",tv.getText().toString());
            editor.commit();
        }
    }
}

这是问题的图像 image of the error

1 个答案:

答案 0 :(得分:1)

用给定的更新替换您的代码:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button btnred,btngreen,btnblue,btnpluse,btnsave;
    private LinearLayout mlinearLayout;
    private TextView tv;
    int point = 0;
    public SharedPreferences sp,prefs;
    private int progress;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        SharedPreferences prefs = getSharedPreferences("save", MODE_PRIVATE);
        String textValue= prefs.getString("textValue", "0");

        mlinearLayout=(LinearLayout)findViewById(R.id.activity_main);
        SharedPreferences sp = getSharedPreferences("saveColour", MODE_PRIVATE);
        int colourValue = sp.getInt("colourValue", Color.WHITE);
        mlinearLayout.setBackgroundColor(colourValue);

        btnred = (Button) findViewById(R.id.cred);
        btnblue = (Button) findViewById(R.id.cblue);
        btngreen = (Button) findViewById(R.id.cgreen);
        btnpluse = (Button)findViewById(R.id.btnadd);
        btnsave = (Button)findViewById(R.id.save);
        tv=(TextView)findViewById(R.id.tv) ;
        tv.setText(textValue);
        btnred.setOnClickListener(this);
        btnsave.setOnClickListener(this);
        btnpluse.setOnClickListener(this);
        btnblue.setOnClickListener(this);
        btngreen.setOnClickListener(this);


        }



    @Override
    public void onClick(View v) {
        if (btnred == v){
            mlinearLayout.setBackgroundColor(Color.RED);
            SharedPreferences.Editor editor = getSharedPreferences("saveColour", MODE_PRIVATE).edit();
            editor.putInt("colourValue", Color.RED);
            editor.commit();

        }
        else if (btngreen == v){
            mlinearLayout.setBackgroundColor(Color.GREEN);
            SharedPreferences.Editor editor = getSharedPreferences("saveColour", MODE_PRIVATE).edit();
            editor.putInt("colourValue", Color.GREEN);
            editor.commit();
        }
        else if (btnblue == v){
            mlinearLayout.setBackgroundColor(Color.BLUE);
            SharedPreferences.Editor editor = getSharedPreferences("saveColour", MODE_PRIVATE).edit();
            editor.putInt("colourValue", Color.BLUE);
            editor.commit();


        }
        else if (btnpluse== v){
            point++;
            tv.setText("" + point);

        }
        else if (btnsave == v){
            Toast.makeText(this,"btn clickd",Toast.LENGTH_LONG).show();
            SharedPreferences.Editor editor = getSharedPreferences("save", MODE_PRIVATE).edit();
            editor.putString("textValue", tv.getText().toString());
            editor.commit();
        }
    }
}
希望能帮到你!