2011-02-04 61 views
0

我对Android开发非常陌生,在这里我遇到了一些小问题。 我想通过扩展TextView来创建一个自定义的TextView类,并在ListView中使用这个类(ListView的每一行都包含这个类的对象)。在Android中获取充气异常

我创建了我的自定义类这样的

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); 
} 
} 

,并为每一行ofmy的ListView我创建了一个XML结构,其中包括一个TextView的,我的自定义类,以及图像

<?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(列表视图中的每一行的布局结构),那么一切工作正常

我已经搜索论坛,但我没有得到我做错了什么。

请帮我解决这个问题。

+1

请问您能显示完整的错误信息吗? – WarrenFaith 2011-02-04 10:24:46

回答

1

我不知道这是你的错误的原因,但你的构造函数调用init()函数,而init函数使用那个mPaint变量。

mPaint变量声明为Paint mPaint;,但它永远不会实例化,所以它是空的。你会得到一个nullPointerException在那里,我想

我不知道mPaint作品的构造,但如何你需要做这样的事情之前,你可以使用它:

mPaint = new Paint mPaint(); 

编辑:啊:我看到你的onDraw()确实使用Paint paint = new Paint();。但那可能有点太晚了?

+0

非常感谢。这是唯一的问题。 thanx很多搞清楚 – nikki 2011-02-04 11:01:45