SharedPreferences编辑器不起作用

时间:2016-10-01 16:41:50

标签: java android sharedpreferences

我想在SharedPreferences中保护一个int,但是当我使用Editor时,我的程序总是崩溃。如果我使用调试工具,它说pref和prefEditor总是为null而highscore为0.我希望有人可以帮助我。这是我的代码:

package ordicolor.ordicolor;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Bundle;


public class MainActivity extends Activity {

    int highscore;
    SharedPreferences pref;
    SharedPreferences.Editor prefEditor;

    public MainActivity(){

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new GameView(this));
        pref = this.getSharedPreferences("high", Context.MODE_PRIVATE);
        highscore = pref.getInt("high",10);
    }

    public static int getScreenWidth() {
        return Resources.getSystem().getDisplayMetrics().widthPixels;
    }

    public static int getScreenHeight() {
        return Resources.getSystem().getDisplayMetrics().heightPixels;
    }

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

    public void setHighscore(int pHigh){
        highscore = pHigh;
    }

    public int getHighscore(){
        return highscore;
    }

    public void safe(){
        pref = getSharedPreferences("high", Context.MODE_PRIVATE);
        prefEditor = pref.edit(); //Here must be the mistake.
        prefEditor.putInt("high",this.getHighscore());
        prefEditor.commit();
    }

}
在本课程中,我使用MainActivity中的方法。我认为只有3种方法很重要(ondraw(),onTouchEvent()和zeit())。是的,我知道,我应该用英语编写我的程序,但我认为在本课程中你找不到任何重要的东西。
package ordicolor.ordicolor;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import java.util.Random;

import static java.lang.String.valueOf;

public class GameView extends SurfaceView {


    private SurfaceHolder surfaceHolder;
    MainActivity main = new MainActivity();
    private Bitmap bmp1, bmp2 ,bmp3 ,bmp4 ,bmp5 ,bmp6 ,bmp7 ,bmp8;
    private Bitmap licht;
    private int y;
    private int direction, fortschritt, tlinks, trechts, rZaehler, blinken, etagenHoehe, etage, gefahren, rndEtage, zeitzaehler, rectBreite, cdGeschwindigkeit, screenW ,screenH, sollFahren, score, restartZaehler, btnGroesse, highscore;
    private GameLoopThread theGameLoopThread;
    private Sprite theSprite, theSprite2, theSprite3, theSprite4;
    private boolean amfahren, schaden, hoch, runter, fuellen, gameOver, speichern;
    Random rnd = new Random();
    Paint paint = new Paint();

    @SuppressLint("WrongCall") public GameView(Context context) {
        super(context);
        surfaceHolder = getHolder();
        surfaceHolder.addCallback(new SurfaceHolder.Callback() {

            public void surfaceDestroyed(SurfaceHolder holder) {
                boolean retry = true;
                theGameLoopThread.setRunning(false);
                while(retry){
                    try {
                        theGameLoopThread.join();
                        retry=false;
                    }catch(InterruptedException e){

                    }
                }

            }

            public void surfaceCreated(SurfaceHolder holder) {
                theGameLoopThread.setRunning(true);
                theGameLoopThread.start();
            }

            public void surfaceChanged(SurfaceHolder holder, int format,int width, int height) {


            }

        });
        direction = 0;
        fortschritt = 0;
        tlinks = 0;
        trechts = 0;
        rZaehler = 0;
        blinken = 0;
        etagenHoehe = 0;
        etage = 0;
        gefahren = 0;
        rndEtage= 0;
        zeitzaehler = 0;
        rectBreite= 0;
        cdGeschwindigkeit = 0;
        screenW = 0;
        screenH = 0;
        sollFahren = 0;
        score = 0;
        amfahren = false;
        schaden = false;
        hoch = false;
        runter = false;
        fuellen = false;
        gameOver = false;
        speichern = false;
        screenW = main.getScreenWidth();
        screenH = main.getScreenHeight();
        btnGroesse = screenH / 6;

        bmp1 = BitmapFactory.decodeResource(getResources(),R.drawable.fahrstuhl,null);
        bmp1 = Bitmap.createScaledBitmap(bmp1,screenW, screenH * 8,true);
        etagenHoehe = bmp1.getHeight() / 8;
        gefahren = 0;
        bmp2 = BitmapFactory.decodeResource(getResources(),R.drawable.innen,null);
        bmp2 = Bitmap.createScaledBitmap(bmp2, screenW, screenH,true);
        bmp3 = BitmapFactory.decodeResource(getResources(),R.drawable.plus,null);
        bmp3 = Bitmap.createScaledBitmap(bmp3, btnGroesse, btnGroesse,true);
        bmp4 = BitmapFactory.decodeResource(getResources(),R.drawable.minus,null);
        bmp4 = Bitmap.createScaledBitmap(bmp4, btnGroesse, btnGroesse,true);
        bmp5 = BitmapFactory.decodeResource(getResources(),R.drawable.tuerlinks,null);
        bmp5 = Bitmap.createScaledBitmap(bmp5, screenW / 2, screenH,true);
        bmp6 = BitmapFactory.decodeResource(getResources(),R.drawable.tuerrechts,null);
        bmp6 = Bitmap.createScaledBitmap(bmp6, screenW / 2, screenH,true);
        bmp7 = BitmapFactory.decodeResource(getResources(),R.drawable.reparieren,null);
        bmp7 = Bitmap.createScaledBitmap(bmp7, btnGroesse, btnGroesse,true);
        bmp8 = BitmapFactory.decodeResource(getResources(),R.drawable.ordicolorgameover,null);
        bmp8 = Bitmap.createScaledBitmap(bmp8, screenW, screenH,true);
        licht = BitmapFactory.decodeResource(getResources(),R.drawable.lichtzwei,null);
        trechts = bmp2.getWidth();
        tlinks = -bmp2.getWidth() / 2;
        theSprite = new Sprite(this, bmp3, 0, screenH - btnGroesse);
        theSprite2 = new Sprite(this, bmp4, screenW - btnGroesse , screenH - btnGroesse);
        theSprite3 = new Sprite(this, bmp7, (screenW/2)-(btnGroesse/2) , screenH - btnGroesse);
        theSprite4 = new Sprite(this, bmp8, 0, 0);
        theGameLoopThread = new GameLoopThread(this);
        rndEtage();
        paint.setTextSize(50);
        y = -etagenHoehe *7;
    }


    @Override
    protected void onDraw(Canvas canvas) {
        if(!gameOver){
            paint.setColor(Color.BLACK);
            canvas.drawBitmap(bmp1, 0, y, null);
            canvas.drawBitmap(bmp5,tlinks ,0, null);
            canvas.drawBitmap(bmp6,trechts ,0, null);
            canvas.drawBitmap(bmp2, 0, 0, null);
            canvas.drawBitmap(licht, 0, 0,null);
            theSprite.onDraw(canvas);
            theSprite2.onDraw(canvas);
            if(schaden){theSprite3.onDraw(canvas);}
            canvas.drawText(valueOf(etage),bmp2.getWidth() / 2 - 12, 68, paint);
            canvas.drawText(valueOf(rndEtage),bmp2.getWidth()- 80, bmp2.getHeight() / 2 + 19, paint);
            paint.setColor(Color.GREEN);
            canvas.drawRect(0 ,0 ,rectBreite,10,paint);
        }
        else{
            paint.setColor(Color.WHITE);
            canvas.drawRect(0 ,0 ,screenW,screenH,paint);
            paint.setColor(Color.RED);
            paint.setTextSize(100);
            canvas.drawText("Game Over",100, screenH/4, paint);
            paint.setColor(Color.BLACK);
            canvas.drawText("Score: " + valueOf(score), 100, screenH/4*2, paint);
            canvas.drawText("Highscore: " + valueOf(main.getHighscore()), 100, screenH/8*7, paint);
            theSprite4.onDraw(canvas);
        }



    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        synchronized (getHolder()) {


                if (theSprite.isTouched(event.getX(), event.getY()) && etage < 7 && !runter) {
                    direction = 30;
                    gefahren = 0;
                    etage++;
                    fortschritt = 0;
                    amfahren = true;
                    hoch = true;
                    fuellen = true;
                    sollFahren += 1;
                }

                if (theSprite2.isTouched(event.getX(), event.getY()) && etage > 0 && !hoch) {
                    direction = -30;
                    gefahren = 0;
                    etage--;
                    fortschritt = 0;
                    amfahren = true;
                    runter = true;
                    fuellen = true;
                    sollFahren += 1;
                }


            if (theSprite3.isTouched(event.getX(), event.getY())) {
                rZaehler++;
            }

            if (theSprite4.isTouched(event.getX(), event.getY()) && gameOver && restartZaehler >= 150) {
                gameOver = false;
                rectBreite = 0;
                paint.setTextSize(50);
                y = -etagenHoehe * 7;
                etage = 0;
                score = 1;
                schaden = false;
                cdGeschwindigkeit = 0;
                restartZaehler = 0;
                sollFahren = 0;
                licht = BitmapFactory.decodeResource(getResources(),R.drawable.lichtzwei,null);
                main.safe();
            }

        }
        return false;
    }


    //Testet, ob der Fahrstuhl die gewünschte Etage errreicht hat und hält ihn an.
    public void fahren(){

        if(amfahren){
            if(fortschritt == 0){
                schliessen();
            }
            if(fortschritt == 1){
                schaden();
            }
            if(fortschritt == 2){
                bewegen();
            }
            if(fortschritt == 3){
                oeffnen();
            }
        }
        else{
            rndEtage();
        }

        if(gameOver){
            restartZaehler++;
        }

    }

    //schließt die Tür
    public void schliessen(){

            if(tlinks < 0){
                tlinks += 20;
                trechts -= 20;
            }
            else{
                tlinks += 4;
                trechts -= 4;
                fortschritt = 1;
                runter = true;
                hoch = true;
            }
    }


    //Steuert den Schaden des Aufzugs.
    public void schaden(){

            if(!schaden){
                if(rnd.nextInt(4 - -1) == 1){
                    schaden = true;
                    rZaehler = 0;
                }
                else{

                    fortschritt = 2;
                }
            }
            else{
                blinken++;
                fuellen = false;
                if(10 <= rZaehler){
                    schaden = false;
                    fortschritt = 2;
                    licht = BitmapFactory.decodeResource(getResources(),R.drawable.lichtzwei,null);
                }
                if(blinken ==10){
                    licht = BitmapFactory.decodeResource(getResources(),R.drawable.lichtzwei,null);
                }
                if(blinken ==20){
                    licht = BitmapFactory.decodeResource(getResources(),R.drawable.licht,null);
                    licht = Bitmap.createScaledBitmap(licht, screenW, screenH,true);
                    blinken = 0;
                }
            }

    }


    //Bewegt den Aufzug.
    public void bewegen(){


        fuellen = true;
        if(direction != 0){
            y += direction;
            gefahren += 30;
        }
        if(gefahren >= etagenHoehe * sollFahren) {
            direction = 0;
            fortschritt = 3;
            tlinks -= 4;
            trechts += 4;
            sollFahren = 0;
        }

    }


    //Öffnet die Tür.
    public void oeffnen(){

            if(tlinks > -600){
                tlinks -= 20;
                trechts += 20;
            }
            else{
                amfahren = false;
                fortschritt = 0;
                runter = false;
                hoch = false;
                fuellen = false;
            }

    }

    public void rndEtage(){
        if(etage == rndEtage && !gameOver) {
            rndEtage = rnd.nextInt(7 - -1);
            rectBreite = 0;
            cdGeschwindigkeit++;
            score++;
        }
    }


    //Hier wird die Zeit, die der Spieler zum drücken hat verwaltet.
    public void zeit(){
        if(!fuellen){
            if(zeitzaehler >= 10){
                rectBreite +=30;
                zeitzaehler = cdGeschwindigkeit;
            }
            else{
                zeitzaehler++;
            }
            if(rectBreite >= screenW){
                fortschritt = 3;
                gameOver = true;
                if(main.getHighscore() < score){
                    main.setHighscore(highscore);
                }
            }
        }
    }

}

0 个答案:

没有答案