onResume() 更新 TextView

发布于 2024-11-27 13:36:28 字数 974 浏览 1 评论 0原文

我有我的主要仓库活动,他将整数值设置为文本视图,现在我希望在调用 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 变得完全疯狂,超过半秒没有记录任何活动。有什么解决办法吗?提前致谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花间憩 2024-12-04 13:36:28

我首先添加 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 也会被调用。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文