简体   繁体   English

Android显示白屏,直到Asynctask启动,但我已经加载了布局

[英]Android displaying white screen until Asynctask starts but I have already loaded a layout

So I have the following code and a problem: It should load a layout that should say "downloading database..." (dynamically added to the layout) however I get a white-screen until the database is updated and then it just proceeds with add_categories_layout(0) and works fine. 因此,我有以下代码和一个问题:它应该加载一个布局,该布局应显示“正在下载数据库...”(动态添加到布局中),但是在更新数据库之前出现白屏,然后继续进行处理add_categories_layout(0)并正常工作。

How do I get it to show me the first layout? 如何获得显示第一个布局的信息?

protected void onResume(){ 受保护的void onResume(){

    super.onResume();
    setContentView(R.layout.home_screen_layout);
    SF = (LinearLayout) findViewById(R.id.SF);
    p_db_adapter = new DBAdapterProducts(getApplicationContext());
    c_db_adapter = new DBAdapterCategories(getApplicationContext());
    add_downloading_DB_layout();

    new UpdateDB().execute(CATEGORY_URL, PRODUCTS_URL, TIMESTAMP_URL_CATEGORIES, TIMESTAMP_URL_PRODUCTS);       
    while(DBupdated == false){}
    add_categories_layout(0);

} }

You should be putting setContentView in either onCreate() or onCreateView() 您应该将setContentView放在onCreate()onCreateView()

Also can we see the dynamic adding of the layout? 我们还能看到布局的动态添加吗?

在onResume完成之前,它不会绘制屏幕,​​而在DBUpdated为true之前,onResume将不会完成(此时交换布局)。

Typically you set the contentview in the oncreate() method. 通常,您可以在oncreate()方法中设置contentview。 There may be code in there causing it not no work. 那里可能有代码,导致它无法正常工作。 If you dont have on Oncreate method, make one and copy/paste this code in there. 如果您没有Oncreate方法,请制作一个并在其中复制/粘贴此代码。

You can put add_categories_layout(0); 您可以放置add_categories_layout(0); in the onPostExecute() of your AsyncTask since it runs on the UI Thread . AsyncTaskonPostExecute() ,因为它运行在UI Thread So when your task finishes it will run this method and onResume() can finish doing what it needs. 因此,当您的任务完成时,它将运行此方法, onResume()可以完成所需的工作。 If your AsyncTask is an inner class then you will have access to the Activity methods and member variables. 如果您的AsyncTask是一个内部类,那么您将有权访问Activity方法和成员变量。 If it is a separate file then you just need to pass a reference of your Activity to it to call the method. 如果它是一个单独的文件,则只需将其Activity的引用传递给它以调用该方法。

Here are a couple answers that can help with it if you need. 如果需要, 这里有几个答案可以帮助您。

As others have said, typically setContentView() is done in onCreate() to ensure it is one of the first things that runs but this shouldn't be a problem. 正如其他人所说, setContentView()通常是在onCreate()完成的,以确保它是运行的第一批内容之一,但这不是问题。 However, I would move it there unless you have a reason to have it in onResume() . 但是,除非您有理由将其放在onResume() ,否则我将其移到那里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM