0

私はAndroid開発に非常に慣れていないので、ここで少し問題に直面しています。TextViewを拡張してカスタムTextViewクラスを作成し、このクラスをListViewで使用したいと思います(ListViewのすべての行にこのクラスのオブジェクトが含まれています)。

このようなカスタムクラスを作成しました

public class CustomTextView extends TextView{

Context context;
Paint mPaint;
String text;

public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
}

public CustomTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

public CustomTextView(Context context) {
    super(context);
    init();
}


private void init(){
    setPadding(3, 3, 3, 3);
    mPaint.setTextSize(16);
    mPaint.setColor(Color.GREEN);
    mPaint.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
}

@Override
public void setText(CharSequence text, BufferType type) {

    this.text = (String) text;
    super.setText(text, type);
}


@Override
  protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    Rect rect = new Rect();
    Paint paint = new Paint();
    paint.setStyle(Paint.Style.STROKE);
    paint.setColor(Color.WHITE);
    paint.setStrokeWidth(3);
    getLocalVisibleRect(rect);
    canvas.drawRect(rect, paint);       
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    int reqWidth;
    int reqHeight;

    // width & height mode
    int widthMode = MeasureSpec.getMode(widthMeasureSpec);
    int heightMode = MeasureSpec.getMode(heightMeasureSpec);
    // specified width & height
    int widthSize = MeasureSpec.getSize(widthMeasureSpec);
    int heightSize = MeasureSpec.getSize(heightMeasureSpec);

    // find out Width based on widthMode
    if(widthMode == MeasureSpec.EXACTLY)
        // set user specified Width
        reqWidth = widthSize;
    else
        // find out the total pixel size required for first and last text
        reqWidth = (int)(mPaint.measureText(text)) ;

    // find out Height based on heightMode
    if(heightMode == MeasureSpec.EXACTLY)
        // set user specified Height
        reqHeight = heightSize;
    else
        // get the default height of the Font
        reqHeight = (int) mPaint.getTextSize();

    // set the calculated width and height of your drawing area
    setMeasuredDimension(reqWidth, reqHeight);
}
}

ListViewの各行に対して、TextView、カスタムクラス、および画像で構成されるxml構造を作成しました。

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_height="wrap_content" 
 android:gravity="left|center"
 android:layout_width="wrap_content" 
 android:paddingBottom="5px"
 android:paddingTop="5px" 
 android:paddingLeft="5px">
 <TextView android:id="@+id/TextView01" 
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" 
 android:layout_marginRight="5dip"
 android:gravity="center"
 android:background="@drawable/bg" 
 android:textColor="#FFFF00"
 android:text="hi"/>

<!--<TextView android:id="@+id/TextView02" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content"
android:layout_marginLeft="10px" 
android:text="hi"/>

-->
<com.example.CustomTextView  
    android:id="@+id/TextView02"
    android:layout_width="fill_parent" 
    android:layout_height="50px"

/>   


<ImageView android:id = "@+id/image"
android:src="@drawable/icon"
android:layout_width = "50dip"
android:layout_height = "50dip"

/>
</AbsoluteLayout>

ListViewのAdapterクラスで、次のような構造を取得しています

public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if (convertView == null) {
         convertView =  mInflator.inflate(R.layout.listview, null);

         convertView.setMinimumHeight(80);

         holder = new ViewHolder();
         holder.text = (TextView) convertView
         .findViewById(R.id.TextView01);


         holder.text2 = (CustomTextView) convertView
         .findViewById(R.id.TextView02);
}

しかし、私は

 convertView =  mInflator.inflate(R.layout.listview, null);

ただし、カスタムビューの代わりに、layout.xml(リストビューの各行のレイアウト構造)に単純なtextViewを追加すると、すべてが正常に機能します。

フォーラムを検索しましたが、間違っていることがわかりません。

この場合、私を助けてください。

4

1 に答える 1

1

これがエラーの原因かどうかはわかりませんが、コンストラクターがinit()関数を呼び出し、init 関数がそのmPaint変数を使用しています。

mPaint変数は として宣言されていPaint mPaint;ますが、インスタンス化されていないため、null です。あなたはnullPointerException私が思うそこに着くでしょう

のコンストラクターがどのように機能するかはわかりませんが、mPaint使用する前に次のようなことを行う必要があります。

mPaint = new Paint mPaint();

編集:ああ:私はあなたが使用していることを見てonDraw() ますPaint paint = new Paint();。しかし、それは少し遅すぎるかもしれませんか?

于 2011-02-04T10:35:28.800 に答える