我正在创建自定义视图。我在Layout XML中为视图添加了以下行 的的xmlns:我=“http://schemas.android.com/apk/res-auto
@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);
}
在MainActivity中添加了该视图。除了Lollipop之外,我在所有版本都能正常使用。它在这一行显示InflateException('setContentView(R.layout.activity_main);'),并且应用程序在棒棒糖中立即崩溃。我如何为棒棒糖创建自定义视图。
CustomViewCode:
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)
at
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
T
答案 0 :(得分:1)
添加此构造函数后尝试
public ViewClip(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
答案 1 :(得分:1)
您需要添加一个如下所示的构造函数:
public ViewClip(Context context, AttributeSet attrs)
从堆栈跟踪中可以清楚地看到:
Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
说缺少<init>
和Context
参数类型的AttributeSet
(构造函数)。