热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

我创建了一个自定义视图.但是当我想使用它时,我的应用程序崩溃了这是我的看法:packagecom.example.pooyafayyaz.first_photo_edit_test

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

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导致这个错误.但我不知道这是什么问题.

xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/View"
android:layout_
android:layout_
/>

这是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: [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: [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)

解决方法:

加:

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

实现布局通胀框架预期的构造函数.


推荐阅读
author-avatar
Vee-健健健
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有