0

我有这个ListView,当我启动应用程序时,它会给出一个空指针异常,但如果视图是 GONE,它不会给出任何错误,

lVers = (ListView) view.findViewById(R.id.lVers);

与适配器

RechercheAdapter adapter2 = new RechercheAdapter(getActivity(), R.layout.elem_recherche, listPaysVers);
lVers.setAdapter(adapter2);

这是适配器类

public class RechercheAdapter extends ArrayAdapter<Pays> {

        Context context;
        ArrayList<Pays> data;

        public RechercheAdapter(Context context, int textViewResourceId, ArrayList<Pays> d) {
            super(context, textViewResourceId, d);
            this.data = d;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return data.size();
        }

        @Override
        public Pays getItem(int position) {
            // TODO Auto-generated method stub
            return data.get(position);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Pays p = data.get(position);

            LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View v = inflater.inflate(R.layout.elem_recherche, null);

            ImageView drap = (ImageView) v.findViewById(R.id.drap);
            drap.setImageDrawable(Utils.getBitmapFromBase64FullSize(p.getDreapeau64(), getActivity()));

            TextView nom = (TextView) v.findViewById(R.id.nom);
            nom.setText(p.getPays());

            return convertView;
        }

    }

当我尝试启动应用程序时,我得到一个空指针异常,

09-24 12:52:37.095: E/AndroidRuntime(30836): FATAL EXCEPTION: main
09-24 12:52:37.095: E/AndroidRuntime(30836): java.lang.NullPointerException
09-24 12:52:37.095: E/AndroidRuntime(30836):    at android.widget.AbsListView.obtainView(AbsListView.java:2467)
09-24 12:52:37.095: E/AndroidRuntime(30836):    at android.widget.ListView.makeAndAddView(ListView.java:1775)
09-24 12:52:37.095: E/AndroidRuntime(30836):    at android.widget.ListView.fillDown(ListView.java:678)
09-24 12:52:37.095: E/AndroidRuntime(30836):    at android.widget.ListView.fillFromTop(ListView.java:739)
09-24 12:52:37.095: E/AndroidRuntime(30836):    at android.widget.ListView.layoutChildren(ListView.java:1628)
09-24 12:52:37.095: E/AndroidRuntime(30836):    at android.widget.AbsListView.onLayout(AbsListView.java:2300)
09-24 12:52:37.095: E/AndroidRuntime(30836):    at android.view.View.layout(View.java:14072)
09-24 12:52:37.095: E/AndroidRuntime(30836):    at android.view.ViewGroup.layout(ViewGroup.java:4607)
09-24 12:52:37.095: E/AndroidRuntime(30836):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1655)
09-24 12:52:37.095: E/AndroidRuntime(30836):    at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1644)
09-24 12:52:37.095: E/AndroidRuntime(30836):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1428)
09-24 12:52:37.095: E/AndroidRuntime(30836):    at android.view.View.layout(View.java:14072)
09-24 12:52:37.095: E/AndroidRuntime(30836):    at android.view.ViewGroup.layout(ViewGroup.java:4607)
09-24 12:52:37.095: E/AndroidRuntime(30836):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1655)
09-24 12:52:37.095: E/AndroidRuntime(30836):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1513)
09-24 12:52:37.095: E/AndroidRuntime(30836):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1426)
09-24 12:52:37.095: E/AndroidRuntime(30836):    at android.view.View.layout(View.java:14072)
09-24 12:52:37.095: E/AndroidRuntime(30836):    at android.view.ViewGroup.layout(ViewGroup.java:4607)
09-24 12:52:37.095: E/AndroidRuntime(30836):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1655)
09-24 12:52:37.095: E/AndroidRuntime(30836):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1513)
09-24 12:52:37.095: E/AndroidRuntime(30836):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1426)
09-24 12:52:37.095: E/AndroidRuntime(30836):    at android.view.View.layout(View.java:14072)
09-24 12:52:37.095: E/AndroidRuntime(30836):    at android.view.ViewGroup.layout(ViewGroup.java:4607)
09-24 12:52:37.095: E/AndroidRuntime(30836):    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
09-24 12:52:37.095: E/AndroidRuntime(30836):    at android.view.View.layout(View.java:14072)
09-24 12:52:37.095: E/AndroidRuntime(30836):    at android.view.ViewGroup.layout(ViewGroup.java:4607)
09-24 12:52:37.095: E/AndroidRuntime(30836):    at android.widget.RelativeLayout.onLayout(RelativeLayout.java:948)
09-24 12:52:37.095: E/AndroidRuntime(30836):    at android.view.View.layout(View.java:14072)
09-24 12:52:37.095: E/AndroidRuntime(30836):    at android.view.ViewGroup.layout(ViewGroup.java:4607)
09-24 12:52:37.095: E/AndroidRuntime(30836):    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
09-24 12:52:37.095: E/AndroidRuntime(30836):    at android.view.View.layout(View.java:14072)
09-24 12:52:37.095: E/AndroidRuntime(30836):    at android.view.ViewGroup.layout(ViewGroup.java:4607)
09-24 12:52:37.095: E/AndroidRuntime(30836):    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
09-24 12:52:37.095: E/AndroidRuntime(30836):    at android.view.View.layout(View.java:14072)
09-24 12:52:37.095: E/AndroidRuntime(30836):    at android.view.ViewGroup.layout(ViewGroup.java:4607)
09-24 12:52:37.095: E/AndroidRuntime(30836):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1655)
09-24 12:52:37.095: E/AndroidRuntime(30836):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1513)
09-24 12:52:37.095: E/AndroidRuntime(30836):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1426)
09-24 12:52:37.095: E/AndroidRuntime(30836):    at android.view.View.layout(View.java:14072)
09-24 12:52:37.095: E/AndroidRuntime(30836):    at android.view.ViewGroup.layout(ViewGroup.java:4607)
09-24 12:52:37.095: E/AndroidRuntime(30836):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1655)
09-24 12:52:37.095: E/AndroidRuntime(30836):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1513)
09-24 12:52:37.095: E/AndroidRuntime(30836):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1426)
09-24 12:52:37.095: E/AndroidRuntime(30836):    at android.view.View.layout(View.java:14072)
09-24 12:52:37.095: E/AndroidRuntime(30836):    at android.view.ViewGroup.layout(ViewGroup.java:4607)
09-24 12:52:37.095: E/AndroidRuntime(30836):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1655)
09-24 12:52:37.095: E/AndroidRuntime(30836):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1513)
09-24 12:52:37.095: E/AndroidRuntime(30836):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1426)
09-24 12:52:37.095: E/AndroidRuntime(30836):    at android.view.View.layout(View.java:14072)
09-24 12:52:37.095: E/AndroidRuntime(30836):    at android.view.ViewGroup.layout(ViewGroup.java:4607)
09-24 12:52:37.095: E/AndroidRuntime(30836):    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
09-24 12:52:37.095: E/AndroidRuntime(30836):    at android.view.View.layout(View.java:14072)
09-24 12:52:37.095: E/AndroidRuntime(30836):    at android.view.ViewGroup.layout(ViewGroup.java:4607)
09-24 12:52:37.095: E/AndroidRuntime(30836):    at android.widget.RelativeLayout.onLayout(RelativeLayout.java:948)
09-24 12:52:37.095: E/AndroidRuntime(30836):    at android.view.View.layout(View.java:14072)
09-24 12:52:37.095: E/AndroidRuntime(30836):    at android.view.ViewGroup.layout(ViewGroup.java:4607)
09-24 12:52:37.095: E/AndroidRuntime(30836):    at android.widget.RelativeLayout.onLayout(RelativeLayout.java:948)
09-24 12:52:37.095: E/AndroidRuntime(30836):    at android.view.View.layout(View.java:14072)
09-24 12:52:37.095: E/AndroidRuntime(30836):    at android.view.ViewGroup.layout(ViewGroup.java:4607)
09-24 12:52:37.095: E/AndroidRuntime(30836):    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
09-24 12:52:37.095: E/AndroidRuntime(30836):    at android.view.View.layout(View.java:14072)
09-24 12:52:37.095: E/AndroidRuntime(30836):    at android.view.ViewGroup.layout(ViewGroup.java:4607)
09-24 12:52:37.095: E/AndroidRuntime(30836):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1655)
09-24 12:52:37.095: E/AndroidRuntime(30836):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1513)
09-24 12:52:37.095: E/AndroidRuntime(30836):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1426)
09-24 12:52:37.095: E/AndroidRuntime(30836):    at android.view.View.layout(View.java:14072)
09-24 12:52:37.095: E/AndroidRuntime(30836):    at android.view.ViewGroup.layout(ViewGroup.java:4607)
09-24 12:52:37.095: E/AndroidRuntime(30836):    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
09-24 12:52:37.095: E/AndroidRuntime(30836): 

有人知道发生了什么吗?谢谢

4

7 回答 7

8

Return View V not convertView 或

   convertView=inflater.inflate(R.layout.elem_recherche, null);
于 2013-09-24T12:01:13.023 回答
1

尝试这个..

public class RechercheAdapter extends ArrayAdapter<Pays> {

        Context context;
        ArrayList<Pays> data;

        public RechercheAdapter(Context context, int textViewResourceId, ArrayList<Pays> d) {
            super(context, textViewResourceId, d);
            LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            this.data = d;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return data.size();
        }

        @Override
        public Pays getItem(int position) {
            // TODO Auto-generated method stub
            return data.get(position);
        }

@Override
        public View getView(int position, View convertView, ViewGroup parent) {

          View view = convertView;
            Pays p = data.get(position);
            view = inflater.inflate(R.layout.elem_recherche, null);

            ImageView drap = (ImageView) v.findViewById(R.id.drap);
            drap.setImageDrawable(Utils.getBitmapFromBase64FullSize(p.getDreapeau64(), getActivity()));

            TextView nom = (TextView) v.findViewById(R.id.nom);
            nom.setText(p.getPays());

            return convertView;
        }
于 2013-09-24T12:06:14.937 回答
1

我认为你必须给你要膨胀的父布局,因为你的列表是在父布局中创建的

  view = mInflater.inflate(R.layout.elem_recherche, parent, false);
于 2013-09-24T12:14:18.953 回答
1

替换这个:

View v = inflater.inflate(R.layout.elem_recherche, null);

和 :

convertView = inflater.inflate(R.layout.elem_recherche, null);
于 2013-09-24T12:03:04.097 回答
1

问题在于您的getView(int, View, ViewGroup)方法:

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ...
        View v = inflater.inflate(R.layout.elem_recherche, null);
        ...
        return convertView;
    }

请注意,您正在返回convertView,而不是v您的膨胀布局。convertViewnull因为它没有启动到视图,从而导致NullPointerException访问。

试试这个:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    Pays p = data.get(position);

    LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if( convertView == null ) {
        convertView = inflater.inflate(R.layout.elem_recherche, null, false);
    }

    ImageView drap = (ImageView) convertView.findViewById(R.id.drap);
    drap.setImageDrawable(Utils.getBitmapFromBase64FullSize(p.getDreapeau64(), getActivity()));

    TextView nom = (TextView) convertView.findViewById(R.id.nom);
    nom.setText(p.getPays());

    return convertView;
}
于 2013-09-24T12:03:50.753 回答
1

您正在膨胀一个视图并返回一个视图,这就是它为空的原因.. 像这样更改您的 getview metgod ..

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Pays p = data.get(position);
if(convertView==null){
            LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.elem_recherche, null);
            ImageView drap = (ImageView) convertView.findViewById(R.id.drap);
            TextView nom = (TextView) convertView.findViewById(R.id.nom);
}  
         drap.setImageDrawable(Utils.getBitmapFromBase64FullSize(p.getDreapeau64(), getActivity()));
           nom.setText(p.getPays());

        return convertView;
    }
于 2013-09-24T12:07:27.353 回答
1

使用 convertView = inflater.inflate(R.layout.elem_recherche, null); 而不是 View v = inflater.inflate(R.layout.elem_recherche, null);then,返回 convertView;

于 2013-09-24T12:08:04.667 回答