2 u014724833 u014724833 于 2016.01.17 08:11 提问

火大,快崩溃了,这段自定义adapter到底错在哪里啊!! 20C

以下是源码

 import java.util.List;
import java.util.zip.Inflater;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class AdapterPerson extends ArrayAdapter<NewPerson> {

    Context context;
    List<NewPerson> persons;
    int res;

    public AdapterPerson(Context context, int resource, List<NewPerson> objects) {
        super(context, resource, objects);

        context = this.context;
        persons = objects;
        res = resource;

    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        NewPerson person = getItem(position);
        Holder holder;
        if(convertView == null){
            convertView = View.inflate(context, res, null);                               //就是这一行在报错空指针异常,但是我死活看不出哪里有空指针异常啊,资源文件也是能链接过去的
            holder = new Holder();
            holder.age = (TextView) convertView.findViewById(R.id.age);
            holder.name = (TextView) convertView.findViewById(R.id.name);
            holder.sex = (TextView) convertView.findViewById(R.id.sex);
            holder.info = (TextView) convertView.findViewById(R.id.info);
            convertView.setTag(holder);
        }else{
            holder = (Holder)convertView.getTag();
        }
        holder.name.setText(person.getName());
        holder.age.setText(person.getAge());
        holder.info.setText(person.getInfo());

        return convertView;
    }

    private static class Holder {
        TextView name, age, sex, info;
    }

}

01-17 07:57:48.636: E/AndroidRuntime(17863): FATAL EXCEPTION: main
01-17 07:57:48.636: E/AndroidRuntime(17863): Process: com.sz.sqlitedemo1, PID: 17863
01-17 07:57:48.636: E/AndroidRuntime(17863): java.lang.NullPointerException
01-17 07:57:48.636: E/AndroidRuntime(17863): at android.view.LayoutInflater.from(LayoutInflater.java:211)
01-17 07:57:48.636: E/AndroidRuntime(17863): at android.view.View.inflate(View.java:18048)
01-17 07:57:48.636: E/AndroidRuntime(17863): at com.sz.sqlitedemo1.AdapterPerson.getView(AdapterPerson.java:42)
01-17 07:57:48.636: E/AndroidRuntime(17863): at android.widget.AbsListView.obtainView(AbsListView.java:3065)
01-17 07:57:48.636: E/AndroidRuntime(17863): at android.widget.ListView.makeAndAddView(ListView.java:2125)
01-17 07:57:48.636: E/AndroidRuntime(17863): at android.widget.ListView.fillDown(ListView.java:940)
01-17 07:57:48.636: E/AndroidRuntime(17863): at android.widget.ListView.fillFromTop(ListView.java:1025)
01-17 07:57:48.636: E/AndroidRuntime(17863): at android.widget.ListView.layoutChildren(ListView.java:1949)
01-17 07:57:48.636: E/AndroidRuntime(17863): at android.widget.AbsListView.onLayout(AbsListView.java:2897)
01-17 07:57:48.636: E/AndroidRuntime(17863): at android.view.View.layout(View.java:15348)
01-17 07:57:48.636: E/AndroidRuntime(17863): at android.view.ViewGroup.layout(ViewGroup.java:4662)
01-17 07:57:48.636: E/AndroidRuntime(17863): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
01-17 07:57:48.636: E/AndroidRuntime(17863): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
01-17 07:57:48.636: E/AndroidRuntime(17863): at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
01-17 07:57:48.636: E/AndroidRuntime(17863): at android.view.View.layout(View.java:15348)
01-17 07:57:48.636: E/AndroidRuntime(17863): at android.view.ViewGroup.layout(ViewGroup.java:4662)
01-17 07:57:48.636: E/AndroidRuntime(17863): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1055)
01-17 07:57:48.636: E/AndroidRuntime(17863): at android.view.View.layout(View.java:15348)
01-17 07:57:48.636: E/AndroidRuntime(17863): at android.view.ViewGroup.layout(ViewGroup.java:4662)
01-17 07:57:48.636: E/AndroidRuntime(17863): at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
01-17 07:57:48.636: E/AndroidRuntime(17863): at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
01-17 07:57:48.636: E/AndroidRuntime(17863): at android.view.View.layout(View.java:15348)
01-17 07:57:48.636: E/AndroidRuntime(17863): at android.view.ViewGroup.layout(ViewGroup.java:4662)
01-17 07:57:48.636: E/AndroidRuntime(17863): at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:413)
01-17 07:57:48.636: E/AndroidRuntime(17863): at android.view.View.layout(View.java:15348)
01-17 07:57:48.636: E/AndroidRuntime(17863): at android.view.ViewGroup.layout(ViewGroup.java:4662)
01-17 07:57:48.636: E/AndroidRuntime(17863): at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
01-17 07:57:48.636: E/AndroidRuntime(17863): at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
01-17 07:57:48.636: E/AndroidRuntime(17863): at android.view.View.layout(View.java:15348)
01-17 07:57:48.636: E/AndroidRuntime(17863): at android.view.ViewGroup.layout(ViewGroup.java:4662)
01-17 07:57:48.636: E/AndroidRuntime(17863): at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2355)
01-17 07:57:48.636: E/AndroidRuntime(17863): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2066)
01-17 07:57:48.636: E/AndroidRuntime(17863): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1060)
01-17 07:57:48.636: E/AndroidRuntime(17863): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6175)
01-17 07:57:48.636: E/AndroidRuntime(17863): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
01-17 07:57:48.636: E/AndroidRuntime(17863): at android.view.Choreographer.doCallbacks(Choreographer.java:580)
01-17 07:57:48.636: E/AndroidRuntime(17863): at android.view.Choreographer.doFrame(Choreographer.java:550)
01-17 07:57:48.636: E/AndroidRuntime(17863): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
01-17 07:57:48.636: E/AndroidRuntime(17863): at android.os.Handler.handleCallback(Handler.java:733)
01-17 07:57:48.636: E/AndroidRuntime(17863): at android.os.Handler.dispatchMessage(Handler.java:95)
01-17 07:57:48.636: E/AndroidRuntime(17863): at android.os.Looper.loop(Looper.java:136)
01-17 07:57:48.636: E/AndroidRuntime(17863): at android.app.ActivityThread.main(ActivityThread.java:5054)
01-17 07:57:48.636: E/AndroidRuntime(17863): at java.lang.reflect.Method.invokeNative(Native Method)
01-17 07:57:48.636: E/AndroidRuntime(17863): at java.lang.reflect.Method.invoke(Method.java:515)
01-17 07:57:48.636: E/AndroidRuntime(17863): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
01-17 07:57:48.636: E/AndroidRuntime(17863): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
01-17 07:57:48.636: E/AndroidRuntime(17863): at dalvik.system.NativeStart.main(Native Method)

7个回答

devmiao
devmiao   Ds   Rxr 2016.01.17 09:46
qq_27238663
qq_27238663   2016.01.17 23:53

定义一个LayoutInflater,private LayoutInflater mInflater,然后在构造函数里写mInflater=LayoutInflater.from(context)

qq_27238663
qq_27238663   2016.01.18 00:42

后面getView也不要用View.inflater了。直接用mInflater

lang523493505
lang523493505   2016.01.18 22:32

把getview里面那个 View.inflate改成LayoutInflate.from(context).infinite
这样应该就可以了

lxk_1993
lxk_1993   Rxr 2016.01.19 08:38

把getview里面那个 View.inflate改成LayoutInflate.from(context).inflate

euthan
euthan   2016.01.22 09:32

把res去掉看看,应该是方法调用出错了

qq_18481971
qq_18481971   2016.02.22 21:50

context 初始化那里错了

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!