手机睡眠时Android分数重置为零

时间:2014-01-04 18:43:29

标签: android

我的第一个stackoverflow帖子。

我在Play商店有一个免费的评分应用程序已经有一年了,但是一位用户最近指出,当手机进入睡眠状态时,分数会重置为零。他是对的。从我读过的内容来看,我猜我的问题与savedInstanceState有关。

增加/减少分数的实际代码位于savedInstanceState之外......但每当我尝试解决这个问题时,我都会收到错误。在我走这条路之前,我想我最好让专家们参与其中。事先谢谢。

我的代码:

package com.estrellastudios.rally.point.free;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Typeface;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.view.View;
import android.view.View.OnClickListener;

public class KeepScore extends Activity  implements OnClickListener {

    // declare a few variables on outer side of code
    TextView tvhs, tvvs, tvho, tvvo;
    ImageButton homeAddButton, homeSubtractButton, 
    visitorAddButton, visitorSubtractButton;
    Button reset;   
    int scoreH;
    int scoreV;





    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.keepscore);

        //declare all the buttons
        homeAddButton = (ImageButton)findViewById(R.id.home_add_button);
        visitorAddButton = (ImageButton)findViewById(R.id.visitor_add_button);
        homeSubtractButton = (ImageButton)findViewById(R.id.home_subtract);
        visitorSubtractButton = (ImageButton)findViewById(R.id.visitor_subtract);
        reset = (Button) findViewById(R.id.reset);



        // declare the home & visitor scores
        tvhs = (TextView)findViewById(R.id.home_score);
        tvvs = (TextView)findViewById(R.id.visitor_score); 


        // declare the home & visitor teams
        TextView tvho = (TextView)findViewById(R.id.home_output);
        TextView tvvo = (TextView)findViewById(R.id.visitor_output);            
        tvho.setText(getIntent().getExtras().getString("homeText"));
        tvvo.setText(getIntent().getExtras().getString("visitorText")); 


        homeAddButton.setOnClickListener((OnClickListener) this);
        homeSubtractButton.setOnClickListener((OnClickListener) this);
        visitorAddButton.setOnClickListener((OnClickListener) this);
        visitorSubtractButton.setOnClickListener((OnClickListener) this);





            // ChunkFive                  
            // team name styling and output here
            Typeface Chunkfive = Typeface.createFromAsset(getAssets(), "fonts/Chunkfive.ttf");

            tvho.setTypeface(Chunkfive);
            //tvho.setTextSize(29.f);               
            tvvo.setTypeface(Chunkfive);
            //tvvo.setTextSize(29.f);

            // ChunkFive
            // Scoring styling and output here
            tvhs.setTypeface(Chunkfive);
            //tvhs.setTextSize(120.f);        
            tvvs.setTypeface(Chunkfive);
            //tvvs.setTextSize(120.f);


            tvhs.setText(String.valueOf(scoreH));
            tvvs.setText(String.valueOf(scoreV));

    }



         // start view

            public void onClick(View v) {

            boolean showTextH = false;
            boolean showTextV = false;


             // start switch case   

            if(scoreH==0 && scoreV > 0){
                switch(v.getId())
                {
                case R.id.home_add_button: scoreH++; showTextH = true; break;
                //case R.id.home_subtract: scoreH--; showTextH = true; break;
                case R.id.visitor_add_button: scoreV++; showTextV = true; break;
                case R.id.visitor_subtract: scoreV--; showTextV = true; break;
                }
                if(showTextH)
                    tvhs.setText(String.valueOf(scoreH));
                    tvvs.setText(String.valueOf(scoreV));
                if(showTextV)
                    tvhs.setText(String.valueOf(scoreH));
                    tvvs.setText(String.valueOf(scoreV));


                // end switch case


            }

            else if(scoreV==0 && scoreH > 0){

                switch(v.getId())
                {
                case R.id.home_add_button: scoreH++; showTextH = true; break;
                case R.id.home_subtract: scoreH--; showTextH = true; break;
                case R.id.visitor_add_button: scoreV++; showTextV = true; break;
                //case R.id.visitor_subtract: scoreV--; showTextV = true; break;
                }
                if(showTextH)
                    tvhs.setText(String.valueOf(scoreH));
                    tvvs.setText(String.valueOf(scoreV));
                if(showTextV)
                    tvhs.setText(String.valueOf(scoreH));
                    tvvs.setText(String.valueOf(scoreV));



                // end switch case

            }


            else if(scoreV==0 && scoreH==0){

                switch(v.getId())
                {
                case R.id.home_add_button: scoreH++; showTextH = true; break;
                //case R.id.home_subtract: scoreH--; showTextH = true; break;
                case R.id.visitor_add_button: scoreV++; showTextV = true; break;
                //case R.id.visitor_subtract: scoreV--; showTextV = true; break;
                }
                if(showTextH)
                    tvhs.setText(String.valueOf(scoreH));
                    tvvs.setText(String.valueOf(scoreV));
                if(showTextV)
                    tvhs.setText(String.valueOf(scoreH));
                    tvvs.setText(String.valueOf(scoreV));


                // end switch case

            }

            else {

            switch(v.getId())
            {
            case R.id.home_add_button: scoreH++; showTextH = true; break;
            case R.id.home_subtract: scoreH--; showTextH = true; break;
            case R.id.visitor_add_button: scoreV++; showTextV = true; break;
            case R.id.visitor_subtract: scoreV--; showTextV = true; break;
            }
            if(showTextH)
                tvhs.setText(String.valueOf(scoreH));
                tvvs.setText(String.valueOf(scoreV));
            if(showTextV)
                tvhs.setText(String.valueOf(scoreH));
                tvvs.setText(String.valueOf(scoreV));


            // end switch case

            }





            // reset button
            reset.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    scoreH = 0;
                    scoreV = 0;
                    tvhs.setText(String.valueOf(scoreH));
                    tvvs.setText(String.valueOf(scoreV));
                }

            });

            //end reset button


            }  // end public void onClick


    @Override
        protected void onPause() {
            tvhs.setText(String.valueOf(scoreH));
            tvvs.setText(String.valueOf(scoreV));
        super.onPause();
        }

        @Override
        protected void onResume() {
            tvhs.setText(String.valueOf(scoreH));
            tvvs.setText(String.valueOf(scoreV));
        super.onResume();
        }



}       

1 个答案:

答案 0 :(得分:0)

我并非100%肯定,但我的猜测是,当手机进入睡眠状态时,您的活动正在被破坏,并且当手机重新打开时会重新创建。每次在其中一个分数更改时,我会将分数存储在共享首选项中,然后在onCreate方法中从“共享首选项”中恢复这些值。请注意,这将恢复使用的最后一个值,即使它是一个新游戏,所以你不会再用一些逻辑来处理这个。在你这样做之前,在你的onCreate中添加一个Log.d来查看这是否正在发生,但仍然只是为了安全起见。一旦应用程序处于后台,您将无法保证活动将保留在内存中。 http://developer.android.com/guide/topics/data/data-storage.html

修改

这是您的代码更新了一些日志调试输出,以便您可以开始了解LogCat中发生的事情:

package com.estrellastudios.rally.point.free;

import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

public class KeepScore extends Activity  implements OnClickListener {

// Instance Variables
TextView tvhs, tvvs, tvho, tvvo;
ImageButton homeAddButton, homeSubtractButton, 
visitorAddButton, visitorSubtractButton;
Button reset;   
int scoreH;
int scoreV;
private static final String TAG = KeepScore.class.getSimpleName(); 







@Override
public void onCreate(Bundle savedInstanceState) {
    /*
     * Now you can see every time the onCreate Method
     * is called. The onCreate method is called only when
     * the activity is created. So if this gets called instead
     * of onResume then you now that this activity has been removed
     * from memory and is being recreated.
     */
    Log.d(TAG, "onCreate Method Called");

    super.onCreate(savedInstanceState);
    setContentView(R.layout.keepscore);

    //declare all the buttons
    homeAddButton = (ImageButton)findViewById(R.id.home_add_button);
    visitorAddButton = (ImageButton)findViewById(R.id.visitor_add_button);
    homeSubtractButton = (ImageButton)findViewById(R.id.home_subtract);
    visitorSubtractButton = (ImageButton)findViewById(R.id.visitor_subtract);
    reset = (Button) findViewById(R.id.reset);



    // declare the home & visitor scores
    tvhs = (TextView)findViewById(R.id.home_score);
    tvvs = (TextView)findViewById(R.id.visitor_score); 


    // declare the home & visitor teams
    TextView tvho = (TextView)findViewById(R.id.home_output);
    TextView tvvo = (TextView)findViewById(R.id.visitor_output);            
    tvho.setText(getIntent().getExtras().getString("homeText"));
    tvvo.setText(getIntent().getExtras().getString("visitorText")); 


    homeAddButton.setOnClickListener((OnClickListener) this);
    homeSubtractButton.setOnClickListener((OnClickListener) this);
    visitorAddButton.setOnClickListener((OnClickListener) this);
    visitorSubtractButton.setOnClickListener((OnClickListener) this);





    // ChunkFive                  
    // team name styling and output here
    Typeface Chunkfive = Typeface.createFromAsset(getAssets(), "fonts/Chunkfive.ttf");

    tvho.setTypeface(Chunkfive);
    //tvho.setTextSize(29.f);               
    tvvo.setTypeface(Chunkfive);
    //tvvo.setTextSize(29.f);

    // ChunkFive
    // Scoring styling and output here
    tvhs.setTypeface(Chunkfive);
    //tvhs.setTextSize(120.f);        
    tvvs.setTypeface(Chunkfive);
    //tvvs.setTextSize(120.f);

    Log.d(TAG, "Home team score in onCreate: " + scoreH);
    tvhs.setText(String.valueOf(scoreH));
    Log.d(TAG, "Vistor team score in onCreate: " + scoreV);
    tvvs.setText(String.valueOf(scoreV));

}



// start view

public void onClick(View v) {

    boolean showTextH = false;
    boolean showTextV = false;


    // start switch case   

    if(scoreH==0 && scoreV > 0){
        switch(v.getId())
        {
        case R.id.home_add_button: scoreH++; showTextH = true; break;
        //case R.id.home_subtract: scoreH--; showTextH = true; break;
        case R.id.visitor_add_button: scoreV++; showTextV = true; break;
        case R.id.visitor_subtract: scoreV--; showTextV = true; break;
        }
        if(showTextH)
            tvhs.setText(String.valueOf(scoreH));
        tvvs.setText(String.valueOf(scoreV));
        if(showTextV)
            tvhs.setText(String.valueOf(scoreH));
        tvvs.setText(String.valueOf(scoreV));


        // end switch case


    }

    else if(scoreV==0 && scoreH > 0){

        switch(v.getId())
        {
        case R.id.home_add_button: scoreH++; showTextH = true; break;
        case R.id.home_subtract: scoreH--; showTextH = true; break;
        case R.id.visitor_add_button: scoreV++; showTextV = true; break;
        //case R.id.visitor_subtract: scoreV--; showTextV = true; break;
        }
        if(showTextH)
            tvhs.setText(String.valueOf(scoreH));
        tvvs.setText(String.valueOf(scoreV));
        if(showTextV)
            tvhs.setText(String.valueOf(scoreH));
        tvvs.setText(String.valueOf(scoreV));



        // end switch case

    }


    else if(scoreV==0 && scoreH==0){

        switch(v.getId())
        {
        case R.id.home_add_button: scoreH++; showTextH = true; break;
        //case R.id.home_subtract: scoreH--; showTextH = true; break;
        case R.id.visitor_add_button: scoreV++; showTextV = true; break;
        //case R.id.visitor_subtract: scoreV--; showTextV = true; break;
        }
        if(showTextH)
            tvhs.setText(String.valueOf(scoreH));
        tvvs.setText(String.valueOf(scoreV));
        if(showTextV)
            tvhs.setText(String.valueOf(scoreH));
        tvvs.setText(String.valueOf(scoreV));


        // end switch case

    }

    else {

        switch(v.getId())
        {
        case R.id.home_add_button: scoreH++; showTextH = true; break;
        case R.id.home_subtract: scoreH--; showTextH = true; break;
        case R.id.visitor_add_button: scoreV++; showTextV = true; break;
        case R.id.visitor_subtract: scoreV--; showTextV = true; break;
        }
        if(showTextH)
            tvhs.setText(String.valueOf(scoreH));
        tvvs.setText(String.valueOf(scoreV));
        if(showTextV)
            tvhs.setText(String.valueOf(scoreH));
        tvvs.setText(String.valueOf(scoreV));


        // end switch case

    }





    // reset button
    reset.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            scoreH = 0;
            scoreV = 0;
            tvhs.setText(String.valueOf(scoreH));
            tvvs.setText(String.valueOf(scoreV));
        }

    });

    //end reset button


}  // end public void onClick


@Override
protected void onPause() {

    /*I do no see the point in setting your
     * score text views to the score values when
     * they are already set. I would remove these
     * 2 lines of code.
     */
    tvhs.setText(String.valueOf(scoreH));
    tvvs.setText(String.valueOf(scoreV));
    super.onPause();
}

@Override
protected void onResume() {         
    Log.d(TAG, "Home team score in onResume: " + scoreH);
    tvhs.setText(String.valueOf(scoreH));
    Log.d(TAG, "Vistor team score in onResume: " + scoreV);
    tvvs.setText(String.valueOf(scoreV));
    super.onResume();
}
}