膨胀自定义View类时出错

时间:2017-07-16 13:33:09

标签: android

我尝试开发一个扩展from itertools import cycle, islice var_new = list(islice(cycle(var),0,n) 的{​​{1}}类,它在画布上使用Checkerboard方法显示棋盘格。

但是,当我尝试将其包含在布局中时,Android会向我发出以下错误:

View

这是我的XML:

onDrawn

这是主要的活动,它会膨胀它:

07-16 13:26:34.677 2478-2478/? E/AndroidRuntime: FATAL EXCEPTION: main
                                             Process: vmc.chesslys, PID: 2478
                                             java.lang.RuntimeException: Unable to start activity ComponentInfo{vmc.chesslys/vmc.chesslys.MainActivity}: android.view.InflateException: Binary XML file line #11: Binary XML file line #11: Error inflating class vmc.chesslys.CheckerBoard
                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
                                                 at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
                                                 at android.os.Handler.dispatchMessage(Handler.java:102)
                                                 at android.os.Looper.loop(Looper.java:154)
                                                 at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
                                              Caused by: android.view.InflateException: Binary XML file line #11: Binary XML file line #11: Error inflating class vmc.chesslys.CheckerBoard
                                              Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class vmc.chesslys.CheckerBoard
                                              Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
                                                 at java.lang.Class.getConstructor0(Class.java:2204)
                                                 at java.lang.Class.getConstructor(Class.java:1683)
                                                 at android.view.LayoutInflater.createView(LayoutInflater.java:618)
                                                 at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:787)
                                                 at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
                                                 at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
                                                 at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
                                                 at android.view.LayoutInflater.parseInclude(LayoutInflater.java:994)
                                                 at android.view.LayoutInflater.rInflate(LayoutInflater.java:854)
                                                 at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
                                                 at android.view.LayoutInflater.parseInclude(LayoutInflater.java:994)
                                                 at android.view.LayoutInflater.rInflate(LayoutInflater.java:854)
                                                 at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
                                                 at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
                                                 at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
                                                 at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
                                                 at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:292)
                                                 at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
                                                 at vmc.chesslys.MainActivity.onCreate(MainActivity.java:22)
                                                 at android.app.Activity.performCreate(Activity.java:6662)
                                                 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
                                                 at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
                                                 at android.os.Handler.dispatchMessage(Handler.java:102)
                                                 at android.os.Looper.loop(Looper.java:154)
                                                 at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

你能帮帮我吗?

提前致谢

1 个答案:

答案 0 :(得分:4)

您的CheckerBoard课程缺少一个双参数public构造函数,其中包含ContextAttributeSet。这是LayoutInflater使用的构造函数。