使用Customarrayadapter自定义列表视图时出错

时间:2014-02-13 19:44:03

标签: java android listview custom-adapter

我正在关注自定义列表视图教程here,我不确定是什么原因造成的。 logcat是:

02-13 14:00:51.346: E/AndroidRuntime(26969): FATAL EXCEPTION: main
02-13 14:00:51.346: E/AndroidRuntime(26969): Process: com.skateconnect, PID: 26969
02-13 14:00:51.346: E/AndroidRuntime(26969): java.lang.NullPointerException
02-13 14:00:51.346: E/AndroidRuntime(26969):    at com.skateconnect.android.adapters.CustomBaseAdapter.getView(CustomBaseAdapter.java:57)
02-13 14:00:51.346: E/AndroidRuntime(26969):    at android.widget.AbsListView.obtainView(AbsListView.java:2263)
02-13 14:00:51.346: E/AndroidRuntime(26969):    at android.widget.ListView.measureHeightOfChildren(ListView.java:1263)
02-13 14:00:51.346: E/AndroidRuntime(26969):    at android.widget.ListView.onMeasure(ListView.java:1175)
02-13 14:00:51.346: E/AndroidRuntime(26969):    at android.view.View.measure(View.java:16573)
02-13 14:00:51.346: E/AndroidRuntime(26969):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5147)
02-13 14:00:51.346: E/AndroidRuntime(26969):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
02-13 14:00:51.346: E/AndroidRuntime(26969):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
02-13 14:00:51.346: E/AndroidRuntime(26969):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
02-13 14:00:51.346: E/AndroidRuntime(26969):    at android.view.View.measure(View.java:16573)
02-13 14:00:51.346: E/AndroidRuntime(26969):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5147)
02-13 14:00:51.346: E/AndroidRuntime(26969):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
02-13 14:00:51.346: E/AndroidRuntime(26969):    at android.view.View.measure(View.java:16573)
02-13 14:00:51.346: E/AndroidRuntime(26969):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5147)
02-13 14:00:51.346: E/AndroidRuntime(26969):    at  com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
02-13 14:00:51.346: E/AndroidRuntime(26969):    at android.view.View.measure(View.java:16573)
02-13 14:00:51.346: E/AndroidRuntime(26969):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5147)
02-13 14:00:51.346: E/AndroidRuntime(26969):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
02-13 14:00:51.346: E/AndroidRuntime(26969):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2289)
02-13 14:00:51.346: E/AndroidRuntime(26969):    at android.view.View.measure(View.java:16573)
02-13 14:00:51.346: E/AndroidRuntime(26969):    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2064)
02-13 14:00:51.346: E/AndroidRuntime(26969):    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1191)
02-13 14:00:51.346: E/AndroidRuntime(26969):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1400)
02-13 14:00:51.346: E/AndroidRuntime(26969):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1078)
02-13 14:00:51.346: E/AndroidRuntime(26969):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5861)
02-13 14:00:51.346: E/AndroidRuntime(26969):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
02-13 14:00:51.346: E/AndroidRuntime(26969):    at android.view.Choreographer.doCallbacks(Choreographer.java:562)
02-13 14:00:51.346: E/AndroidRuntime(26969):    at android.view.Choreographer.doFrame(Choreographer.java:532)
02-13 14:00:51.346: E/AndroidRuntime(26969):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
02-13 14:00:51.346: E/AndroidRuntime(26969):    at android.os.Handler.handleCallback(Handler.java:733)
02-13 14:00:51.346: E/AndroidRuntime(26969):    at android.os.Handler.dispatchMessage(Handler.java:95)
02-13 14:00:51.346: E/AndroidRuntime(26969):    at android.os.Looper.loop(Looper.java:137)
02-13 14:00:51.346: E/AndroidRuntime(26969):    at android.app.ActivityThread.main(ActivityThread.java:5083)
02-13 14:00:51.346: E/AndroidRuntime(26969):    at java.lang.reflect.Method.invokeNative(Native Method)
02-13 14:00:51.346: E/AndroidRuntime(26969):    at java.lang.reflect.Method.invoke(Method.java:515)
02-13 14:00:51.346: E/AndroidRuntime(26969):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
02-13 14:00:51.346: E/AndroidRuntime(26969):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
02-13 14:00:51.346: E/AndroidRuntime(26969):    at dalvik.system.NativeStart.main(Native Method)

custombaseadapter.java的位置如下:

public class CustomBaseAdapter extends BaseAdapter {
Context context;
List<RowItem> rowItems;

public CustomBaseAdapter(Context context, List<RowItem> items) {
    this.context = context;
    this.rowItems = items;
}

/*private view holder class*/
private class ViewHolder {
 TextView pid;
 ImageView image;
 TextView name;
 TextView distance;
 TextView date;
 TextView time;
}

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;

    LayoutInflater mInflater = (LayoutInflater)
        context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.list_item, null);
        holder = new ViewHolder();
        holder.pid = (TextView) convertView.findViewById(R.id.pid);
        holder.image = (ImageView) convertView.findViewById(R.id.list_image);
        holder.name = (TextView) convertView.findViewById(R.id.name);
        holder.distance = (TextView) convertView.findViewById(R.id.distance);
        holder.date = (TextView) convertView.findViewById(R.id.date);
        holder.time = (TextView) convertView.findViewById(R.id.time);
        convertView.setTag(holder);
    }
    else {
        holder = (ViewHolder) convertView.getTag();
    }

    RowItem rowItem = (RowItem) getItem(position);

    holder.pid.setText(rowItem.getpid());
    holder.image.setImageResource(rowItem.getimage());
    holder.name.setText(rowItem.getname());
    holder.distance.setText(rowItem.getdisp_dist());
    holder.date.setText(rowItem.getdate());
    holder.time.setText(rowItem.gettime());
    /*

     */
    return convertView;
}

@Override
public int getCount() {
    return rowItems.size();
}

@Override
public Object getItem(int position) {
    return rowItems.get(position);
}

@Override
public long getItemId(int position) {
    return rowItems.indexOf(getItem(position));
}
}    

和第57行是

holder.pid.setText(rowItem.getpid());

我仍然习惯于自定义适配器,所以我们将非常感谢您的帮助!

提前谢谢

泰勒

编辑:rowItem为NULL EDIT2:如何填充:

                for (int i = 0; i < spots.length(); i++) {
                    JSONObject c = spots.getJSONObject(i);
                    search_trig=0;

                    // Storing each json item in variable
                    String strlong = c.getString(TAG_LONG);
                    double elong = Double.parseDouble(strlong);
                    String strlat = c.getString(TAG_LAT);
                    double elat = Double.parseDouble(strlat);
                    double dist = getDistance(slat, slong, elat, elong);
                    String distance = String.format("%.1f", dist);
                    String id = c.getString(TAG_PID);
                    String pave = c.getString(TAG_PAVEMENT);
                    String traffic = c.getString(TAG_TRAFFIC);
                    String enviro = c.getString(TAG_ENVIRONMENT);
                    String name = c.getString(TAG_NAME);
                    String image_B64 = c.getString(TAG_IMAGE_B64);
                    String dist_display = distance+ " Mi";
                    String timeAway=".";
                    type=".";
                    date=".";
                    time=".";
                    //EVENT STUFF
                    if(add.equals("Event")){
                        //need to add other stuff too
                        type = c.getString(TAG_TYPE);
                        date = c.getString(TAG_DATE);
                        time = c.getString(TAG_TIME);
                        timeAway = String.valueOf(getTimeAWAY(date));
                    }
                    image = getDispImage(type,enviro);
                    if(search_bp==1){
                        //checks distance
                        if(str_dist.equals("Any")==false){
                        if((dist>Double.parseDouble(str_dist))){
                            search_trig=1;
                        }
                        }

                        if(str_pavement.equals("Any")==false){
                        if((str_pavement.equals(pave)==false)){
                            search_trig=1;
                        }
                        }

                        if(str_traffic.equals("Any")==false){
                        if((str_traffic.equals(traffic)==false)){
                            search_trig=1;
                        }
                        }

                        if(str_enviro.equals("Any")==false){
                        if((str_enviro.equals(enviro)==false)){
                            search_trig=1;
                        }
                        }

                        //EVENT STUFF
                        if(add.equals("Event")){

                            if(str_type.equals("Any")==false){
                                if((str_type.equals(type)==false)){
                                    search_trig=1;
                                }
                                }

                        }
                        item = new RowItem(id,image,name,distance,date,time,dist_display,timeAway);
                        shr_spots.put(id, new SHARE(id, name,strlong,strlat,pave,traffic,enviro,image_B64,date,time,type)); 


                        if(search_trig == 0){
                            //spotsList.add(map); 
                            rowItems.add(item);
                            n=1;
                        }
                    }else{
                        //spotsList.add(map);
                         rowItems.add(item);

                        n=1;
                    }
                }

初始化为

rowItems = new ArrayList<RowItem>();

EDIT3:

CustomBaseAdapter adapter = new CustomBaseAdapter(AllSpotsActivity.this, rowItems);
                lv.setAdapter(adapter);

3 个答案:

答案 0 :(得分:0)

在这一行:

RowItem rowItem = (RowItem) getItem(position);

也许getItem(position)返回null。然后你尝试在它上面调用getpid()=&gt; NPE

在此行放置一个断点并检查调试器中的值,或者将值输出到logcat以确认。然后调整代码以在调用方法之前检查空值。

更新:在深入了解填充列表的for循环后,我在最后看到了以下部分:

                }else{
                    //spotsList.add(map);
                     rowItems.add(item);

                    n=1;
                }

所以你将“item”添加到列表中,但是你从未在if语句中为else-case设置它。

如果(search_bp == 1),你只为案例设置项目,但不是这样的情况。

所以最后你最终会在输入for-loop之前添加项目值。

尝试为此案例指定正确的项目值,可能是这样的:

                  }else{
                     //spotsList.add(map);
                     item = new RowItem(id,image, //etc.
                     rowItems.add(item);
                  }

或者,在这种情况下,根本不要将项目添加到列表中。取决于你想要的东西; - )

我希望我的猜测是对的,并帮助解决你的问题。

答案 1 :(得分:0)

你的填充函数(虽然有点caotical)似乎没问题,你的构造函数似乎没问题,所以由于你的stacktrace引用了测量错误,它只是一个布局问题。

当膨胀时,你正在呼唤:

convertView = mInflater.inflate(R.layout.list_item, null);

尝试通过以下方式替换此行:

convertView = mInflater.inflate(R.layout.list_item, parent, false);

答案 2 :(得分:0)

更改您的退货声明。

变化 return convertView;

与 返回视图;

此处视图将是您的虚增视图。