0
  public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        loadData();
        setContentView(R.layout.preload);

        Thread thread = new Thread()
        {
            @Override
            public void run() {
               setUpDB();
               setContentView(R.layout.main);
            }
        };
        thread.start();
    }

所以我希望它在数据库设置(setUpDB)时显示预加载视图,然后在设置完成后切换到主视图更改为主视图。我几乎可以正常工作了;显示加载器直到大约 80% 然后崩溃,但现在我什至无法让它显示加载器...如果我在线程中有 setUpDB() 它会崩溃,如果我有它在线程之外它显示一个空白屏幕直到完全加载。我不记得以前的代码了……有什么想法吗?

4

3 回答 3

4

尝试使用 assynctask ..

         private class LoadAssync extends AsyncTask<String, Void, Void> { // Assync task

    protected void onPreExecute() {

            ProgressDialog dialog=ProgressDialog.show(this,"","Loading");

    }

    protected Void doInBackground(final String... args) {
         setUpDB();

    }

    protected void onPostExecute(final Void unused) {


        if (dialog.isShowing()) {
            dialog.dismiss();
        }

    }
}

执行 assynctask

LoadAssync mAsyync1;

mAsyync1 = new LoadAssync();
            mAsyync1.execute(null);
于 2012-05-16T10:32:52.160 回答
2

声明一个 Handler 并调用它来像这样更新你的 UI,

    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

ProgressDialog pd=ProgressDialog.show(this,"","Please Wait...");
Handler handler =new Handler()
{
 public void handleMessage(Message msg)
{
 if(msg.what==0)
{
 pd.dismiss();
 setContentView(R.layout.main);
}
}
};   
            Thread thread = new Thread()
            {
                @Override
                public void run() {
                  loadData();
                   setUpDB();
                  handler.sendEmptyMEssage(0);
                }
            };
            thread.start();
        }
于 2012-05-16T10:28:35.770 回答
0
    setUpDB();
    splashHandler.sendEmptyMessageDelayed(STOP_SPLASH, STOP_SPLASH_DELAY);

private Handler splashHandler = new Handler() {
    public void handleMessage(android.os.Message msg) {

        switch (msg.what) {
        case STOP_SPLASH:
                            progressbar.dismiss();  
            startActivity(new Intent(getApplicationContext(),
                    HomeScreenActivity.class));
            finish();
            break;
        }
    };
};
于 2012-05-16T10:27:57.300 回答