0

I have two strings that I want to save them I wrote the code as shown below

public class MainActivity extends Activity {
    Button result;
    EditText b951, b9511, sum95, t95, p95

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {   
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        LoadPreferences();
        result = (Button)findViewById(R.id.btn1);
        b951 = (EditText)findViewById(R.id.b951);
        b9511 = (EditText)findViewById(R.id.b9511);
        sum95 = (EditText)findViewById(R.id.sum95);
        p95 = (EditText)findViewById(R.id.p95);
        t95 = (EditText)findViewById(R.id.t95);     
    }

    public void close (View v){
        SavePreferences("p2b951", b951.getText().toString());
        SavePreferences("p2b9511", b9511.getText().toString());

        finish();
    }

    private void SavePreferences(String key, String value){
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(key, value);
        editor.commit();
    }

    private void LoadPreferences(){
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
        String p1b951 = sharedPreferences.getString("p2b951", "1");
        String p1b9511 = sharedPreferences.getString("p2b9511", "1");

        b951.setText(p1b951);
        b9511.setText(p1b9511);
    }


    public void result (View v) {   
        try {       
            int ib951 = Integer.parseInt(b951.getText().toString());                
            int ib9511 = Integer.parseInt(b9511.getText().toString());          

            int iisum95 = (ib9511-ib951);
            sum95.setText(String.valueOf(iisum95));

            int isum95 = Integer.parseInt(sum95.getText().toString());

            int ip95 = Integer.parseInt(p95.getText().toString());

            int pp95 = (isum95*ip95);
            t95.setText(String.valueOf(pp95));
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}

But it seems there is a problem with this two lines:

b951.setText(p1b951);
b9511.setText(p1b9511);

I tried this

b951.setText(String.valueOf.p1b951);
b9511.setText(String.valueOf.p1b9511);

Also the problems still exist

When I open the application and when the loadpreferences is called the app will force close

The logcat show me that the error is with this two lines for sure it's just with the first line coz he didn't get the second but they are the same so they are wrong wroted

Any help??

Ambrish
  • 3,437
  • 2
  • 22
  • 38

2 Answers2

0

Change onCreate() as follows:

@Override
public void onCreate(Bundle savedInstanceState)
{

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    result = (Button)findViewById(R.id.btn1);
    b951 = (EditText)findViewById(R.id.b951);
    b9511 = (EditText)findViewById(R.id.b9511);
    sum95 = (EditText)findViewById(R.id.sum95);
    p95 = (EditText)findViewById(R.id.p95);
    t95 = (EditText)findViewById(R.id.t95);   
    LoadPreferences();  
}

This is because you are using the EditTexts in the LoadPreferences() without calling findViewById() on the EditTexts.

kgandroid
  • 5,219
  • 5
  • 34
  • 65
0

You can follow and help from like codes. such as

Setting values in Preference:

SharedPreferences.Editor editor = getSharedPreferences(MODE_PRIVATE).edit();
 editor.putString("name", "Elena");
 editor.putInt("idName", 12);
 editor.commit();

Retrieve data from preference:

SharedPreferences prefs = getSharedPreferences(MODE_PRIVATE); 
String restoredText = prefs.getString("text", null);
if (restoredText != null) {
  String name = prefs.getString("name", "No name defined");//"No name defined" is the default value.
  int idName = prefs.getInt("idName", 0); //0 is the default value.
}

it any confused about this then follow this link

Community
  • 1
  • 1
kablu
  • 569
  • 1
  • 6
  • 25