onResume() update TextView

2019-03-05 06:32发布

问题:

I have my main depotactivity where he sets integer value to a textview, now I want this value to get updated when onResume() is called... but when I add my little onResume() code

public class DepotActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        DepotDBUtils utils = new DepotDBUtils(this);
        int itemcount = utils.countItems(this);

        TextView tv = (TextView)findViewById(R.id.counter);
        tv.setText(tv.getText()+" "+itemcount);
    }
   String TAG = "DepotActivity";
   String[] itemInfo;
 public void onResume(){
    Log.d("DepotActivity","onResume() gets called");
    DepotDBUtils utils = new DepotDBUtils(this);
    int itemcount = utils.countItems(this);

    TextView tv = (TextView)findViewById(R.id.counter);
    tv.setText(tv.getText()+" "+itemcount);
    } 

to the app I can't even start it, and LogCat gets totally crazy and doesn't log any activity for more than half a second. Any solutions? Thanks in advance

回答1:

I'd start with adding super.onResume();:

@Override
protected void onResume(){
    super.onResume();
    // The rest
}

I would also remove that:

    DepotDBUtils utils = new DepotDBUtils(this);
    int itemcount = utils.countItems(this);

    TextView tv = (TextView)findViewById(R.id.counter);
    tv.setText(tv.getText()+" "+itemcount);

from onCreate, since every time onCreate is called, onResume is called as well.