2015-07-19 55 views
0
package com.carterharrison.taptap; 

import android.app.Activity; 
import android.content.Context; 
import android.content.SharedPreferences; 
import android.media.MediaPlayer; 
import android.os.Vibrator; 
import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.EditText; 
import android.widget.ImageView; 
import android.widget.TextView; 


public class MainActivity extends ActionBarActivity { 
    SharedPreferences SP; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     SP = getSharedPreferences("points", Context.MODE_PRIVATE); 


    } 




     public int x = SP.getInt("points", -1); 



    public void val1(View v){ 

     x = (x+1); 
     con(); 

    } 

    public void val2(View v){ 

     x = (x+2); 
     con(); 
    } 

    public void val3(View v){ 

     x = (x+5); 
     con(); 
     } 

    public void val4(View v){ 

     x = (x+10); 
    con(); 

} 

public void val5(View v){ 

    x = (x+25); 
    con(); 

} 

public void val6(View v){ 

    x = (x+50); 
    con(); 

} 

public void con(){ 
    SharedPreferences.Editor editor = SP.edit(); 

    if (x > 1000) { 
     ImageView img5=(ImageView)findViewById(R.id.imageView5); 
     img5.setVisibility(View.VISIBLE);} 
    if (x > 625) { 
     ImageView img4=(ImageView)findViewById(R.id.imageView4); 
     img4.setVisibility(View.VISIBLE);} 
    if (x > 300) { 
     ImageView img3=(ImageView)findViewById(R.id.imageView3); 
     img3.setVisibility(View.VISIBLE);} 
    if (x > 100) { 
     ImageView img2=(ImageView)findViewById(R.id.imageView2); 
     img2.setVisibility(View.VISIBLE);} 
    if (x > 30) { 
     ImageView img=(ImageView)findViewById(R.id.ImageView); 
     img.setVisibility(View.VISIBLE);} 

    TextView tv = (TextView)findViewById(R.id.val); 
    tv.setText("" + x); 


    editor.putInt("score", x); 
    editor.apply(); 

} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 

}我有SharedPreferences保存一個int

我試圖挽救INT x的值的問題。我的應用程序在啓動時崩潰,有人可以幫助我,我是新來java和SharedPreferences,謝謝。我可能會做這一切都錯了,我需要幫助保存x的價值。

+1

是否有異常堆棧跟蹤? – aquaraga

+0

對不起,我不知道那是什麼。 –

+0

如果您使用IDE來運行該應用程序,什麼消息被記錄到控制檯窗口? – aquaraga

回答

1

要設置intSharedPReference

public void SaveInt(String key, int value){ 
    SharedPreferences sharedPreferences = getSharedPreferences("points", Activity.MODE_PRIVATE) 
    SharedPreferences.Editor editor = sharedPreferences.edit(); 
    editor.putInt(key, value); 
    editor.commit(); 
} 

爲了得到詮釋值從SharedPreference

public void LoadInt(String key){ 
    SharedPreferences sharedPreferences = getSharedPreferences("points", Activity.MODE_PRIVATE) 
    int savedValue = sharedPreferences.getInt(key, 0); 
} 

編輯:

每當你想在保存INT價值共享偏好只需撥打 如下:

SaveInt(「my_int」,5);

每當要檢索的值相同INT:

INT mScore = LoadInt( 「my_int」);

希望它能幫助你。

+0

但是我如何調用這些函數? –

+0

@CarterHarrison,我編輯了我的答案,請檢查。 –