我正在創建自定義視圖。我在下面一行在佈局XML 的xmlns的觀點:我=「http://schemas.android.com/apk/res-auto自定義視圖不適用於Android棒棒糖
<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我查看工作正常。所有版本,除了棒棒糖它表明InflateException在這條線(「的setContentView(R.layout.activity_main);」),和應用程序崩潰立即棒棒糖我如何創建棒棒糖自定義視圖
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
顯示,自定義視圖 –
添加跟蹤(日誌從log.cat)的代碼,自定義視圖代碼 – once2go
後總錯誤日誌 –