Android检索适配器元素

时间:2014-03-16 15:03:47

标签: android listview nullpointerexception adapter

我的Android应用程序中出现了一个奇怪的问题。 我创建了自定义Adapter和自定义Progress bar。 在我的Adapter layout中,我有一个TextView和一个ProgressBarView,就像这样:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:orientation="vertical"
    android:baselineAligned="false">

    <TextView
        android:id="@+id/text_title"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:textStyle="bold"
        android:text="@string/stats_title" />

    <com.myapp.utils.ProgressBarView
        android:id="@+id/progressbar"
        android:layout_height="10dp"
        android:layout_width="fill_parent"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="20dp" />

</LinearLayout>

Adapter class我尝试检索这两个元素,所以我使用了以下代码:

public View getView(int position, View convertView, ViewGroup parent)
{
    UserStatsHolder holder = null;

    if (convertView == null)
    {
        LayoutInflater inflater = LayoutInflater.from(context);
        convertView = inflater.inflate(R.layout.adapter_user_stats, null);

        holder = new UserStatsHolder();

        // Retrieve layout elements
        holder.text_title = (TextView) convertView.findViewById(R.id.text_title);
        holder.progressbar = (ProgressBarView) convertView.findViewById(R.id.progressbar);

        convertView.setTag(holder);
    }
    else
    {
        holder = (UserStatsHolder) convertView.getTag();
    }

    Item item = getItem(position);
    if (item == null) return convertView;

    holder.text_title.setText( item.getTitle() );
    holder.progressbar.setValue( item.getValue() );

    return convertView;
}
  • holder.text_title确实存在且不是null
  • holder.progressbar null应该设置。

最后,ProgressBarView的代码是这样的:

package com.myapp.utils;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

public class ProgressBarView extends View
{
private Paint paint = null;
private int fillColor = Color.parseColor("#2D6EB9");
private int emptyColor = Color.parseColor("#233952");
private int value = 0;
private int max = 100;
private int displayedParts = 10;
private PorterDuffXfermode clearMode = null;
private RectF rectFill = null;
private RectF rectEmpty = null;
private List<RectF> separators = null;

public ProgressBarView(Context context)
{
    super(context);
    this.value = 0;
    this.max = 100;
    this.displayedParts = 10;
    this.paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    this.clearMode = new PorterDuffXfermode(Mode.CLEAR);
    this.separators = new ArrayList<RectF>();
}

public ProgressBarView(Context context, AttributeSet attrs)
{
    super(context);
    this.value = 0;
    this.max = 100;
    this.displayedParts = 10;
    this.paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    this.clearMode = new PorterDuffXfermode(Mode.CLEAR);
    this.separators = new ArrayList<RectF>();
}

public ProgressBarView(Context context, AttributeSet attrs, int defStyle)
{
    super(context, attrs, defStyle);
    this.value = 0;
    this.max = 100;
    this.displayedParts = 10;
    this.paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    this.clearMode = new PorterDuffXfermode(Mode.CLEAR);
    this.separators = new ArrayList<RectF>();
}

public ProgressBarView(Context context, int value, int max, int displayedParts)
{
    super(context);
    this.value = value;
    this.max = max;
    this.displayedParts = displayedParts;
    this.paint =  new Paint(Paint.ANTI_ALIAS_FLAG);
    this.clearMode = new PorterDuffXfermode(Mode.CLEAR);
    this.separators = new ArrayList<RectF>();
}

@Override
protected void onDraw(Canvas canvas)
{
    super.onDraw(canvas);

    // Foreground
    this.paint.setColor(this.fillColor);
    canvas.drawRect(this.rectFill, this.paint);

    // Background
    this.paint.setColor(this.emptyColor);
    canvas.drawRect(this.rectEmpty, this.paint);

    // Separator
    this.paint.setColor(Color.TRANSPARENT);
    this.paint.setXfermode(this.clearMode);
    for (RectF separator : this.separators)
    {
        canvas.drawRect(separator, this.paint);
    }
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
    int minw = getSuggestedMinimumWidth();
    int w = Math.max(minw, MeasureSpec.getSize(widthMeasureSpec));

    int minh = this.getSuggestedMinimumHeight();
    int h = Math.max(minh, MeasureSpec.getSize(heightMeasureSpec));

    setMeasuredDimension(w, h);
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh)
{
    int width = this.value * this.getWidth() / this.max;
    this.rectFill = new RectF(0, 0, width, this.getHeight());
    this.rectEmpty = new RectF(width, 0, this.getWidth(), this.getHeight());

    int spaceBetween = this.getWidth() / 100;
    int widthPart = this.getWidth() / this.displayedParts - (int)(0.9 * spaceBetween);
    int startX = widthPart;
    for (int i=0; i<this.displayedParts - 1; i++)
    {
        this.separators.add( new RectF(startX, 0, startX + spaceBetween, getHeight()) );
        startX += spaceBetween + widthPart;
    }
}

public int getValue()
{
    return value;
}

public void setValue(int value)
{
    this.value = value;
    this.invalidate();
}

public int getMax()
{
    return max;
}

public void setMax(int max)
{
    this.max = max;
    this.invalidate();
}

public int getDisplayedParts()
{
    return displayedParts;
}

public void setDisplayedParts(int displayedParts)
{
    this.displayedParts = displayedParts;
    this.invalidate();
}

}

所以,我的问题是:我做错了什么还是我的自定义进度条视图格式不正确?

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

解决方案非常简单:在我的自定义视图中,我必须将构造函数更改为

public ProgressBarView(Context context, AttributeSet attrs)
{
    super(context, attrs);