【问题标题】:Why does UI not load after setContentView(), but only after onCreate() executes?为什么在 setContentView() 之后 UI 不加载,而是在 onCreate() 执行之后加载?
【发布时间】:2016-02-03 12:03:15
【问题描述】:

在我的 onCreate 方法中,我一开始就设置了内容视图()

inflater = getLayoutInflater();
    Thread.setDefaultUncaughtExceptionHandler(new MyExceptionHandler());
    root = inflater.inflate(R.layout.activity_main, null, false);
//    root.setBackgroundColor(color50);
    setContentView(root);

... 在 onCreate() 快结束时,我调用了一个自定义 asynctask 并从中获取结果。但是,我的应用程序的屏幕一直保持黑色,直到任务完成执行。

try {
        task = (DatabaseTask) new DatabaseTask().execute(new DatabaseTaskParams(activity, images));
        helper = task.get();
    } catch (Exception e) {
        Log.e("DatabaseTask", "stack trace", e);
    }

    suggestions = helper.getKeywords();

为什么会发生这种情况?是因为我的 XML,还是因为 onCreate 仅在调用它之后才呈现 UI,还是其他原因?

这是我的活动根 XML

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/fragment_photos_root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="?android:attr/actionBarSize"
    tools:context="dtancompany.gallerytest.MainActivity">
    <com.sothree.slidinguppanel.SlidingUpPanelLayout
        xmlns:sothree="http://schemas.android.com/apk/res-auto"
        android:id="@+id/sliding_add_panel"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:gravity="bottom"
        sothree:umanoPanelHeight="0dp"
        sothree:umanoParalaxOffset="100dp"
        sothree:umanoDragView="@+id/dragView"
        sothree:umanoOverlay="true">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <android.support.v4.widget.SwipeRefreshLayout
                android:id="@+id/swipe_refresh_layout"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
                <GridView android:id="@+id/grid_view"
                    android:layout_width="match_parent"        android:layout_height="match_parent"
                    android:numColumns="auto_fit"
                    android:gravity="center"
                    android:stretchMode="columnWidth"/>

            </android.support.v4.widget.SwipeRefreshLayout>
        </FrameLayout>

        <RelativeLayout
            android:id="@+id/add_keyword_panel"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="bottom"
            android:orientation="vertical"
            android:clipToPadding="false"
            android:padding="16dp">
            <EditText
                android:id="@+id/edit_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:maxLength="64"
                android:hint="@string/edit_text_hint"
                android:background="@android:color/transparent"
                android:textSize="25sp"
                android:layout_centerHorizontal="true"/>

            <ImageButton
                android:id="@+id/add"
                android:layout_width="60dp"
                android:layout_height="60dp"
                android:src="@drawable/plus"
                android:background="@android:color/transparent"
                android:layout_marginTop="10dp"
                android:layout_centerHorizontal="true"
                android:scaleType="fitCenter"
                android:layout_below="@id/edit_text"/>
        </RelativeLayout>
    </com.sothree.slidinguppanel.SlidingUpPanelLayout>

    <FrameLayout
        android:id="@+id/listFragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</FrameLayout>

【问题讨论】:

  • 你使用的是 Fragment 还是 Activity ?

标签: android android-layout android-activity android-asynctask


【解决方案1】:

为什么会这样?

因为您在您的AsyncTask 上调用get()get() 说“哦,是的,AsyncTask 我刚刚执行了,没关系,让我们占用主应用程序线程等待它”。

不要阻塞等待数据库 I/O 完成的主应用程序线程,而是将更新 UI 逻辑移动到 onPostExecute()AsyncTask

【讨论】:

    猜你喜欢
    • 2015-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多