inflate()后文本未出现在TextView框中

时间:2016-05-09 18:59:47

标签: java android android-layout

我正在尝试创建一个仪表板,其中包含登录时用户队列中每条消息的文本视图。由于必须动态构建此视图,因此我正在为队列中的每个消息数量扩展布局;布局只有一个linearlayout,其中包含三个textview元素,这些元素将根据每条消息的内容(发件人,消息,时间等等)而改变。在给布局充气之后,膨胀视图中没有任何文本只有一个空白容器。我已经尝试更改inflate方法以调用null作为第二个参数,并且省略第三个参数以强制默认(true)无效。在此先感谢您的帮助。

来自activity_dash_board.java

public class DashBoardActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dash_board);
        // Create Async object for network processing
        DashBoardAsync m_processDashboard = new DashBoardAsync(getApplicationContext(), (LinearLayout)findViewById(R.id.LL_dashboardContainer), (LinearLayout)findViewById(R.id.LL_pagingQueue));

        // Get the info passed from the LoginActivity
        Bundle extras = getIntent().getExtras();


     }

来自DashBoardAsync.java

public class DashBoardAsync extends AsyncTask<Void, Integer, Boolean> {

            public static String[][] m_organizationPagingQueue = new String[3][3];

            private LinearLayout m_LL_container;
            private LinearLayout m_LL_queue;
            private Context m_C_context;


            public DashBoardAsync(Context context, LinearLayout container, LinearLayout queue){
                m_C_context = context;
                m_LL_container = container;
                m_LL_queue = queue;
            }

//doInBackground omitted

@Override
protected void onPostExecute(Boolean result){
    LayoutInflater inflater = (LayoutInflater)m_C_context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

for(int a = 0; a < 2; ++a) {
    View inflatedView = inflater.inflate(R.layout.activity_dash_board_paging_queue, m_LL_container, false);

    TextView requester = (TextView)inflatedView.findViewById(R.id.TV_pagingRequester);
    requester.setText("This is some text");

    TextView message = (TextView)inflatedView.findViewById(R.id.TV_pagingMessage);
    message.setText("This is some text");


    m_LL_container.addView(inflatedView);
}
super.onPostExecute(result);

}

来自activity_dash_board_paging_queue.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:layout_gravity="top"
    android:weightSum="1"
    android:id="@+id/LL_pagingQueue"
    android:background="#ffffff"
    android:paddingTop="2dp"
    android:paddingBottom="2dp"
    android:gravity="top"
    android:showDividers="end"
    android:divider="#a41515">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="@string/TV_pagingRequester"
        android:id="@+id/TV_pagingRequester" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="@string/TV_pagingMessage"
        android:id="@+id/TV_pagingMessage" />

    <TextView
        android:layout_width="46dp"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="@string/TV_pagingTime"
        android:id="@+id/TV_pagingTime"
        android:layout_gravity="right" />

</LinearLayout>

来自activity_dash_board.xml

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:weightSum="1"
        android:id="@+id/LL_dashboardContainer"
        android:background="#24e3b0">


        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="75dp"
                android:id="@+id/imageView"
                android:src="@drawable/logo"
                android:adjustViewBounds="true"
                android:contentDescription="Scrolling header" />
        </LinearLayout>

</LinearLayout>

Layout with inflated View containing blank textviews

Preview of activity_dash_board_paging_queue.xml

1 个答案:

答案 0 :(得分:0)

postExecute运行时,您的视图可能尚未完全创建。我会将异步调用移动到您知道UI存在的onResume

另外,请勿使用getApplicationContext(),请使用this