GetView NullPointerException

时间:2014-10-04 13:21:00

标签: android android-adapter android-adapterview getview

我已实施自定义列表视图以显示餐厅详细信息。当我安装为新应用程序时,它运行良好。在它连续崩溃之后。我得到的问题和答案与stackoverflow中的问题类似。我尝试了那些答案。但它不适合我。我该如何解决这个问题? 提前谢谢。

这是我的代码

public class RestaurantAdapter extends BaseAdapter{
    String hotelName, partySize, searchByTime;
    ArrayList<String> urlList, timeList, offerList;
    Context context;
    ViewHolder holder;
    LayoutInflater inflater;
    RestaurantData obj;
    ArrayList<RestaurantData> dataList;

public RestaurantAdapter(Context con, ArrayList<RestaurantData> list,
        String searchTime) {
    context = con;
    dataList = list;
    inflater = (LayoutInflater) con
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    searchByTime = searchTime;
}

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

@Override
public Object getItem(int arg0) {
    return null;
}

@Override
public long getItemId(int pos) {
    return pos;
}

public class ViewHolder {
    public TextView tv_hotel, tv_party_label, tv_party_value;
    public Gallery gallery;
    public LinearLayout ll_time;
    public ProgressBar progress;
    public HorizontalScrollView hsv;
}

@SuppressLint({ "ViewHolder", "InflateParams", "NewApi", "CutPasteId" })
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View vi = convertView;
    SessionManager sesObj = new SessionManager(context);
    holder = new ViewHolder();
    if (sesObj.isSearchLayoutVisible() == 0) {
        obj = dataList.get(position);
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (vi == null) {
            vi = inflater.inflate(R.layout.restaurant_item, parent, false);     
            holder.ll_time = (LinearLayout) vi.findViewById(R.id.ll_timer);
            holder.tv_hotel = (TextView) vi.findViewById(R.id.tv_hotelname);
            holder.tv_party_label = (TextView) vi
                    .findViewById(R.id.tv_party_label);
            holder.tv_party_value = (TextView) vi
                    .findViewById(R.id.tv_party_value_item);
            holder.gallery = (Gallery) vi.findViewById(R.id.gallery1);
            holder.progress = (ProgressBar) vi
                    .findViewById(R.id.progressBar1);
            holder.hsv = (HorizontalScrollView) vi
                    .findViewById(R.id.hsv_time);
            vi.setTag(holder);
        } else {
            holder = (ViewHolder) vi.getTag();

        }

        holder.tv_hotel.setText(obj.hotelName);
        holder.tv_party_value.setText(obj.partySize);
        holder.gallery.setAnimationDuration(10);
        holder.gallery.setTag(position);
        holder.tv_hotel.setTag(position);
        holder.gallery.setAdapter(new GalleryImageAdapter(context,
                obj.urlList));
        timeList = obj.timeList;
        offerList = obj.offerList;
        for (int i = 0; i < timeList.size(); i++) {
            View viewTime = inflater.inflate(R.layout.time_item, null);
            final Button btn_time = (Button) viewTime
                    .findViewById(R.id.btn_time);
            btn_time.setText(timeList.get(i));
            btn_time.setTag(position + "-" + offerList.get(i));         
            holder.ll_time.addView(viewTime);
        }
    }
    return vi;
}


private class GalleryImageAdapter extends BaseAdapter {

    ArrayList<String> urlList;

    public GalleryImageAdapter(Context context, ArrayList<String> url) {
        urlList = url;
    }

    public int getCount() {
        return urlList.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    // Override this method according to your need
    @SuppressLint({ "ViewHolder", "InflateParams" })
    public View getView(int index, View view, ViewGroup viewGroup) {
        // TODO Auto-generated method stub
        View vi = view;
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (vi == null)
            vi = inflater.inflate(R.layout.image, viewGroup, false);
        final ImageView img = (ImageView) vi
                .findViewById(R.id.img_for_gallery);
        holder.progress.setVisibility(View.GONE);
        ImageLoader.getInstance().displayImage(urlList.get(index), img);
        return vi;
    }
    }
}

我得到了这个

10-04 18:47:02.819: E/AndroidRuntime(9010): FATAL EXCEPTION: main
10-04 18:47:02.819: E/AndroidRuntime(9010): java.lang.NullPointerException
10-04 18:47:02.819: E/AndroidRuntime(9010):     at android.widget.AbsListView.obtainView(AbsListView.java:2463)
10-04 18:47:02.819: E/AndroidRuntime(9010):     at android.widget.ListView.makeAndAddView(ListView.java:1775)
10-04 18:47:02.819: E/AndroidRuntime(9010):     at android.widget.ListView.fillDown(ListView.java:678)
10-04 18:47:02.819: E/AndroidRuntime(9010):     at android.widget.ListView.fillFromTop(ListView.java:739)
10-04 18:47:02.819: E/AndroidRuntime(9010):     at android.widget.ListView.layoutChildren(ListView.java:1628)
10-04 18:47:02.819: E/AndroidRuntime(9010):     at android.widget.AbsListView.onLayout(AbsListView.java:2296)
10-04 18:47:02.819: E/AndroidRuntime(9010):     at android.view.View.layout(View.java:14055)
10-04 18:47:02.819: E/AndroidRuntime(9010):     at android.view.ViewGroup.layout(ViewGroup.java:4604)
10-04 18:47:02.819: E/AndroidRuntime(9010):     at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
10-04 18:47:02.819: E/AndroidRuntime(9010):     at android.view.View.layout(View.java:14055)
10-04 18:47:02.819: E/AndroidRuntime(9010):     at android.view.ViewGroup.layout(ViewGroup.java:4604)
10-04 18:47:02.819: E/AndroidRuntime(9010):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1655)
10-04 18:47:02.819: E/AndroidRuntime(9010):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1513)
10-04 18:47:02.819: E/AndroidRuntime(9010):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1426)
10-04 18:47:02.819: E/AndroidRuntime(9010):     at android.view.View.layout(View.java:14055)
10-04 18:47:02.819: E/AndroidRuntime(9010):     at android.view.ViewGroup.layout(ViewGroup.java:4604)
10-04 18:47:02.819: E/AndroidRuntime(9010):     at android.widget.RelativeLayout.onLayout(RelativeLayout.java:948)
10-04 18:47:02.819: E/AndroidRuntime(9010):     at android.view.View.layout(View.java:14055)
10-04 18:47:02.819: E/AndroidRuntime(9010):     at android.view.ViewGroup.layout(ViewGroup.java:4604)
10-04 18:47:02.819: E/AndroidRuntime(9010):     at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
10-04 18:47:02.819: E/AndroidRuntime(9010):     at android.view.View.layout(View.java:14055)
10-04 18:47:02.819: E/AndroidRuntime(9010):     at android.view.ViewGroup.layout(ViewGroup.java:4604)
10-04 18:47:02.819: E/AndroidRuntime(9010):     at android.widget.RelativeLayout.onLayout(RelativeLayout.java:948)
10-04 18:47:02.819: E/AndroidRuntime(9010):     at android.view.View.layout(View.java:14055)
10-04 18:47:02.819: E/AndroidRuntime(9010):     at android.view.ViewGroup.layout(ViewGroup.java:4604)
10-04 18:47:02.819: E/AndroidRuntime(9010):     at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
10-04 18:47:02.819: E/AndroidRuntime(9010):     at android.view.View.layout(View.java:14055)
10-04 18:47:02.819: E/AndroidRuntime(9010):     at android.view.ViewGroup.layout(ViewGroup.java:4604)
10-04 18:47:02.819: E/AndroidRuntime(9010):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1655)
10-04 18:47:02.819: E/AndroidRuntime(9010):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1513)
10-04 18:47:02.819: E/AndroidRuntime(9010):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1426)
10-04 18:47:02.819: E/AndroidRuntime(9010):     at android.view.View.layout(View.java:14055)
10-04 18:47:02.819: E/AndroidRuntime(9010):     at android.view.ViewGroup.layout(ViewGroup.java:4604)
10-04 18:47:02.819: E/AndroidRuntime(9010):     at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
10-04 18:47:02.819: E/AndroidRuntime(9010):     at android.view.View.layout(View.java:14055)
10-04 18:47:02.819: E/AndroidRuntime(9010):     at android.view.ViewGroup.layout(ViewGroup.java:4604)
10-04 18:47:02.819: E/AndroidRuntime(9010):     at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1992)
10-04 18:47:02.819: E/AndroidRuntime(9010):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1813)
10-04 18:47:02.819: E/AndroidRuntime(9010):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1112)
10-04 18:47:02.819: E/AndroidRuntime(9010):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4472)
10-04 18:47:02.819: E/AndroidRuntime(9010):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
10-04 18:47:02.819: E/AndroidRuntime(9010):     at android.view.Choreographer.doCallbacks(Choreographer.java:555)
10-04 18:47:02.819: E/AndroidRuntime(9010):     at android.view.Choreographer.doFrame(Choreographer.java:525)
10-04 18:47:02.819: E/AndroidRuntime(9010):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
10-04 18:47:02.819: E/AndroidRuntime(9010):     at android.os.Handler.handleCallback(Handler.java:615)
10-04 18:47:02.819: E/AndroidRuntime(9010):     at android.os.Handler.dispatchMessage(Handler.java:92)
10-04 18:47:02.819: E/AndroidRuntime(9010):     at android.os.Looper.loop(Looper.java:137)
10-04 18:47:02.819: E/AndroidRuntime(9010):     at android.app.ActivityThread.main(ActivityThread.java:4898)
10-04 18:47:02.819: E/AndroidRuntime(9010):     at java.lang.reflect.Method.invokeNative(Native Method)
10-04 18:47:02.819: E/AndroidRuntime(9010):     at java.lang.reflect.Method.invoke(Method.java:511)
10-04 18:47:02.819: E/AndroidRuntime(9010):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
10-04 18:47:02.819: E/AndroidRuntime(9010):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
10-04 18:47:02.819: E/AndroidRuntime(9010):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:2)

getView()应始终返回非null View。您的sesObj.isSearchLayoutVisible() == 0条件使其在条件不为真且没有循环视图时返回空值。