1

我的 android listview 有一个非常奇怪的问题。列表视图位于片段内,一切都在编译,我不再收到空指针错误,但列表视图显示为空。即使它看起来是空的,日志也说明列表视图有 385 个对象。我不明白为什么它是空的。我确实得到了一个蓝色片段,并且填充了列表视图。有任何想法吗?

我如何设置适配器:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tabs_layout);
    initialiseTabHost(savedInstanceState);
    if (savedInstanceState != null) {
        mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab")); //set the tab as per the saved state
    }

    ActivePackages = getList();
    LayoutInflater inflater = (LayoutInflater)   this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);        
    LinearLayout mContainer = (LinearLayout) inflater.inflate(R.layout.tab_frag1_layout, null);
    ListView activeList = (ListView) mContainer.findViewById(R.id.activelist);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, ActivePackages);
    Log.i("valueof activeList",String.valueOf(activeList.getCount())); //returns 0 
    activeList.setAdapter(adapter);
    adapter.notifyDataSetChanged();
    Log.i("valueof activeList",String.valueOf(activeList.getCount())); //returns 385. 
}

这是片段的 xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView 
        android:id="@+id/activelist"
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        android:background="#0073fd">    
    </ListView>

</LinearLayout>
4

1 回答 1

1

在 Activity 中,您设置setContentView(R.layout.tabs_layout);了 tabs_layout ,然后您在 inflate 布局R.layout.tab_frag1_layoutLinearLayout mContainer但尚未添加mContainerin tabs_layout

尝试mContainer在选项卡布局中设置。

于 2012-09-10T17:23:53.460 回答