0

我编写了通过按 + 按钮计算某些内容的应用程序,并将其显示在文本视图中。知道我想在 onstop() 方法中保存文本视图的值并在 onstart 中使用它并再次在文本视图上显示该值。实际上我做到了,但是当我单击 + 按钮时,文本视图重置为 1 而不是从最后一个值增加。我实际上将文本视图值保存在 onSaveInstanceState 中并在 onRestoreInstanceState 上使用它。main.java 代码:

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

编辑:日志

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
4

3 回答 3

0

您在此处将 salvatcount 初始化为 0:

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

相反,使用共享首选项中的值对其进行初始化:

SharedPreferences setting =getSharedPreferences("setting",0);
salvatcount = Integer.valueOf(setting.getString("salavatcount", "0"));
// ....
salavatcounter.setText("" + salvatcount);
于 2012-04-24T10:19:02.657 回答
0

您没有将 salavatcount 的值设置为您共享偏好的值。当您单击按钮时,它被重置为 0,因为这是 salavatcount 的默认值。

您还将 int 值保存为字符串。

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

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

    }

然后在您的 on create 方法中,从 SharePreferences 中设置 salavatcount 值。

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



  }

编辑:

我会像这样重写代码:

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);
    }
}
}
于 2012-04-24T10:26:08.257 回答
0

我将代码更改为此并解决了我的问题。感谢所有帮助我的人

@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
}

和 onstop 方法

@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
}
于 2012-04-25T00:23:52.670 回答