【问题标题】:onResume() update TextViewonResume() 更新 TextView
【发布时间】:2011-07-31 15:42:53
【问题描述】:

我有我的主要 depotactivity,他将整数值设置为 textview,现在我希望在调用 onResume() 时更新这个值......但是当我添加我的小 onResume() 代码时

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

对于我什至无法启动的应用程序,LogCat 变得非常疯狂,并且超过半秒没有记录任何活动。有什么解决办法吗?提前致谢

【问题讨论】:

  • 注意onResume在第一次创建Activity的时候就被调用了,所以不需要复制onCreate和onResume中的代码。

标签: java android onresume


【解决方案1】:

我先添加super.onResume();:

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

我也会删除它:

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

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

来自onCreate,因为每次调用onCreate,也会调用onResume

【讨论】:

  • 还将@Override 添加到onRexume() 并将public 更改为protected
  • @MisterSquonk - 真实而真实。谢谢!
  • 谢谢! (我只能在 3 分钟内接受你的回答.. 不知道为什么,但我会这样做)也感谢额外的提示,现在清理我的代码 :) 并再次查看 android 生命周期 lol
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-12
  • 2013-01-26
  • 2013-11-01
相关资源
最近更新 更多