android.view.InflateException - 创建自定义视图的问题

时间:2014-03-16 04:21:57

标签: java android xml android-custom-view

我是Android开发的新手,并且非常努力使用自定义视图。我已经在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);
}

}

logcat输出

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

3 个答案:

答案 0 :(得分:1)

我确信您在创建customView时缺少其中一个构造函数,您必须覆盖这些构造函数 看到你堆栈跟踪后。

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

//上面的consturctor正在误导

注意:并在创建自定义视图时覆盖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
}

答案 1 :(得分:1)

如果您将内部类用作自定义视图类,则它必须为static。 inflater没有引用外部类来实例化它。

最好将自定义视图类作为内部类而不是内部类。

答案 2 :(得分:0)

这里的问题显然是您将View定义为活动的内部非静态类。 这意味着要实例化的视图需要Activity的一个实例。 至少让它静止不动。 顺便说一句你正在使用的onDraw太重了。您正在为每次绘制创建新的字体,解码和缩放位图。 尝试重构一下