自定义视图在Android Lollipop中不起作用

娜娜·桑达里(Gnana Soundari)

我正在创建一个自定义视图。我在布局XML xmlns中为该视图添加了以下行:my =“ http://schemas.android.com/apk/res-auto

<RelattiveLayout  xmlns:my="http://schemas.android.com/apk/res-auto">
<com.clippingtest.ViewClip
    android:layout_below="@id/tv"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

</com.clippingtest.ViewClip>
</RelativeLayout>

在MainActivity中添加了该视图。除了Lollipop,我在所有版本中都能正常工作。它在此行('setContentView(R.layout.activity_main);')中显示InflateException,并且应用程序立即在棒棒糖中崩溃。我如何为棒棒糖创建自定义视图。

CustomViewCode:

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    int width = getWidth();

    Paint paint = new Paint();
    paint.setStyle(Paint.Style.FILL);
    paint.setColor(Color.BLUE);
    paint.setStrokeWidth(2);
    Path path = new Path();
    path.moveTo(0,0);
    path.lineTo(width-180, 80);
    path.lineTo(width, 60);
    path.close();
    canvas.drawPath(path, paint);

}

错误代码:

   06-27 17:31:44.365  28812-28812/com.clippingtest E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.clippingtest, PID: 28812
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.clippingtest/com.clippingtest.MainActivity}: android.view.InflateException: Binary XML file line #13: Error inflating class com.clippingtest.ViewClip
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
        at android.app.ActivityThread.access$800(ActivityThread.java:151)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5257)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
 Caused by: android.view.InflateException: Binary XML file line #13: Error inflating class com.clippingtest.ViewClip
        at android.view.LayoutInflater.createView(LayoutInflater.java:616)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
        at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249)
        at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)
        at com.clippingtest.MainActivity.onCreate(MainActivity.java:14)
        at android.app.Activity.performCreate(Activity.java:5990)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)

            在

android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390) at android.app.ActivityThread.access$800(ActivityThread.java:151)

      

        

 Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
        at java.lang.Class.getConstructor(Class.java:531)
        at java.lang.Class.getConstructor(Class.java:495)
        at 
叔叔

您需要添加一个如下所示的构造函数:

public ViewClip(Context context, AttributeSet attrs)

从堆栈跟踪中可以很明显地看出这一点: Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]

表示缺少参数类型<init>(构造函数)ContextAttributeSet

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

自定义视图的Android边框底部不起作用

Android自定义视图onDraw不起作用

向 Xamarin 的 PDFTron.Android 中不起作用的页面添加自定义视图

动画列表在Android 5.0(Lollipop)中不起作用

相机的SurfaceView在Android Lollipop OS中不起作用

Android中的自定义视图

片段与Android中的自定义视图

在Android中旋转自定义视图

自定义进度可绘制在Android Lollipop(API 21)设备上不起作用

自定义背景资源在android中不起作用

android中的自定义listView不起作用

Android自定义组合视图保存和还原状态不起作用

IComponents视图的Nop Commerce自定义视图引擎在nopcommerce 4.0中不起作用

自定义视图上的 Onclicklistener 不起作用

搜索在自定义列表视图中不起作用

onitemclicklistener 在自定义列表视图中不起作用

集合视图作为自定义键盘不起作用

iOS 按钮在自定义视图中不起作用

Android Lollipop工具栏与自定义视图

将自定义视图添加到 NavigationBar 中不起作用?

UITapGestureRecognizer在自定义类中不起作用(不是视图控制器)

用Xib创建的自定义视图在Interface Builder中不起作用

MBProgressHud视图自定义在Swift 3中不起作用并且不显示吗?

在Android Studio中找不到我的自定义视图的自定义属性

将自定义样式设置为android中的自定义视图内的textviews

在自定义Dialog Android Kotlin中,视图为null

如何在Android中单击创建自定义视图

ListView中的Android自定义视图setSelected(true)撤消

Android:XML布局中的自定义视图