AsyncTask 和 setContentView

发布于 2025-01-07 06:17:55 字数 668 浏览 1 评论 0原文

我有这个代码用于管理数据库中的某些国家/地区;

class checkCountryAsync extends AsyncTask<String, String, String> {

    @Override
    protected void onPreExecute() {
       super.onPreExecute();
       setContentView(R.layout.main);

    }
    @Override
    protected String doInBackground(String... aurl) {
        MDPIActivity.this.runOnUiThread(new Runnable() {

            public void run() {
                  CountryTable country = new CountryTable() ;
                  country.EnterCountry();
            }
        });
        return null;
    }
}

有了这个,我想设置内容视图,然后在后台,方法 onBackground 起作用,但我仍然需要等待内容视图,直到 onBackground 方法未完成。

谢谢。

I have this code for managing some countrys on my database;

class checkCountryAsync extends AsyncTask<String, String, String> {

    @Override
    protected void onPreExecute() {
       super.onPreExecute();
       setContentView(R.layout.main);

    }
    @Override
    protected String doInBackground(String... aurl) {
        MDPIActivity.this.runOnUiThread(new Runnable() {

            public void run() {
                  CountryTable country = new CountryTable() ;
                  country.EnterCountry();
            }
        });
        return null;
    }
}

With this, I would like to set the content View and then in background, that the method onBackground works, but I have still to wait for the content view until the onBackground method is not finished.

Thank you.

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

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

发布评论

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

评论(3

2025-01-14 06:17:55

我不认为有任何理由将 setContentView() 放在 onPreExecute 方法中,它应该放在 onCreate 方法中以避免任何类型当您尝试查找视图时,会发生 NullPointerException ,对于您的 AsyncTask ,您应该只使用在方法doInBackground()

i don't see any reason for putting the setContentView() in the onPreExecute method, it should be in the onCreate method to avoid any kind of NullPointerException when you will try to find your views, and for your AsyncTask , you should just use the onPostExecute() which is executed after the method doInBackground()

单挑你×的.吻 2025-01-14 06:17:55

请尝试一下这个,想想它的工作原理。`class checkCountryAsync extends AsyncTask {

@Override
protected void onPreExecute() {
   super.onPreExecute();
   setContentView(R.layout.main);

}
@Override
protected String doInBackground(String... aurl) {
    CountryTable country = new CountryTable() ;
    country.EnterCountry();
    return null;
}

}
`

Please try this one, think its work.`class checkCountryAsync extends AsyncTask {

@Override
protected void onPreExecute() {
   super.onPreExecute();
   setContentView(R.layout.main);

}
@Override
protected String doInBackground(String... aurl) {
    CountryTable country = new CountryTable() ;
    country.EnterCountry();
    return null;
}

}
`

多像笑话 2025-01-14 06:17:55

您应该尝试publishProgress() 和onProgressUpdate() 方法。

You should try publishProgress() and onProgressUpdate() methods.

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