2011-10-11 46 views
1

我有一個視圖存根問題。ViewStub空指針異常

這是ViewStub在我的Java文件

ViewStub stub = (ViewStub) findViewById(R.id.stub1); 
ArrayAdapter<String> content=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,files); 
View inflated=stub.inflate(); 
ListView people=(ListView)inflated.findViewById(R.id.friends); 
people.setAdapter(content); 

這是我初始化Viewstub

在視圖存根充氣
<?xml version="1.0" encoding="utf-8"?> 
<merge xmlns:android="http://schemas.android.com/apk/res/android"> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id = "@+id/linear_details" > 
    <include layout="@layout/home_back_next_bar" /> 
    <ViewStub android:id="@+id/stub1" android:inflatedId="@+id/showlayout" 
android:layout="@layout/layout1" android:layout_width="fill_parent" 
android:layout_height="wrap_content"/> 

</LinearLayout> 
</merge> 

我的佈局文件如下所述XML:

 <?xml version="1.0" encoding="utf-8"?> 
    <ListView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/friends" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
/> 

此代碼給我一個NullPointerException在創建ListView的實例來填充它。 任何想法是什麼原因導致此問題以及如何解決。

非常感謝提前

錯誤日誌貓: enter image description here

+0

在日誌中顯示空指針異常。 –

回答

1

而不是使用

ListView people=(ListView)inflated.findViewById(R.id.friends); 

我用

ListView people=(ListView)stub.inflate(); 

和它的工作不知道爲什麼? 。但它確實:))