在点击计数器应用中引入 Sharedpreference 导致应用崩溃 [英] Introduction of Sharedpreference in tap counter app causes app crash

查看:26
本文介绍了在点击计数器应用中引入 Sharedpreference 导致应用崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试做一个基本的点击计数器应用程序,它将计数器值存储到 sharedpreference.当我运行应用程序时,它与没有共享首选项的点击计数器一样正常工作,但是当我引入共享首选项时,应用程序崩溃了.

I'm trying to do a basic tap counter app that stores counter value to sharedpreference. App works as normal as a tap counter without sharedpreference when I run it, but when I introduce sharedpreference, the app crashes.

~MainActivity~

public class MainActivity extends AppCompatActivity {

public static final String PREFS = "examplePrefs";
Button btn_tap, btn_trophy, btn_reset;
TextView tv_tapped, tv_times, tv_tap1;
private int counter = 0;

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

    btn_tap = (Button) findViewById(R.id.btn_tap);
    btn_trophy = (Button) findViewById(R.id.btn_trophy);
    btn_reset = (Button) findViewById(R.id.btn_reset);
    tv_tap1 = (TextView) findViewById(R.id.tv_tap1);
    tv_tapped = (TextView) findViewById(R.id.tv_tapped);
    tv_times = (TextView) findViewById(R.id.tv_times);

    SharedPreferences example = getSharedPreferences(PREFS, counter);
    int valueString = example.getInt("userValue", 0);
    tv_tapped.setText(valueString);

    btn_tap.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            counter +=1;
            tv_tapped.setText(counter);
            tv_tapped.setTextColor(Color.BLUE);

            SharedPreferences example = getSharedPreferences(PREFS, counter);
            SharedPreferences.Editor editor = example.edit();
            editor.putInt("userValue", counter);
            editor.commit();

        }
    });
    btn_reset.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            counter = 0;
            tv_tapped.setText(counter);
            tv_tapped.setTextColor(Color.RED);
        }
    });

    btn_trophy.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(MainActivity.this, TrophyActivity.class);
            startActivity(i);
        }
    });

  }
}

第二个活动从 sharedpreference 中获取值并显示奖杯级别名称,例如 1-25 = 初学者,26-50 = 中级等等,它在代码中,我认为这没有问题但没有伤害如果你看到了.

The second activity gets the value from sharedpreference and displays the trophy level name, like 1-25 = beginner, 26-50 = intermediate etc it's in the code, which i don't think has a problem but doesn't hurt if you see it.

~奖杯活动~

public class TrophyActivity extends AppCompatActivity {

public static final String PREFS = "examplePrefs";
Button btn_back, btn_exit;
TextView tv_congrats, tv_received, tv_trophyName, tv_trophy;
ImageView iV_trophy;

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

    btn_back = (Button) findViewById(R.id.btn_back);
    btn_exit = (Button) findViewById(R.id.btn_exit);
    tv_congrats = (TextView) findViewById(R.id.tv_congratulations);
    tv_received = (TextView) findViewById(R.id.tv_received);
    tv_trophyName = (TextView) findViewById(R.id.tv_trophy_name);
    tv_trophy = (TextView) findViewById(R.id.tv_trophy);
    iV_trophy = (ImageView) findViewById(R.id.iV_trophy);

    SharedPreferences example = getSharedPreferences(PREFS, MODE_PRIVATE);
    int valueString = example.getInt("userValue", 0);

    if (valueString<=25){
        tv_trophyName.setText("Beginner");
        iV_trophy.setImageResource(R.drawable.ic_beginner);
    }else if (valueString<=50){
        tv_trophyName.setText("Intermediate");
        iV_trophy.setImageResource(R.drawable.ic_ntermediate);
    }else if (valueString<=75){
        tv_trophyName.setText("Semi Jumbo");
        iV_trophy.setImageResource(R.drawable.ic_semi_jumbo);
    }else if (valueString<=100){
        tv_trophyName.setText("Jumbo");
        iV_trophy.setImageResource(R.drawable.ic_jumbo);
    }

    btn_back.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            TrophyActivity.super.onBackPressed();
        }
    });

    btn_exit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            moveTaskToBack(true);
            android.os.Process.killProcess(android.os.Process.myPid());
            System.exit(1);
        }
    });

  }
}

提前感谢您的帮助!!!:-)

Thank you for your help in advance !!! :-)

推荐答案

TextViewint 不是好朋友所以用:

TextView and int are not good friends so use:

tv_tapped.setText(String.valueOf(valueString)); // will convert the int to String
tv_tapped.setText(String.valueOf(counter));     // another case 

当您传递 int 时,Android 所做的是,来自 void setText (int resid)

when you pass an int what Android does is , from void setText (int resid)

使用字符串资源标识符设置要显示的文本

Sets the text to be displayed using a string resource identifier

所以因为不会找到资源匹配,因为它只是你传递的一个随机数,因此例外

so since there will be no resource match found because it's just a random number passed by you so hence exception

这篇关于在点击计数器应用中引入 Sharedpreference 导致应用崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆