2015-06-27 95 views
0

我正在創建自定義視圖。我在下面一行在佈局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 
+0

顯示,自定義視圖 –

+0

添加跟蹤(日誌從log.cat)的代碼,自定義視圖代碼 – once2go

+0

後總錯誤日誌 –

回答

1

您需要添加一個構造函數,如下所示:

public ViewClip(Context context, AttributeSet attrs) 

這是從堆棧跟蹤清楚: Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]

說,<init>(構造函數)與ContextAttributeSet參數類型丟失。

+0

謝謝wujek。它在視圖中工作膨脹在Activity中。但它在片段中不起作用(onCreateView) –

+0

然後,您應該顯示新的異常跟蹤。正如你從最初的問題看到的那樣,只有你提供了所有必要的信息,人們才能回答。 「不起作用」不會告訴我們任何事情。 – wujek

+1

好@wujek,我會發送 –

1

加入此構造

public ViewClip(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 
     super(context, attrs, defStyleAttr, defStyleRes); 

    } 
後重
+0

不,它再次崩潰。它顯示setContentView(R.layout.activity_main)中的錯誤; –