android自定义视图错误:android.view.InflateException

时间:2015-03-27 21:25:03

标签: android canvas view logcat android-logcat

我创建了一个自定义视图。但是当我想使用它时,我的应用程序崩溃了 这是我的观点:

package com.example.pooyafayyaz.first_photo_edit_test;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

/**
 * Created by pooyafayyaz on 3/27/2015.
 */
public class CanvasView extends View {
    ImageView img;
    Bitmap canvasBitmap;
    Canvas drawCanvas;
    Drawable drawable;

    public CanvasView(Context context) {
        super(context);
//        img = img_taken;
    }
    public void setCanvasPath(String bitmap_path) {
        BitmapFactory.Options decode_options = new BitmapFactory.Options();
        decode_options.inMutable = true;
        canvasBitmap  = BitmapFactory.decodeFile(bitmap_path,decode_options);
        drawCanvas = new Canvas(canvasBitmap);
//        img.setImageBitmap(canvasBitmap);
        invalidate();
    }
    @Override

    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (canvasBitmap != null) {
            canvas.drawBitmap(canvasBitmap, 0, 0, null);
        }

    }
}

这是xml文件我认为xml导致了这个错误。但是我不知道这个问题是什么。 :

<com.example.pooyafayyaz.first_photo_edit_test.CanvasView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/View"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    /> 

这是logcat:

03-28 01:40:02.690  16581-16581/com.example.pooyafayyaz.first_photo_edit_test E/Trace﹕ error opening trace file: No such file or directory (2)
03-28 01:40:05.400  16581-16581/com.example.pooyafayyaz.first_photo_edit_test E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.pooyafayyaz.first_photo_edit_test/com.example.pooyafayyaz.first_photo_edit_test.second}: android.view.InflateException: Binary XML file line #2: Error inflating class com.example.pooyafayyaz.first_photo_edit_test.CanvasView
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2332)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2368)
            at android.app.ActivityThread.access$600(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1330)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1074)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:841)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class com.example.pooyafayyaz.first_photo_edit_test.CanvasView
            at android.view.LayoutInflater.createView(LayoutInflater.java:596)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
            at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:240)
            at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
            at com.example.pooyafayyaz.first_photo_edit_test.second.onCreate(second.java:25)
            at android.app.Activity.performCreate(Activity.java:5066)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1102)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2288)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2368)
            at android.app.ActivityThread.access$600(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1330)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:155)
            at android.app.ActivityThread.main(ActivityThread.java:5536)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1074)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:841)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
            at java.lang.Class.getConstructorOrMethod(Class.java:460)
            at java.lang.Class.getConstructor(Class.java:431)
            at android.view.LayoutInflater.createView(LayoutInflater.java:561)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
            at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:240)
            at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
            at com.example.pooyafayyaz.first_photo_edit_test.second.onCreate(second.java:25)
            at android.app.Activity.performCreate(Activity.java:5066)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1102)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2288)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2368)
            at android.app.ActivityThread.access$600(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1330)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:155)
            at android.app.ActivityThread.main(ActivityThread.java:5536)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1074)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:841)
            at dalvik.system.NativeStart.main(Native Method)
03-28 01:44:19.180  17830-17830/com.example.pooyafayyaz.first_photo_edit_test E/Trace﹕ error opening trace file: No such file or directory (2)
03-28 01:44:25.750  17830-17830/com.example.pooyafayyaz.first_photo_edit_test E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.pooyafayyaz.first_photo_edit_test/com.example.pooyafayyaz.first_photo_edit_test.second}: android.view.InflateException: Binary XML file line #2: Error inflating class com.example.pooyafayyaz.first_photo_edit_test.CanvasView
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2332)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2368)
            at android.app.ActivityThread.access$600(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1330)

     Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class com.example.pooyafayyaz.first_photo_edit_test.CanvasView
            at android.view.LayoutInflater.createView(LayoutInflater.java:596)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
            at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:240)
            at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
            at com.example.pooyafayyaz.first_photo_edit_test.second.onCreate(second.java:25)
            at android.app.Activity.performCreate(Activity.java:5066)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1102)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2288)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2368)
            at android.app.ActivityThread.access$600(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1330)
            at android.os.Handler.dispatchMessage(Handler.java:99)
     Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
            at java.lang.Class.getConstructorOrMethod(Class.java:460)
            at java.lang.Class.getConstructor(Class.java:431)
            at android.view.LayoutInflater.createView(LayoutInflater.java:561)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
            at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:240)
            at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
            at com.example.pooyafayyaz.first_photo_edit_test.second.onCreate(second.java:25)
            at android.app.Activity.performCreate(Activity.java:5066)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1102)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2288)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2368)
            at android.app.ActivityThread.access$600(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1330)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:155)
            at android.app.ActivityThread.main(ActivityThread.java:5536)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1074)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:841)
            at dalvik.system.NativeStart.main(Native Method)
03-28 01:46:04.460  19120-19120/com.example.pooyafayyaz.first_photo_edit_test E/Trace﹕ error opening trace file: No such file or directory (2)
03-28 01:46:06.290  19120-19120/com.example.pooyafayyaz.first_photo_edit_test E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.pooyafayyaz.first_photo_edit_test/com.example.pooyafayyaz.first_photo_edit_test.second}: android.view.InflateException: Binary XML file line #2: Error inflating class com.example.pooyafayyaz.first_photo_edit_test.CanvasView
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2332)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2368)
            at android.app.ActivityThread.access$600(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1330)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:155)
            at android.app.ActivityThread.main(ActivityThread.java:5536)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1074)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:841)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class com.example.pooyafayyaz.first_photo_edit_test.CanvasView
            at android.view.LayoutInflater.createView(LayoutInflater.java:596)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
            at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:240)
            at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
            at com.example.pooyafayyaz.first_photo_edit_test.second.onCreate(second.java:25)
            at android.app.Activity.performCreate(Activity.java:5066)

2 个答案:

答案 0 :(得分:3)

添加:

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

实现布局通胀框架所期望的构造函数。

答案 1 :(得分:0)

如果您使用的是自定义视图。 Android期望内部LayoutInflater的一个AttributeSet。

所以只需将AttributeSet添加到CustomView构造函数及其super(context,attributeSet)方法中。

public CustomView(Context context, AttributeSet attributeSet{
   super(context,attributeSet);
}