0

I wrote the App that count something by press on + button and show it in text view. know I want to save the value of text view in onstop() method and use it in onstart and show that value on text view again. Actually I did it but, when I click on + button the text view reset to 1 instead of increasing from last value. I actually save the text view value in onSaveInstanceState and use it on onRestoreInstanceState. main.java code:

public class main extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
    int salavatcount ;
    public String fonts="NAZANIN.TTF";
    TextView salavatcounter;
    Button addsalavat,sefrkon;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (savedInstanceState == null) {
           salavatcount = 0;
        }
        setContentView(R.layout.main);
        salavatcounter = (TextView) findViewById(R.id.salavatcounter);
        addsalavat = (Button) findViewById(R.id.addsalavat);
        addsalavat.setOnClickListener(this);
        sefrkon = (Button) findViewById(R.id.sefrkon);
        sefrkon.setOnClickListener(this);
        SharedPreferences setting =getSharedPreferences("setting",0);
        salavatcounter.setText(setting.getString("salavatcount", ""+0));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater menu_option_infalter = getMenuInflater();
        menu_option_infalter.inflate(R.menu.optionmenu, menu);        
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId()== R.id.about) {
            Intent intent = new Intent(main.this,about.class);
            startActivity(intent);
        }
        return super.onOptionsItemSelected(item);
    }

        @Override
    protected void onSaveInstanceState(Bundle savedInstanceState) {
            savedInstanceState.putString("YourTextViewTextIdentifier",
                salavatcounter.getText().toString()
            );
            savedInstanceState.putInt("int", salavatcount);
            super.onSaveInstanceState(savedInstanceState);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        salavatcounter.setText(savedInstanceState.getString("YourTextViewTextIdentifier"));
        salavatcount = savedInstanceState.getInt("int");
    }

    @Override
    protected void onStop() {    
        super.onStop();
        SharedPreferences setting=getSharedPreferences("setting", 0);
        SharedPreferences.Editor editor=setting.edit();
        editor.putString("salavatcount", salavatcounter.getText().toString());
        editor.commit();
    }

    public void onClick(View v) {
        if (v.getId()== R.id.addsalavat){
            salavatcount++;
            salavatcounter.setText(""+salavatcount);
        }
        else if (v.getId() == R.id.sefrkon) {
            salavatcount=0;
            salavatcounter.setText(""+salavatcount);
        }
    }
}

Edit: log

04-24 01:59:50.823: W/dalvikvm(6645): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
04-24 01:59:50.833: E/AndroidRuntime(6645): Uncaught handler: thread main exiting due to uncaught exception
04-24 01:59:50.843: E/AndroidRuntime(6645): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pishgamanit.salavatcounter/com.pishgamanit.salavatcounter.main}: java.lang.NullPointerException
04-24 01:59:50.843: E/AndroidRuntime(6645):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
04-24 01:59:50.843: E/AndroidRuntime(6645):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
04-24 01:59:50.843: E/AndroidRuntime(6645):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)
04-24 01:59:50.843: E/AndroidRuntime(6645):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
04-24 01:59:50.843: E/AndroidRuntime(6645):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-24 01:59:50.843: E/AndroidRuntime(6645):     at android.os.Looper.loop(Looper.java:123)
04-24 01:59:50.843: E/AndroidRuntime(6645):     at android.app.ActivityThread.main(ActivityThread.java:4363)
04-24 01:59:50.843: E/AndroidRuntime(6645):     at java.lang.reflect.Method.invokeNative(Native Method)
04-24 01:59:50.843: E/AndroidRuntime(6645):     at java.lang.reflect.Method.invoke(Method.java:521)
04-24 01:59:50.843: E/AndroidRuntime(6645):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
04-24 01:59:50.843: E/AndroidRuntime(6645):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
04-24 01:59:50.843: E/AndroidRuntime(6645):     at dalvik.system.NativeStart.main(Native Method)
04-24 01:59:50.843: E/AndroidRuntime(6645): Caused by: java.lang.NullPointerException
04-24 01:59:50.843: E/AndroidRuntime(6645):     at com.pishgamanit.salavatcounter.main.onCreate(main.java:29)
04-24 01:59:50.843: E/AndroidRuntime(6645):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-24 01:59:50.843: E/AndroidRuntime(6645):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
04-24 01:59:50.843: E/AndroidRuntime(6645):     ... 11 more
04-24 01:59:50.863: I/dalvikvm(6645): threadid=7: reacting to signal 3
04-24 01:59:50.863: E/dalvikvm(6645): Unable to open stack trace file '/data/anr/traces.txt': Permission denied

3 Answers 3

0

You are not setting the value of salavatcount to the value from you're shared preference. When you click the button it is being reset to 0, as that is the default value of salavatcount.

You are also saving an int value as a string.

  @Override
protected void onStop() {
    super.onStop();

    SharedPreferences setting= getSharedPreferences("setting", 0);
    SharedPreferences.Editor editor=setting.edit();
    editor.putInt("salavatcount", salavatcount);
    editor.commit();

    }

Then in your on create method, set the salavatcount value from the SharePreferences.

int salavatcount;
public String fonts="NAZANIN.TTF";
TextView salavatcounter;
Button addsalavat,sefrkon;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

   setContentView(R.layout.main);


   salavatcounter = (TextView) findViewById(R.id.salavatcounter);
   addsalavat = (Button) findViewById(R.id.addsalavat);
   addsalavat.setOnClickListener(this);
   sefrkon = (Button) findViewById(R.id.sefrkon);
   sefrkon.setOnClickListener(this);

   SharedPreferences setting = getSharedPreferences("setting", 0);

   salavatcount = setting.getInt("salavatcount", 0);

   salavatcounter.setText("" + salavatcount);



  }

Edit:

I would rewrite the code like this:

public class main extends Activity implements OnClickListener {
/** Called when the activity is first created. */
int salavatcount ;
public String fonts="NAZANIN.TTF";
TextView salavatcounter;
Button addsalavat,sefrkon;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

   setContentView(R.layout.main);

   if (savedInstanceState == null) {

       salavatcount = savedInstanceState.getInt("count");

   } else {

       SharedPreferences setting = getSharedPreferences("setting", 0);

       salavatcount = setting.getInt("salavatcount", 0);

   }


   salavatcounter = (TextView) findViewById(R.id.salavatcounter);
   addsalavat = (Button) findViewById(R.id.addsalavat);
   addsalavat.setOnClickListener(this);
   sefrkon = (Button) findViewById(R.id.sefrkon);
   sefrkon.setOnClickListener(this);

   salavatcounter.setText("" + salvatcount);



  }



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater menu_option_infalter = getMenuInflater();
    menu_option_infalter.inflate(R.menu.optionmenu, menu);      
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    if (item.getItemId()== R.id.about) {
        Intent intent = new Intent(main.this,about.class);
        startActivity(intent);
        }
    return super.onOptionsItemSelected(item);
}

    @Override
protected void onSaveInstanceState(Bundle savedInstanceState) {
        savedInstanceState.putInt("count", salavatcount);
        super.onSaveInstanceState(savedInstanceState);
  }


  @Override
protected void onStop() {
    super.onStop();

    SharedPreferences setting= getSharedPreferences("setting", 0);
    SharedPreferences.Editor editor=setting.edit();
    editor.putInt("salavatcount", salavatcount);
    editor.commit();

    }



public void onClick(View v) {
    // TODO Auto-generated method stub

    if (v.getId()== R.id.addsalavat){

        salavatcount++;

        salavatcounter.setText(""+salavatcount);

    }else if (v.getId() == R.id.sefrkon) {
        salavatcount=0;

        salavatcounter.setText(""+salavatcount);
    }
}
}
5
  • u r right, but it doesn't work yet. I think it has some conflict with savedInstanceState method. do u have any suggestion?
    – Reza Shek
    Apr 24, 2012 at 11:14
  • I've edited my post to show a slightly different way of doing it. Apr 24, 2012 at 12:03
  • thanks a lot for reply David. Do you run this code on your computer? I mean do you get result by these changes? actually I don't understand what is count in this line --> salavatcount = savedInstanceState.getInt("count");
    – Reza Shek
    Apr 24, 2012 at 12:14
  • I've not actually run the code, so there may be some typos. I changed "int" to "count" just to make it easier to read. Didn't make sense to me to call a variable "int" Apr 24, 2012 at 12:59
  • yes, I do. "threadid=3: thread exiting with uncaught exception " and "Uncaught handler: thread main exiting due to uncaught exception"
    – Reza Shek
    Apr 24, 2012 at 15:33
0

You initialize salvatcount to 0 here:

if (savedInstanceState == null) {
    salavatcount = 0;
}

instead, initialize it with the value from shared preferences:

SharedPreferences setting =getSharedPreferences("setting",0);
salvatcount = Integer.valueOf(setting.getString("salavatcount", "0"));
// ....
salavatcounter.setText("" + salvatcount);
2
  • tnx for replay. where should I use your code? in if statemenet?
    – Reza Shek
    Apr 24, 2012 at 10:25
  • No, I don't test your code, did you debug it and saw why it failed?
    – MByD
    Apr 24, 2012 at 13:19
0

I change my code to this and my problem solved. thanks all you guy that help me

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (savedInstanceState == null) {
        salavatcount = 0;
    }
    setContentView(R.layout.main);
    salavatcounter = (TextView) findViewById(R.id.salavatcounter);
    addsalavat = (Button) findViewById(R.id.addsalavat);
    addsalavat.setOnClickListener(this);
    sefrkon = (Button) findViewById(R.id.sefrkon);
    sefrkon.setOnClickListener(this);

    // Start save value of salavatcount when exit App
    SharedPreferences setting = getSharedPreferences("countersetting", 0);
    salavatcount = setting.getInt("countervalue", 0);
    salavatcounter.setText(""+salavatcount);
    // End save value of salavatcount when exit App
}

and on onstop method

@Override
protected void onStop() {
    super.onStop();
    // Start save value of salavatcount when exit App
    SharedPreferences setting = getSharedPreferences("countersetting", 0);
    SharedPreferences.Editor editor=setting.edit();
    editor.putInt("countervalue", salavatcount);
    editor.commit();
    // End save value of salavatcount when exit App
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.