java - android.view.InflateException - 创建自定义 View 的问题

标签 java android xml android-custom-view

我是 Android 开发的新手,在使用自定义 View 方面确实很吃力。我已经阅读了很多关于 SO 的内容,它对其他所有内容都非常有帮助,我似乎无法解决这个问题。你们很聪明,对自己的东西了如指掌。我希望有一天能回馈 SO,而不是总是索取!任何帮助将不胜感激。谢谢。

activity_display_message.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
    <TextView  
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/share"
    />
    <view class="me.myfirstapp.DisplayMessageActivity$MyView"
        android:layout_width="150dp"
        android:layout_height="100dp"
    />

DisplayMessageActivity.java

package me.myfirstapp;
import android.os.Bundle; 
import android.app.Activity;
import android.util.AttributeSet;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ShareActionProvider;
import android.support.v4.app.NavUtils;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.os.Build;

@SuppressLint({ "NewApi", "DrawAllocation" }) public class DisplayMessageActivity extends Activity {

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display_message);

    // Make sure we're running on Honeycomb or higher to use ActionBar APIs
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        // Show the Up button in the action bar.
        getActionBar().setDisplayHomeAsUpEnabled(true);
    }

}

public class MyView extends View {

    private int mWidth;
    private int mHeight;

    public MyView(Context context) {
         super(context);
    }

    public MyView(Context context, AttributeSet attribs) {
        super(context, attribs);
    }

    public MyView(Context context, AttributeSet attribs, int defStyle) {
        super(context, attribs, defStyle);
    }

    @Override        
    protected void onDraw(Canvas canvas) {

       super.onDraw(canvas);
       Paint paint = new Paint();

       Bitmap b1=BitmapFactory.decodeResource(getResources(), R.drawable.logolong);
       b1 = Bitmap.createScaledBitmap(b1, 100, 20, false);
       canvas.drawBitmap(b1, 600, 880, paint);

       Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/regencielight.TTF");
       paint.setTypeface(tf);
       paint.setColor(Color.parseColor("#ffffff"));
       paint.setTextSize(46);
       canvas.drawText(message, x1, 880, paint);

       }  

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            mWidth = View.MeasureSpec.getSize(widthMeasureSpec);
            mHeight = View.MeasureSpec.getSize(heightMeasureSpec);
            setMeasuredDimension(mWidth, mHeight);
        }

   }


@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        NavUtils.navigateUpFromSameTask(this);
        return true;
    }
    return super.onOptionsItemSelected(item);
}

日志输出

03-15 21:14:23.382: E/AndroidRuntime(25443): FATAL EXCEPTION: main
03-15 21:14:23.382: E/AndroidRuntime(25443): Process: me.myfirstapp, PID: 25443
03-15 21:14:23.382: E/AndroidRuntime(25443): java.lang.RuntimeException: Unable to start     activity ComponentInfo{me.myfirstapp/me.myfirstapp.DisplayMessageActivity}:     android.view.InflateException: Binary XML file line #12: Error inflating class     me.myfirstapp.DisplayMessageActivity$MyView
03-15 21:14:23.382: E/AndroidRuntime(25443):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
03-15 21:14:23.382: E/AndroidRuntime(25443):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
03-15 21:14:23.382: E/AndroidRuntime(25443):    at android.app.ActivityThread.access$800(ActivityThread.java:135)
03-15 21:14:23.382: E/AndroidRuntime(25443):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
03-15 21:14:23.382: E/AndroidRuntime(25443):    at android.os.Handler.dispatchMessage(Handler.java:102)
03-15 21:14:23.382: E/AndroidRuntime(25443):    at android.os.Looper.loop(Looper.java:136)
03-15 21:14:23.382: E/AndroidRuntime(25443):    at android.app.ActivityThread.main(ActivityThread.java:5017)
03-15 21:14:23.382: E/AndroidRuntime(25443):    at java.lang.reflect.Method.invokeNative(Native Method)
03-15 21:14:23.382: E/AndroidRuntime(25443):    at java.lang.reflect.Method.invoke(Method.java:515)
03-15 21:14:23.382: E/AndroidRuntime(25443):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-15 21:14:23.382: E/AndroidRuntime(25443):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-15 21:14:23.382: E/AndroidRuntime(25443):    at dalvik.system.NativeStart.main(Native Method)
03-15 21:14:23.382: E/AndroidRuntime(25443): Caused by: android.view.InflateException: Binary XML file line #12: Error inflating class me.myfirstapp.DisplayMessageActivity$MyView
03-15 21:14:23.382: E/AndroidRuntime(25443):    at android.view.LayoutInflater.createView(LayoutInflater.java:603)
03-15 21:14:23.382: E/AndroidRuntime(25443):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
03-15 21:14:23.382: E/AndroidRuntime(25443):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
03-15 21:14:23.382: E/AndroidRuntime(25443):    at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
03-15 21:14:23.382: E/AndroidRuntime(25443):    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
03-15 21:14:23.382: E/AndroidRuntime(25443):    at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
03-15 21:14:23.382: E/AndroidRuntime(25443):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
03-15 21:14:23.382: E/AndroidRuntime(25443):    at android.app.Activity.setContentView(Activity.java:1929)
03-15 21:14:23.382: E/AndroidRuntime(25443):    at me.myfirstapp.DisplayMessageActivity.onCreate(DisplayMessageActivity.java:44)
03-15 21:14:23.382: E/AndroidRuntime(25443):    at android.app.Activity.performCreate(Activity.java:5231)
03-15 21:14:23.382: E/AndroidRuntime(25443):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-15 21:14:23.382: E/AndroidRuntime(25443):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
03-15 21:14:23.382: E/AndroidRuntime(25443):    ... 11 more
03-15 21:14:23.382: E/AndroidRuntime(25443): Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
03-15 21:14:23.382: E/AndroidRuntime(25443):    at java.lang.Class.getConstructorOrMethod(Class.java:472)
03-15 21:14:23.382: E/AndroidRuntime(25443):    at java.lang.Class.getConstructor(Class.java:446)
03-15 21:14:23.382: E/AndroidRuntime(25443):    at android.view.LayoutInflater.createView(LayoutInflater.java:568)
03-15 21:14:23.382: E/AndroidRuntime(25443):    ... 22 more

最佳答案

我敢肯定,每当您创建 customView 时,您都缺少构造函数之一,您必须重写这些构造函数 在看到你的堆栈跟踪之后。

 Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]

//上面的构造函数丢失了

注意:并且在创建自定义 View 时覆盖 onMeasure 。这是强制性的

这不是强制性的,大多数时候您甚至不必担心它,只要您从 View 提供构造函数并将它们传递给 super()。但是您必须重写第二个构造函数才能完成您的工作。 ..

public CustomView(Context context)  // No Attributes in this one.
{
  super(context);
  // Your code here
}

public CustomView(Context context, AttributeSet attrs)
{
  super(context, attrs);
  // Your code here
}

public CustomView(Context context, AttributeSet attrs, int default_style)
{
  super(context, attrs, default_style);
  // Your code here
}

关于java - android.view.InflateException - 创建自定义 View 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22432901/

相关文章:

python - 我如何阻止漂亮的汤在解析时跳过行?

尝试解析 XML 文件时 Java 内存不足

java - 将 OneToMany 方映射为所有者

Android 相机预览颜色问题(红色和蓝色交换)

java - 在 Android App 和 Google AppEngine 之间共享 Android 库

java - 尝试与 Android Intent 共享图像

java - 使用 XSLT 转换 XML 和保留 Unicode 字符

java - Richfaces 中带注释的 ManagedBean?

java - GridBagLayout:如何填充所有空白区域

java - 无法启用 SSLv2Hello