自定义视图在 Android Lollipop 中不起作用
Custom view not working in Android Lollipop
我正在创建自定义视图。我在布局 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,我在所有版本中都可以正常工作。它在这一行中显示 InflateException (' setContentView(R.layout.activity_main);' ),应用程序立即在棒棒糖中崩溃。我如何为棒棒糖创建自定义视图。
自定义视图代码:
@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.access0(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.access0(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, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
您需要添加如下所示的构造函数:
public ViewClip(Context context, AttributeSet attrs)
从堆栈跟踪中可以清楚地看出这一点:
Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
表示缺少带有 Context
和 AttributeSet
参数类型的 <init>
(构造函数)。
我正在创建自定义视图。我在布局 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,我在所有版本中都可以正常工作。它在这一行中显示 InflateException (' setContentView(R.layout.activity_main);' ),应用程序立即在棒棒糖中崩溃。我如何为棒棒糖创建自定义视图。
自定义视图代码:
@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.access0(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.access0(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, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
您需要添加如下所示的构造函数:
public ViewClip(Context context, AttributeSet attrs)
从堆栈跟踪中可以清楚地看出这一点:
Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
表示缺少带有 Context
和 AttributeSet
参数类型的 <init>
(构造函数)。