Android 中出现 inflate 异常

发布于 2024-10-15 14:08:56 字数 4300 浏览 4 评论 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);

但是,如果我不是在自定义视图中添加一个简单的textView,而是在layout.xml(列表视图每行的布局结构)中添加一个简单的textView,那么一切都会正常工作

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

在这种情况下请帮助我。

I am very new to Android development and I am facing a little problem here.
I want to create a custom TextView class by extending TextView and use this class in ListView(every row of ListView contains this class' object).

I have created my custom class like this

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

and for each row ofmy ListView I have created a xml structure consisting of a TextView,My custom class, and an image

<?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>

In the Adapter class of my ListView I am retrieving the structure like this

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

But I am getting inflate exception on

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

However, if instead of my custom view , I add a simple textView in layout.xml(layout structure of each row of listview) then everything works fine

I have searched forums but I am not getting what i am doing wrong.

Please help me in this case.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幸福%小乖 2024-10-22 14:08:56

我不知道这是否是导致错误的原因,但您的构造函数调用 init() 函数,并且 init 函数使用该 mPaint 变量。

mPaint 变量被声明为 Paint mPaint;,但它从未实例化,因此它为 null。你会得到一个 nullPointerException ,我想

我不知道 mPaint 的构造函数是如何工作的,但你需要在使用它之前执行类似的操作:

mPaint = new Paint mPaint();

编辑:啊:我发现您的 onDraw() 确实使用 Paint Paint = new Paint();。但这可能有点太晚了?

I don't know if this is the cause of your error, but your constructors call the init() function, and the init function uses that mPaint variable.

The mPaint variable is declared as Paint mPaint;, but it is never instantiated, so it is null. You'll be getting a nullPointerException there i think

I don't know how the constructor of mPaint works, but you need to do something like this before you can use it:

mPaint = new Paint mPaint();

edit: ah: i'm seeing that your onDraw() does use Paint paint = new Paint();. But that might be a bit too late?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文