每当我滚动这个ListView时NullPointerException

时间:2014-11-30 20:58:58

标签: android listview nullpointerexception scroll baseadapter

这是我的代码:

    import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import org.joda.time.YearMonth;

import java.util.ArrayList;

public class DayAdapter extends BaseAdapter {
    private int startDay;
    private int endDay;
    Context mContext;
    LayoutInflater mInflater;
    private ArrayList<Integer> theList = new ArrayList<Integer>();

    public DayAdapter(Context c, int sD, int eD){
        startDay = sD;
        endDay = eD;
        mContext = c;
        mInflater = LayoutInflater.from(c);
        if (sD > eD){
            startDay = eD;
            endDay = sD;

        }
        for (int i = startDay; i<= endDay; i++){
            theList.add(new Integer(i));
        }
    }

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

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View newView = convertView;
        DayHolder holder;
        Integer curr = (Integer) theList.get(position);
        if (null == convertView){
            holder = new DayHolder();
            newView = mInflater.inflate(R.layout.activity_select_day,null);
            holder.mText = (TextView) newView.findViewById(R.id.selectDays);
        }else{
            holder = (DayHolder) newView.getTag();
        }
        holder.mText.setText(curr.toString());
        return newView;
    }

    static class DayHolder{
        TextView mText;
    }
}

以下是错误消息:

11-30 12:49:58.030  15317-15317/apps.me.jj.recordtimeeveryhour E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at apps.me.jj.recordtimeeveryhour.DayAdapter.getView(DayAdapter.java:63)
            at android.widget.HeaderViewListAdapter.getView(HeaderViewListAdapter.java:220)
            at android.widget.AbsListView.obtainView(AbsListView.java:2031)
            at android.widget.ListView.makeAndAddView(ListView.java:1820)
            at android.widget.ListView.fillUp(ListView.java:705)
            at android.widget.ListView.fillGap(ListView.java:645)
            at android.widget.AbsListView.trackMotionScroll(AbsListView.java:4607)
            at android.widget.AbsListView.scrollIfNeeded(AbsListView.java:2871)
            at android.widget.AbsListView.onTouchEvent(AbsListView.java:3136)
            at android.view.View.dispatchTouchEvent(View.java:5579)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1959)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1720)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1965)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1734)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1965)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1734)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1965)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1734)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1982)
            at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1441)
            at android.app.Activity.dispatchTouchEvent(Activity.java:2421)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1930)
            at android.view.View.dispatchPointerEvent(View.java:5759)
            at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:2970)
            at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2529)
            at android.view.ViewRootImpl.processInputEvents(ViewRootImpl.java:869)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:883)
            at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2505)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:154)
            at android.app.ActivityThread.main(ActivityThread.java:4945)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
            at dalvik.system.NativeStart.main(Native Method)

ListView加载正常..但每当我开始滚动显示时,我都会收到此错误。任何想法如何解决这个问题,而不是彻底改变我的代码(即添加新的库,一个月前只学习Java和Android编程的人可能不熟悉。)

第63行:holder.mText.setText(curr.toString());

1 个答案:

答案 0 :(得分:1)

首先,不要使用LayoutInflater.from()。使用您从LayoutInflater获得的ActivityOtherwise, your layouts will not work properly

其次,请勿使用inflate(R.layout.activity_select_day,null);。使用inflate(R.layout.activity_select_day, parent, false);。否则,您的布局将无法正常运行when the root container of the layout file is a RelativeLayout

第三,如果您致电getTag(),则应致电setTag()。你不是;因此,您的getTag()来电将始终返回null。在创建newView.setTag(holder)实例后,在if块中致电holder