1

我的应用程序在启动时加载了很多东西,经过测试,它在开始时延迟太久而没有splash screen. 所以,我想显示一个splash screen直到我的应用程序完成加载。我不想在 X 秒内显示带有计时器的屏幕。我在这里找到了一个例子:

安卓闪屏

我尝试在上面的 SO 主题中实现代码,但我只是不理解代码。将它集成到我的代码中后,我发现了一个错误,我在下面的代码中进行了注释。但是我不懂很多代码,我在下面的代码中评论了我感到困惑的部分。

public class MainMenu extends Activity {

    private ProgressDialog pd = null;
    private Object data = null;  //What is this?

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.mainmenu);

        // show the ProgressDialog on this thread
        this.pd = ProgressDialog.show(this, "Working...", "Downloading data...", true, false);

        // start a new thread that will download all the data
        new DownloadTask().execute("Any parameters to download.");  //What is DownloadTask()?
    }

    private class DownloadTask extends AsyncTask<String, Void, Object> {

        protected Object doInBackground(String... args) {  //Are these parameters correct?
            return "replace this with your object";  //What is this?
        }

        protected void onPostExecute(Object results) {
            // pass the resulting data to the main activity
            MainMenu.this.data = result;  //Error:  "result cannot be resolved to a variable"

            if(MainMenu.this.pd != null) {
                MainMenu.this.pd.dismiss();
            }
        }
    }
}
4

2 回答 2

4

让我们从错误开始:

MainMenu.this.data = result;

注意到错字了吗?它应该是结果*s*:

MainMenu.this.data = results;

在下面解决您的其余问题:

private class DownloadTask extends AsyncTask<String, Void, Object>

该声明用于一个名为 的内联类DownloadTask,它声明您将Strings (via String...) 作为doInBackground(String... params).

第二个参数(Void在您的情况下)指示用于通过publishProgress(DATATYPE)/ “发布”进度的数据类型onProgressUpdate(DATATYPE... progress)。此方法适用于通知用户更改,例如当您完成下载文件但仍有一些文件未完成时。

在本例中,最后一个参数 ( Object) 指示您将传递给的数据类型。这可能是在某处更新 ListAdapter,或者根据在.onPostExecute(DATATYPE)ObjectdoInBackground

于 2013-03-04T16:19:29.617 回答
2

ProgressDialog在方法中显示onPreexecute和关闭它onPostExcute

像这样的东西

private class DownloadTask extends AsyncTask<String, Void, Object> {

           @Override
protected void onPreExecute() {
  mProgressDialog = new ProgressDialog(activity);
    mProgressDialog =ProgressDialog.show(activity, "", "Please Wait",true,false);
    super.onPreExecute();
}

        protected Object doInBackground(String... args) {  //Are these parameters correct?
            return "replace this with your object";  //What is this?
        }

        protected void onPostExecute(Object results) {
            // pass the resulting data to the main activity
            MainMenu.this.data = results;  //it should be results 
if (mProgressDialog != null || mProgressDialog.isShowing()){
         mProgressDialog.dismiss();
 }
            if(MainMenu.this.pd != null) {
                MainMenu.this.pd.dismiss();
            }
        }
于 2013-03-04T16:19:07.290 回答