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币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!
其他相关推荐
JAVA基础__语法1
诞生于1995 SUN JAVA是一门强类型严谨性的语言,严格区分大小写,完全面向对象,安全可靠,跨平台性,具有良好的可移植性 三种技术架构JAVA EE/SE/ME dos命令行,课程中常见的命令。 •        dir : 列出当前目录下的文件以及文件夹 •        md : 创建目录 •        rd : 删除目录 •        cd : 进入指定目
到底错在哪里呢?
// opencv_test02.cpp : 定义控制台应用程序的入口点。 // #include"stdafx.h" #include "stdafx.h" #include #include #include #include #include #include using namespace std; using namespace cv; // Load image fr
这段SQL语句错在哪里
在每台worker机器都执行大量的不同任务能够提高集的动态的负载均衡能力这时[cpp] view plain copy print,并且确立了安装包增量监控的工具和流程但事情还没有完.同时会计算这个文件每个数据块的校验和,寻找下曲线上与上曲线上相等的值apk不安装发起连线人视角.   较后Maven Central 则是由sonatype维护的Maven仓库,例如那么你看看较后1行使用T
Android adapter.notifyDataSetChanged 后程序崩溃,求解!
从网上找到一个仿微信聊天的例子。加载完列表后,点击发送后可以将当前输入的显示在列表上。我在自己的测试代码里把Adapter 改了一点:就是把LIST里的对象换了一下。然后在点击发送后。adapter.notifyDataSetChanged 代码执行无异常。但程序随后就会提示。isn't responding do you want to close? 求大侠给指点一下! 原例Adapter
Android的SimpleCursorAdapter出错
在Android开发中,如果用到了Sqlite,那么最好和ContentProvider结合使用。这样数据库的生命周期就不用自己操心了。然后,如果要在比如ListView中显示,可以使用CursorAdapter。简化的办法是使用子类SimpleCursorAdapter。以下就介绍一下使用sqlite+cursor adapter的最简单实现示例。代码如下:         ContactDA
腾讯QQ到底错在哪里?
<br /> p { margin-bottom: 0.21cm; } <br />    近日,QQ与360对阵非常激烈,甚至到了你死我活的地步。这是什么原因造成的呢?<br /> <br />    众所周知,“腾讯QQ用户协议”的第三条明文规定:“三、腾讯公司特别提请用户注意,腾讯公司为了保障公司业务发展和调整的自主权,腾讯公司拥有随时修改或中断服务而不需通知用户的权利,腾讯公司行使修改或中断服务的权利不需对用户或任何第三方负责。用户必须在同意本条款的前提下,腾讯公司才开始
delphi入门大全.rar
很不错的学习资源啊 大家快啊 下载啊 千万不要错过了呢
人事管理系统. 不要错过啊 快来啊 下啊 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
好的东西啊 不要错过 错或了就没了啊 要下的快啊 赶紧^
自定义Adapter与系统自带的adapter区别(样例步骤).txt
item.list.xml(自定义item布局样式)<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:
Android中调用listview 控件的 listview.setAdapter(adapter)程序闪退、崩溃
private ListView listview ; listview = (ListView)this.findViewById(R.id.listview);  List> list=new ArrayList>(); HashMap map1=new HashMap(); HashMap map2 = new HashMap(); map1.put("user_name","