问题
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);
}
});
}
}
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.
~TrophyActivity~
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 !!! :-)
回答1:
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
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
来源:https://stackoverflow.com/questions/44707204/introduction-of-sharedpreference-in-tap-counter-app-causes-app-crash