我創建了一個Listfragment,它顯示一個視圖列表。 這裏是ListFragment:嘗試訪問ListView的子項時發生Nullpointer異常
public class AroundYou extends ListFragment {
private int[] images = new int[] {
R.drawable.pinguin,
R.drawable.pinguin,
R.drawable.pinguin,
R.drawable.pinguin,
R.drawable.pinguin,
R.drawable.pinguin,
R.drawable.pinguin,
R.drawable.pinguin,
R.drawable.pinguin,
R.drawable.pinguin
};
private String[] names = new String[] {
"Pingu",
"Pingu",
"Pingu",
"Pingu",
"Pingu",
"Pingu",
"Pingu",
"Pingu",
"Pingu",
"Pingu"
};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
// Each row in the list stores country name, currency and flag
List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();
for(int i=0;i<10;i++){
HashMap<String, String> hm = new HashMap<String,String>();
hm.put("name", names[i]);
hm.put("image",Integer.toString(images[i]));
aList.add(hm);
}
// Keys used in Hashmap
String[] from = { "name","image"};
// Ids of views in listview_layout
int[] to = { R.id.name,R.id.image};
// Instantiating an adapter to store each items
// R.layout.listview_layout defines the layout of each item
SimpleAdapter adapter = new SimpleAdapter(getActivity().getBaseContext(), aList, R.layout.around_you_line_of_list, from, to);
setListAdapter(adapter);
return super.onCreateView(inflater, container, savedInstanceState);
}
}
ListView中的每一行顯示圖像和文本。這是同一時刻的圖片/文字,但稍後我會放入不同的圖片/文字。 行的XML文件around_you_line_of_list.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="horizontal" >
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:contentDescription="@string/hello_world"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp" />
<TextView
android:id="@+id/name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
這工作得很好。但是,我想在ListFragment中修改ListView每一行的圖像。所以我做了onActivityCreated函數。當我調用getChildAt(int)時,我得到了一個NullPointerException異常。爲什麼? 我的意思是,我打電話給ListView AFTER onCreateView,然後我只想:採取此ListView的子視圖,採取此子視圖的視圖,修改imageview。
我如何才能獲得孩子的意見?...
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
ListView listView = getListView();
for(int i = 0;i < 10;i++){
//I'm getting a null pointer exception at this line .
View view = listView.getChildAt(i);
ImageView imageview = (ImageView) view.findViewById(R.id.image);
BitmapDrawable drawable = (BitmapDrawable)imageview.getDrawable();
//here I 'm getting a bitmap from the drawable, and then applying a function that modify
//the bitmap .
Bitmap foreign_image = drawable.getBitmap();
Canvas canvas = null;
Bitmap bitmap = setBitmapClippedCircle(foreign_image,200,200,canvas);
//here I'm putting the result in the imageview .
imageview.setImageBitmap(bitmap);
}
}
如果你需要更多的代碼(如活動至極正在顯示的片段,或其他的東西),或信息,說給我:)
你見過這樣的人的代碼結構嗎?因爲從我記得(我可能是錯的),listview不支持一些viewgroup函數..好嗎?是的,所以讓我看看 – Elltz 2014-12-07 19:54:37
我真的不明白你的意見。事實上,我用這個教程來做到這一點:http://wptrafficanalyzer.in/blog/android-listfragment-with-images-and-text-using-android-support-library/ 但事實是,我想在將圖像放入列表後修改圖像 – 2014-12-07 20:02:10