试图访问的ListView的儿童在空指针异常 [英] Nullpointer exception when trying to access to ListView 's children

查看:143
本文介绍了试图访问的ListView的儿童在空指针异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个Listfragment,它显示的视图列表。
这里是ListFragment:

I've created a Listfragment, which displays a List of views . Here is the ListFragment :

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:

Each row of the ListView displays an image and a text . It's the same image/text for the moment but I will put differents image/text later . The xml file of a row is 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功能。我得到一个NullPointerException异常,当我打电话getChildAt(INT)。为什么呢?
我的意思是,我打电话ListView控件后onCreateView,然后我只是想:借此ListView控件的子视图,以这个孩子的ImageView,modifiying的ImageView的

This works fine . But, I wanted then in the ListFragment to modify the images of each row of the ListView . So I did it in the function onActivityCreated . And I got a NullPointerException when I am calling getChildAt( int ) . Why ? I mean, I'm calling the ListView AFTER onCreateView, and then I just want to : take a child view of this ListView, taking the imageview of this child, modifiying the imageview .

我如何才能获得孩子的意见?...

How can I access to the child views ?...

     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);

        }

    }

如果您需要更多的code(例如活动至极正在显示的片段,或其他的东西),或信息,就对我说:)

If you need some more code (like the activity wich is displaying the fragment, or other stuff), or informations, say it to me :)

推荐答案

如果你正在寻求修改为presented数据,您必须修改备份列表,而不是列表自身的数据。

If you're seeking to modify the data that is presented, you must modify the data that backs the list, not the list itself.

有三个组成部分:你的数据,你的适配器,将您的数据转换成视图,列表视图都可以使用,并且根据列表中的位置从适配器请求视图列表视图本身

There are three components: your data, your adapter which transforms your data into views that the list view can use, and the list view itself which requests views from the adapter based on list position.

如果你需要改变什么presented,更改数据。更新为新,改,数据适配器,并调用 adapter.notifyDatasetChanged()。这将触发你的列表视图再次请求从适配器的数据,因为它的陈旧。

If you need to change what's presented, change the data. Update the adapter with the new, changed, dataset, and call adapter.notifyDatasetChanged(). This will trigger your list view to request data from the adapter again, as its stale.

从你在哪里,在你的code现在,你可以做两件事情之一。

From where you are in your code now, you can do one of two things.


  • 而不是直接使用一个SimpleAdapter中,创建可扩展BaseAdapter类并使用它。添加的方法来改变你的数据( setImages(列表图片)为例),这也包括 notifyDatasetChanged超级调用()作为最后一个发言。

  • 创建SimpleAdapter的新实例使用新的数据和 setListAdapter()与新的适配器实例试。

  • Instead of using a SimpleAdapter directly, create a class that extends BaseAdapter and use that. Add a method to change your data (setImages(List images) for example) which also includes the super call to notifyDatasetChanged() as the last statement.
  • create a new instance of SimpleAdapter with your new data and setListAdapter() again with the new adapter instance.

首先是preferred,但TL:博士;是不直接修改你的看法。这是一个AdapterView因为它是由已经是数据源支持的

The first is preferred, but the tl:Dr; is "don't modify your views directly; it's an AdapterView because it's backed by a data source already."

这篇关于试图访问的ListView的儿童在空指针异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆