自定义ListAdapter不起作用

时间:2014-05-12 11:42:33

标签: listview android-listview adapter listadapter custom-adapter

我正在尝试制作自定义listadapter,它提供了NullPointerException,但我不知道我做错了... 这是自定义listadapter类:

public class BusinessListAdapter extends BaseAdapter {

    private Context mContext;
    private List<Businesses> mBusinessesList = null;
    private LayoutInflater mLayoutInflator = null;
    private ViewHolder mViewHolder;

    private static class ViewHolder{
        TextView mBName;
        TextView mBAddress;
        TextView mBDealsCount;
        TextView mBDistance;
    }

    public BusinessListAdapter(Context mContext,
            List<Businesses> mBusinessesList){
        super();
        this.mContext = mContext;
        this.mBusinessesList =  mBusinessesList;
        this.mLayoutInflator = LayoutInflater.from(mContext);       
    }

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

    @Override
    public Businesses getItem(int position) {       
        return mBusinessesList.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            mLayoutInflator = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = mLayoutInflator.inflate(R.layout.layout_list_item, null); 
            mViewHolder = new ViewHolder();

            mViewHolder.mBName  = (TextView) convertView.
                    findViewById(R.id.tvListItemBName);
            mViewHolder.mBName  = (TextView) convertView.
                    findViewById(R.id.tvListItemBAddress);
            mViewHolder.mBName  = (TextView) convertView.
                    findViewById(R.id.tvListItemBDealsCount);
            mViewHolder.mBName  = (TextView) convertView.
                    findViewById(R.id.tvListItemBDistance);

            convertView.setTag(mViewHolder);
        }else{
            mViewHolder = (ViewHolder) convertView.getTag();
        }

        Businesses business = mBusinessesList.get(position);

        mViewHolder.mBName.setText(business.getbName());
        mViewHolder.mBAddress.setText(business.getbAddress());
        mViewHolder.mBDealsCount.setText(business.getDealCount());
        mViewHolder.mBDistance.setText(business.getbDistance());

        return convertView;
    }

}

以下是我的listview活动:

public class BusinessListActivity extends ListActivity implements OnItemClickListener{

    private ArrayList<Businesses> businessesList;
    private BusinessListAdapter mBusinessListAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_bussinesses_list);

        businessesList = new ArrayList<Businesses>();

        businessesList.add(new Businesses("ABC","ABC ADDRESS","5 Deals","2Km"));
        businessesList.add(new Businesses("ABC","ABC ADDRESS","5 Deals","2Km"));
            businessesList.add(new Businesses("ABC","ABC ADDRESS","5 Deals","2Km"));

        mBusinessListAdapter = new BusinessListAdapter(BusinessListActivity.this,
                businessesList);
        setListAdapter(mBusinessListAdapter);
        getListView().setOnItemClickListener(this);

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        Businesses mBusiness = businessesList.get(position);

        Intent intent = new Intent(BusinessListActivity.this, BusinessDealsActivity.class);     
//      intent.putExtra("mBusiness", mBusiness);        
        startActivity(intent);

    }

}

这是我的logcat。

05-13 12:21:22.080: E/AndroidRuntime(6143): FATAL EXCEPTION: main
05-13 12:21:22.080: E/AndroidRuntime(6143): java.lang.NullPointerException
05-13 12:21:22.080: E/AndroidRuntime(6143):     at com.faisalahsan.discountcloud.listviewadapters.BusinessListAdapter.getView(BusinessListAdapter.java:78)
05-13 12:21:22.080: E/AndroidRuntime(6143):     at android.widget.AbsListView.obtainView(AbsListView.java:2041)
05-13 12:21:22.080: E/AndroidRuntime(6143):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1244)
05-13 12:21:22.080: E/AndroidRuntime(6143):     at android.widget.ListView.onMeasure(ListView.java:1155)
05-13 12:21:22.080: E/AndroidRuntime(6143):     at android.view.View.measure(View.java:12758)
05-13 12:21:22.080: E/AndroidRuntime(6143):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
05-13 12:21:22.080: E/AndroidRuntime(6143):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1369)
05-13 12:21:22.080: E/AndroidRuntime(6143):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:660)
05-13 12:21:22.080: E/AndroidRuntime(6143):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
05-13 12:21:22.080: E/AndroidRuntime(6143):     at android.view.View.measure(View.java:12758)
05-13 12:21:22.080: E/AndroidRuntime(6143):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
05-13 12:21:22.080: E/AndroidRuntime(6143):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
05-13 12:21:22.080: E/AndroidRuntime(6143):     at android.view.View.measure(View.java:12758)
05-13 12:21:22.080: E/AndroidRuntime(6143):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:812)
05-13 12:21:22.080: E/AndroidRuntime(6143):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
05-13 12:21:22.080: E/AndroidRuntime(6143):     at android.view.View.measure(View.java:12758)
05-13 12:21:22.080: E/AndroidRuntime(6143):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
05-13 12:21:22.080: E/AndroidRuntime(6143):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
05-13 12:21:22.080: E/AndroidRuntime(6143):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2092)
05-13 12:21:22.080: E/AndroidRuntime(6143):     at android.view.View.measure(View.java:12758)
05-13 12:21:22.080: E/AndroidRuntime(6143):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1064)
05-13 12:21:22.080: E/AndroidRuntime(6143):     at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2445)
05-13 12:21:22.080: E/AndroidRuntime(6143):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-13 12:21:22.080: E/AndroidRuntime(6143):     at android.os.Looper.loop(Looper.java:137)
05-13 12:21:22.080: E/AndroidRuntime(6143):     at android.app.ActivityThread.main(ActivityThread.java:4424)
05-13 12:21:22.080: E/AndroidRuntime(6143):     at java.lang.reflect.Method.invokeNative(Native Method)
05-13 12:21:22.080: E/AndroidRuntime(6143):     at java.lang.reflect.Method.invoke(Method.java:511)
05-13 12:21:22.080: E/AndroidRuntime(6143):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
05-13 12:21:22.080: E/AndroidRuntime(6143):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
05-13 12:21:22.080: E/AndroidRuntime(6143):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

你为什么这样做,&#34; mViewHolder.mBName&#34;反复用于获得不同的观点

 mViewHolder.mBName  = (TextView) convertView.
                    findViewById(R.id.tvListItemBName);
            mViewHolder.mBName  = (TextView) convertView.
                    findViewById(R.id.tvListItemBAddress);
           mViewHolder.mBName  = (TextView) convertView.
                    findViewById(R.id.tvListItemBDealsCount);
            mViewHolder.mBName  = (TextView) convertView.
                    findViewById(R.id.tvListItemBDistance);

希望上面的代码行是问题的原因,对不同的文本视图使用不同的变量,如下所示,

  mViewHolder.mBName  = (TextView) convertView.
                        findViewById(R.id.tvListItemBName);

     mViewHolder.mBAddress= (TextView) convertView.
                                findViewById(R.id.tvListItemBAddress);
     mViewHolder.mBDealsCount= (TextView) convertView.
                            findViewById(R.id.tvListItemBDealsCount);
mViewHolder.mBDistance = (TextView) convertView.
                        findViewById(R.id.tvListItemBDistance);