Ошибка пользовательского представления android: android.view.InflateException

Я создаю пользовательское view.but, когда я хочу его использовать, у меня случился сбой в моем приложении, это мое мнение:

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" /> 

Это логарифм:

 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) 

Добавить:

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

Для реализации конструктора, ожидаемого в рамках инфраструктуры размещения макета.

Если вы используете пользовательский вид. Android ожидает набор AttributeSet для внутреннего LayoutInflater.

Поэтому просто добавьте AttributeSet в свой CustomView Constructor и в его метод super (context, attributeSet).

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