我的适配器显示nullpointer异常,即使我可以得到文本

时间:2016-01-06 04:44:17

标签: android adapter

这是我的适配器:

public class PlaceAdapter extends BaseAdapter {

    private Context mContext;
    private ArrayList<Place> mPlacesList;
    private ListItemClickListener mClickListener;

    public PlaceAdapter(Context mContext, ArrayList<Place> mCategoryList) {
        this.mContext = mContext;
        this.mPlacesList = mCategoryList;
        // TODO Auto-generated constructor stub
    }

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

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

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    public void setItemClickAndLongClick(ListItemClickListener clickListener) {
        mClickListener = clickListener;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;
        final int i = position;
        if (convertView == null) {
            // inflate the GridView item layout
            LayoutInflater inflater = LayoutInflater.from(mContext);
            convertView = inflater.inflate(R.layout.adapter_places, parent, false);
            convertView.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    mClickListener.onItemClick(i);
                }
            });

            convertView.setOnLongClickListener(new View.OnLongClickListener() {

                @Override
                public boolean onLongClick(View v) {
                    // TODO Auto-generated method stub
                    mClickListener.onItemLongCLick(i);
                    return false;
                }
            });
            // initialize the view holder
            viewHolder = new ViewHolder();
            viewHolder.mIcon = (ImageView) convertView.findViewById(R.id.place_icon);
            viewHolder.mName = (TextView) convertView.findViewById(R.id.place_name);
            viewHolder.mAddress = (TextView) convertView.findViewById(R.id.place_address);
            viewHolder.mDistance = (TextView) convertView.findViewById(R.id.place_distance);
            convertView.setTag(viewHolder);
        } else {
            // recycle the already inflated view
            viewHolder = (ViewHolder) convertView.getTag();
        }

        // update the item view
        viewHolder.mName.setText(mPlacesList.get(position).getName());

        if (mPlacesList.get(position).getAddress() != null) {
            Log.e("TEST", mPlacesList.get(position).getAddress());
            viewHolder.mAddress.setText(mPlacesList.get(position).getAddress());
        }else{
            viewHolder.mAddress.setVisibility(View.INVISIBLE);
        }
    }

    public static class ViewHolder {
        private ImageView mIcon;
        private TextView mName;
        private TextView mAddress;
        private TextView mDistance;

        public ImageView getIcon() {
            return mIcon;
        }

        public TextView getName() {
            return mName;
        }

        public TextView getAddress() {
            return mAddress;
        }

        public TextView getDistance() {
            return mAddress;
        }

    }
}

这是我得到的例外:

01-05 23:37:32.540: E/TEST(2486): 15C4 Thi Sách Street, District 1, Ho Chi Minh City
01-05 23:37:32.548: E/AndroidRuntime(2486): FATAL EXCEPTION: main
01-05 23:37:32.548: E/AndroidRuntime(2486): Process: com.example.finalproject, PID: 2486
01-05 23:37:32.548: E/AndroidRuntime(2486): java.lang.NullPointerException
01-05 23:37:32.548: E/AndroidRuntime(2486):     at com.example.finalproject.adapter.PlaceAdapter.getView(PlaceAdapter.java:97)
01-05 23:37:32.548: E/AndroidRuntime(2486):     at android.widget.AbsListView.obtainView(AbsListView.java:2255)
01-05 23:37:32.548: E/AndroidRuntime(2486):     at android.widget.ListView.makeAndAddView(ListView.java:1790)
01-05 23:37:32.548: E/AndroidRuntime(2486):     at android.widget.ListView.fillDown(ListView.java:691)
01-05 23:37:32.548: E/AndroidRuntime(2486):     at android.widget.ListView.fillFromTop(ListView.java:752)
01-05 23:37:32.548: E/AndroidRuntime(2486):     at android.widget.ListView.layoutChildren(ListView.java:1616)
01-05 23:37:32.548: E/AndroidRuntime(2486):     at android.widget.AbsListView.onLayout(AbsListView.java:2087)
01-05 23:37:32.548: E/AndroidRuntime(2486):     at android.view.View.layout(View.java:14817)
01-05 23:37:32.548: E/AndroidRuntime(2486):     at android.view.ViewGroup.layout(ViewGroup.java:4631)
01-05 23:37:32.548: E/AndroidRuntime(2486):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
01-05 23:37:32.548: E/AndroidRuntime(2486):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
01-05 23:37:32.548: E/AndroidRuntime(2486):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
01-05 23:37:32.548: E/AndroidRuntime(2486):     at android.view.View.layout(View.java:14817)
01-05 23:37:32.548: E/AndroidRuntime(2486):     at android.view.ViewGroup.layout(ViewGroup.java:4631)
01-05 23:37:32.548: E/AndroidRuntime(2486):     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
01-05 23:37:32.548: E/AndroidRuntime(2486):     at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
01-05 23:37:32.548: E/AndroidRuntime(2486):     at android.view.View.layout(View.java:14817)
01-05 23:37:32.548: E/AndroidRuntime(2486):     at android.view.ViewGroup.layout(ViewGroup.java:4631)
01-05 23:37:32.548: E/AndroidRuntime(2486):     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
01-05 23:37:32.548: E/AndroidRuntime(2486):     at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
01-05 23:37:32.548: E/AndroidRuntime(2486):     at android.view.View.layout(View.java:14817)
01-05 23:37:32.548: E/AndroidRuntime(2486):     at android.view.ViewGroup.layout(ViewGroup.java:4631)
01-05 23:37:32.548: E/AndroidRuntime(2486):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
01-05 23:37:32.548: E/AndroidRuntime(2486):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
01-05 23:37:32.548: E/AndroidRuntime(2486):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
01-05 23:37:32.548: E/AndroidRuntime(2486):     at android.view.View.layout(View.java:14817)
01-05 23:37:32.548: E/AndroidRuntime(2486):     at android.view.ViewGroup.layout(ViewGroup.java:4631)
01-05 23:37:32.548: E/AndroidRuntime(2486):     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
01-05 23:37:32.548: E/AndroidRuntime(2486):     at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
01-05 23:37:32.548: E/AndroidRuntime(2486):     at android.view.View.layout(View.java:14817)
01-05 23:37:32.548: E/AndroidRuntime(2486):     at android.view.ViewGroup.layout(ViewGroup.java:4631)
01-05 23:37:32.548: E/AndroidRuntime(2486):     at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1983)
01-05 23:37:32.548: E/AndroidRuntime(2486):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1740)
01-05 23:37:32.548: E/AndroidRuntime(2486):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:996)
01-05 23:37:32.548: E/AndroidRuntime(2486):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5600)
01-05 23:37:32.548: E/AndroidRuntime(2486):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
01-05 23:37:32.548: E/AndroidRuntime(2486):     at android.view.Choreographer.doCallbacks(Choreographer.java:574)
01-05 23:37:32.548: E/AndroidRuntime(2486):     at android.view.Choreographer.doFrame(Choreographer.java:544)
01-05 23:37:32.548: E/AndroidRuntime(2486):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
01-05 23:37:32.548: E/AndroidRuntime(2486):     at android.os.Handler.handleCallback(Handler.java:733)
01-05 23:37:32.548: E/AndroidRuntime(2486):     at android.os.Handler.dispatchMessage(Handler.java:95)
01-05 23:37:32.548: E/AndroidRuntime(2486):     at android.os.Looper.loop(Looper.java:136)
01-05 23:37:32.548: E/AndroidRuntime(2486):     at android.app.ActivityThread.main(ActivityThread.java:5001)
01-05 23:37:32.548: E/AndroidRuntime(2486):     at java.lang.reflect.Method.invokeNative(Native Method)
01-05 23:37:32.548: E/AndroidRuntime(2486):     at java.lang.reflect.Method.invoke(Method.java:515)
01-05 23:37:32.548: E/AndroidRuntime(2486):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
01-05 23:37:32.548: E/AndroidRuntime(2486):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
01-05 23:37:32.548: E/AndroidRuntime(2486):     at dalvik.system.NativeStart.main(Native Method)

正如您所看到的,我从Place对象中成功获取了文本,但我不知道为什么会出现此异常。这真的很奇怪。请帮我!提前谢谢!

1 个答案:

答案 0 :(得分:1)

我发现了问题,我在xml布局文件中声明了错误的id。对于这个愚蠢的问题(;¯Д¯)

,大家都很抱歉