Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7848681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:17:27+00:00 2026-06-02T18:17:27+00:00

I wrote the App that count something by press on + button and show

  • 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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T18:17:30+00:00Added an answer on June 2, 2026 at 6:17 pm

    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);
        }
    }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote an app that uses Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_PICTURES) to find the folder to store
A while ago I wrote an app that relied on XML coming out of
I wrote a C app that uses the PCRE library. Everything works on my
I wrote a small app that turns out to be using a lot of
i wrote a java app that communicates and stores data in oracle; my question
I wrote a simple Sinatra app that generate an image using rmagick from some
I wrote a streaming radio app that is fairly basic in functionality but some
I am making an android app that shows the Calendar day view, it looks
I want to write an app that parse particular threas on a phpbb forum.
I want to write an app that takes in a model filename via cmd

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.