0

我试着这样做

@Override
    protected void onDestroy() {
        super.onDestroy();
        SharedPreferences.Editor editor=this.getMyView().getSp().edit();
        editor.putInt("score",this.getMyView().getScores()+this.getMyView().getScore());
        editor.commit();

它不起作用

有人可以帮我吗?

它的 MyView 类 该类正在工作 问题不存在 也不是整个类的情况 它太长了

package com.example.brickv5;
import...
public class MyView extends View {
    private ArrayList<Brick> brickList = new ArrayList<Brick>();
    private LocalTime time;
    private Brick brick;
    private Killer killer;
    private Bitmap back;
    private int score=0;
    private Timer timer;
    private SharedPreferences sp= getContext().getSharedPreferences("scores",0);
    public SharedPreferences getSp() {
        return sp;
    }
    private int scores;
    private int width=Resources.getSystem().getDisplayMetrics().widthPixels;
    private int height=Resources.getSystem().getDisplayMetrics().heightPixels;
    public MyView(Context context) {
        super(context);
        for (int i = 0; width-(width-20)/5*(i+1)>=0; i++) {
            int x = width-(width-20)/5*(i+1);
            int y = (300);
            brick = new Brick(x, y, this);
            brickList.add(brick);
        }
        back = BitmapFactory.decodeResource ( this.getResources (), R.drawable.background2);
        back=Bitmap.createScaledBitmap(back,width,height,false);
        killer = new Killer(width/2, height-height/3, 0, 0, this);
        killer.setX(killer.getX()-killer.getBitmap().getWidth()/2);
        scores=sp.getInt("score",0);
        timer=new Timer(width,100);
    }
4

0 回答 0