簡體   English   中英

如何在切換活動時刪除黑屏(不起作用)-在Android上

[英]how to remove black screen when switching activity( is not working) - on android

在我的應用程序中,我將一個活動切換到另一個活動(從Main.java切換到Feature_Screen.java)。 在Feature_Screen(第二個活動)中,我將下載大量數據和圖像以在網格視圖中進行設置。 這樣我就可以使用異步任務下載它。 盡管我在第二個活動中使用異步任務,但在將Main.java切換為Feature.java時卻出現黑屏。 我在谷歌搜索,但所有的答案說使用異步任務。

示例代碼:

public class Main extends TabActivity{

public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tabbar);
    .................
    intent = new Intent().setClass(this, Featured_Screen1.class);
    spec = tabHost.newTabSpec("home").setIndicator("",
            res.getDrawable(R.drawable.top_book_icon)).setContent(intent);
    tabHost.addTab(spec);
    ...........
}
}

在Feature_Screen1.java(Second.java)中:

public class Feature_Screen1 extends Activity{

public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.feature);
    .................
      new Content_load().execute();
    ...........
}

 class Content_load extends AsyncTask<Void, Void, Void> 
   {
ProgressDialog dialog = new ProgressDialog(SignInPage.this);

protected void onPreExecute() { 
    dialog.setMessage("Please wait...");
    dialog.setCancelable(false);
    dialog.show();
}

protected void onPostExecute(Void unused) {
    dialog.dismiss();
}

protected Void doInBackground(Void... arg0) {
      ..........
  return null;
   }
  } 

}

我的問題是如何在活動之間切換時避免黑屏? 請幫我。

最后我找到了解決黑屏問題的答案。

像下面那樣使用AsyncTask,這對我有用,如果您遇到任何問題請告訴我

public class SyncroniseRecords extends AsyncTask<Void, Void, Void>
{  
    @Override
    protected void onPreExecute()
    {  
        super.onPreExecute();
        dialog.setMessage("Please wait...");
        dialog.setCancelable(false);
        dialog.show();

    } 
    @Override 
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
        // Things to be done while execution of long running operation is in progress. For example updating ProgessDialog
     }

    @Override 
    protected void onPostExecute(Void result)
    { 
           dialog.cancel();
    }
    @Override
    protected Void doInBackground(Void... params) {

        return null;
    }
} 

這將啟動沒有任何黑屏的活動。 使用onPreExecute可以顯示進度條,一旦過程完成,您可以在OnPostExecute()取消它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM