2017-06-22 53 views
0

我正在嘗試做一個基本的水龍頭計數器應用程序,它將計數器值存儲到sharedpreference。當我運行應用程序時,應用程序與沒有sharedpreference的tap計數器一樣正常工作,但是當我引入sharedpreference時,應用程序崩潰。自來水櫃臺應用程序中Sharedpreference介紹導致應用程序崩潰

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 =中間等它是在代碼中,這我不認爲有問題,但如果你看到它並沒有傷害。

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); 
     } 
    }); 

    } 
} 

感謝您的幫助提前! :-)

回答

1

TextViewint是不好的朋友這樣使用:

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)

設置要使用要顯示的文字字符串資源標識符

這樣既然不會有資源e找到匹配,因爲它只是一個隨機數傳遞給你,所以例外

+1

謝謝,它的工作 – Alex