Android ListView显示为空,但包含对象

时间:2012-09-10 01:41:40

标签: java android xml android-layout android-listview

我的android listview有一个非常奇怪的问题。列表视图位于片段内,所有内容都在编译,我不再收到nullpointer错误,但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>

1 个答案:

答案 0 :(得分:1)

在“活动”中,您设置了setContentView(R.layout.tabs_layout); tabs_layout,然后在R.layout.tab_frag1_layout中展开了LinearLayout mContainer广告,但您尚未在mContainer中添加tabs_layout

尝试在标签布局中设置mContainer