2

我的列表视图在 getView 崩溃。当应用程序第一次加载时,它运行良好,但一旦我离开它或者我截取屏幕截图并尝试分享应用程序崩溃。这是自定义适配器。

public class CustomAdapter extends BaseAdapter
{
private List<AlarmItem> alarms;
private Context context;

public CustomAdapter(List<AlarmItem> alarms,Context context)
{
    this.context = context;
    this.alarms = alarms;
}


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

@Override
public AlarmItem getItem(int position) {
    return alarms.get(position);
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent)
{

    final AlarmItem item = getItem(position);
    Holder holder;

    if(convertView==null)
    {
        LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = layoutInflater.inflate(R.layout.my_alarm_row,parent,false);
        holder = new Holder();
        holder.image= (RoundedImageView)convertView.findViewById(R.id.picture);
        holder.wakeText =(RobotoLight)convertView.findViewById(R.id.waketext);
        holder.name =(RobotoRegular)convertView.findViewById(R.id.name);
        holder.period = (RobotoLight)convertView.findViewById(R.id.period);
        holder.time = (RobotoRegular)convertView.findViewById(R.id.time);
        holder.day1 =(RobotoLight)convertView.findViewById(R.id.day1);
        holder.day2 =(RobotoLight)convertView.findViewById(R.id.day2);
        holder.day3 =(RobotoLight)convertView.findViewById(R.id.day3);
        holder.day4 =(RobotoLight)convertView.findViewById(R.id.day4);
        holder.day5 =(RobotoLight)convertView.findViewById(R.id.day5);
        holder.day6 =(RobotoLight)convertView.findViewById(R.id.day6);
        holder.day7 =(RobotoLight)convertView.findViewById(R.id.day7);
    }
    else
    {
        holder = (Holder)convertView.getTag();
    }


    UrlImageViewHelper.setUrlDrawable(holder.image, "http://graph.facebook.com/" + item.getProfileId().trim() + "/picture?style=large");


    holder.wakeText.setText(context.getString(R.string.wake_message));
    holder.name.setText(item.getName()+" ");
    holder.period.setText(item.getPeriod());
    holder.time.setText(item.getTime());

    /**
     * This block code places sets the color of the particular day. If its active
     * It sets it to a particular colour if not then the inactive colour remains.
     */
    List<RobotoLight> dayList = new ArrayList<RobotoLight>();
    dayList.add(holder.day1);
    dayList.add(holder.day2);
    dayList.add(holder.day3);
    dayList.add(holder.day4);
    dayList.add(holder.day5);
    dayList.add(holder.day6);
    dayList.add(holder.day7);


    for(int x=0;x<7;x++)
    {
        /**
         * if the day at a particular point in the string is equal to one that means its active
         * therefore set active color.
         */
        if(item.getDays().charAt(x) == '1')
        {
             dayList.get(x).setTextColor(R.color.alarm_day_active);
        }
        else
        {
            dayList.get(x).setTextColor(R.color.alarm_day_inactive);
        }
    }


    return convertView;
}

public static class Holder
{

    RoundedImageView image;
    RobotoLight wakeText;
    RobotoRegular name;
    RobotoLight period;
    RobotoRegular time;
    RobotoLight day1;
    RobotoLight day2;
    RobotoLight day3;
    RobotoLight day4;
    RobotoLight day5;
    RobotoLight day6;
    RobotoLight day7;
}



}

这是我的日志猫

    09-04 08:02:45.948: ERROR/AndroidRuntime(9402): FATAL EXCEPTION: main
    java.lang.NullPointerException
    at com.wake.friend.adapter.AlarmAdapter.getView(AlarmAdapter.java:82)
    at android.widget.AbsListView.obtainView(AbsListView.java:2334)
    at android.widget.ListView.makeAndAddView(ListView.java:1937)
    at android.widget.ListView.fillSpecific(ListView.java:1483)
    at android.widget.ListView.layoutChildren(ListView.java:1765)
    at android.widget.AbsListView.onLayout(AbsListView.java:2178)
    at android.view.View.layout(View.java)
    at android.view.ViewGroup.layout(ViewGroup.java:4380)
    at android.widget.RelativeLayout.onLayout(RelativeLayout.java:948)
    at android.view.View.layout(View.java)
    at android.view.ViewGroup.layout(ViewGroup.java:4380)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
    at android.view.View.layout(View.java)
    at android.view.ViewGroup.layout(ViewGroup.java:4380)
    at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1141)
    at android.view.View.layout(View.java)

他们所说的第 82 行是抛出空指针异常。

UrlImageViewHelper.setUrlDrawable(holder.image, "http://graph.facebook.com/" + item.getProfileId().trim() + "/picture?style=large");

我正在使用图像下载助手从网络上检索图像。

4

2 回答 2

1

发生此错误的原因是因为您在检查 convertView 是否为空的 if 语句之外访问持有者类的元素。因此它第一次工作,但是当再次调用列表视图并且 convertView 不为空时,就不能以相同的方式访问持有者。因此,最好将所有分配都放在 if 函数中,就像这样。

 public class CustomAdapter extends BaseAdapter
{
private List<AlarmItem> alarms;
private Context context;

public CustomAdapter(List<AlarmItem> alarms,Context context)
{
this.context = context;
this.alarms = alarms;
}


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

@Override
public AlarmItem getItem(int position) {
return alarms.get(position);
}

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

@Override
 public View getView(int position, View convertView, ViewGroup parent)
 {

final AlarmItem item = getItem(position);
Holder holder;

if(convertView==null)
{
    LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = layoutInflater.inflate(R.layout.my_alarm_row,parent,false);
    holder = new Holder();
    holder.image= (RoundedImageView)convertView.findViewById(R.id.picture);
    holder.wakeText =(RobotoLight)convertView.findViewById(R.id.waketext);
    holder.name =(RobotoRegular)convertView.findViewById(R.id.name);
    holder.period = (RobotoLight)convertView.findViewById(R.id.period);
    holder.time = (RobotoRegular)convertView.findViewById(R.id.time);
    holder.day1 =(RobotoLight)convertView.findViewById(R.id.day1);
    holder.day2 =(RobotoLight)convertView.findViewById(R.id.day2);
    holder.day3 =(RobotoLight)convertView.findViewById(R.id.day3);
    holder.day4 =(RobotoLight)convertView.findViewById(R.id.day4);
    holder.day5 =(RobotoLight)convertView.findViewById(R.id.day5);
    holder.day6 =(RobotoLight)convertView.findViewById(R.id.day6);
    holder.day7 =(RobotoLight)convertView.findViewById(R.id.day7);

    UrlImageViewHelper.setUrlDrawable(holder.image, "http://graph.facebook.com/" + item.getProfileId().trim() + "/picture?style=large");


    holder.wakeText.setText(context.getString(R.string.wake_message));
    holder.name.setText(item.getName()+" ");
    holder.period.setText(item.getPeriod());
    holder.time.setText(item.getTime());

    /**
     * This block code places sets the color of the particular day. If its active
     * It sets it to a particular colour if not then the inactive colour remains.
     */
    List<RobotoLight> dayList = new ArrayList<RobotoLight>();
    dayList.add(holder.day1);
    dayList.add(holder.day2);
    dayList.add(holder.day3);
    dayList.add(holder.day4);
    dayList.add(holder.day5);
    dayList.add(holder.day6);
    dayList.add(holder.day7);


    for(int x=0;x<7;x++)
    {
        /**
         * if the day at a particular point in the string is equal to one that means its active
         * therefore set active color.
         */
        if(item.getDays().charAt(x) == '1')
        {
             dayList.get(x).setTextColor(R.color.alarm_day_active);
        }
        else
        {
            dayList.get(x).setTextColor(R.color.alarm_day_inactive);
        }
    }
}
else
{
    holder = (Holder)convertView.getTag();
}





return convertView;
}

public static class Holder
{

RoundedImageView image;
RobotoLight wakeText;
RobotoRegular name;
RobotoLight period;
RobotoRegular time;
RobotoLight day1;
RobotoLight day2;
RobotoLight day3;
RobotoLight day4;
RobotoLight day5;
RobotoLight day6;
RobotoLight day7;
}



}
于 2013-09-04T13:42:56.210 回答
0

也许item.getProfileId()返回null。

于 2013-09-04T13:07:23.637 回答