2013-01-06 117 views
1

我在我的應用程序的自定義視圖:錯誤添加自己的自定義視圖XML佈局

public class DrawView extends View { 
    Paint paint = new Paint(); 

    public DrawView(Context context) { 
     super(context);    
    } 
    public DrawView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public DrawView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
    } 

,我想這個觀點是這樣添加到我的活動的XML佈局:

<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <FrameLayout 
    android:id="@+id/camera_preview" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"/> 
    <test.example.myfirstapp.DrawView 
     android:id="@+id/mostatil" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/button_capture" 
     android:layout_alignParentRight="true"/> 
    <Button 
     android:id="@+id/button_capture" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginBottom="16dp" 
     android:onClick="capture" 
     android:text="@string/capture_button" /> 
     </RelativeLayout> 

但是當我嘗試運行此活動也有一些象這樣的錯誤:

01-06 14:26:00.897: E/AndroidRuntime(1933): FATAL EXCEPTION: main 
01-06 14:26:00.897: E/AndroidRuntime(1933): java.lang.RuntimeException: Unable to start activity ComponentInfo{test.example.myfirstapp/test.example.myfirstapp.CameraActivity}: android.view.InflateException: Binary XML file line #13: Error inflating class test.example.myfirstapp.DrawView 

我的程序包名是test.ex ample.myfirstapp和XML文件是一個名爲「CameraActivity」活動

編輯:這裏是logcat中的一些額外的線路:

01-06 14:16:20.708: E/AndroidRuntime(1849):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 
01-06 14:16:20.708: E/AndroidRuntime(1849):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
01-06 14:16:20.708: E/AndroidRuntime(1849):  at android.app.ActivityThread.access$600(ActivityThread.java:141) 
01-06 14:16:20.708: E/AndroidRuntime(1849):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
01-06 14:16:20.708: E/AndroidRuntime(1849):  at android.os.Handler.dispatchMessage(Handler.java:99) 
01-06 14:16:20.708: E/AndroidRuntime(1849):  at android.os.Looper.loop(Looper.java:137) 
01-06 14:16:20.708: E/AndroidRuntime(1849):  at android.app.ActivityThread.main(ActivityThread.java:5039) 
01-06 14:16:20.708: E/AndroidRuntime(1849):  at java.lang.reflect.Method.invokeNative(Native Method) 
01-06 14:16:20.708: E/AndroidRuntime(1849):  at java.lang.reflect.Method.invoke(Method.java:511) 
01-06 14:16:20.708: E/AndroidRuntime(1849):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
01-06 14:16:20.708: E/AndroidRuntime(1849):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
01-06 14:16:20.708: E/AndroidRuntime(1849):  at dalvik.system.NativeStart.main(Native Method) 
01-06 14:16:20.708: E/AndroidRuntime(1849): Caused by: android.view.InflateException: Binary XML file line #13: Error inflating class test.example.myfirstapp 
01-06 14:16:20.708: E/AndroidRuntime(1849):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:698) 
01-06 14:16:20.708: E/AndroidRuntime(1849):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:746) 
01-06 14:16:20.708: E/AndroidRuntime(1849):  at android.view.LayoutInflater.inflate(LayoutInflater.java:489) 
01-06 14:16:20.708: E/AndroidRuntime(1849):  at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 
01-06 14:16:20.708: E/AndroidRuntime(1849):  at android.view.LayoutInflater.inflate(LayoutInflater.java:352) 
01-06 14:16:20.708: E/AndroidRuntime(1849):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270) 
01-06 14:16:20.708: E/AndroidRuntime(1849):  at android.app.Activity.setContentView(Activity.java:1881) 
01-06 14:16:20.708: E/AndroidRuntime(1849):  at test.example.myfirstapp.CameraActivity.onCreate(CameraActivity.java:77) 
01-06 14:16:20.708: E/AndroidRuntime(1849):  at android.app.Activity.performCreate(Activity.java:5104) 
01-06 14:16:20.708: E/AndroidRuntime(1849):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 
01-06 14:16:20.708: E/AndroidRuntime(1849):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 
01-06 14:16:20.708: E/AndroidRuntime(1849):  ... 11 more 
01-06 14:16:20.708: E/AndroidRuntime(1849): Caused by: java.lang.ClassNotFoundException: Didn't find class "test.example.myfirstapp" on path: /data/app/test.example.myfirstapp-1.apk 
01-06 14:16:20.708: E/AndroidRuntime(1849):  at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65) 
01-06 14:16:20.708: E/AndroidRuntime(1849):  at java.lang.ClassLoader.loadClass(ClassLoader.java:501) 
01-06 14:16:20.708: E/AndroidRuntime(1849):  at java.lang.ClassLoader.loadClass(ClassLoader.java:461) 
01-06 14:16:20.708: E/AndroidRuntime(1849):  at android.view.LayoutInflater.createView(LayoutInflater.java:552) 
01-06 14:16:20.708: E/AndroidRuntime(1849):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687) 

當我打開「圖形佈局」模式我的XML佈局,有圖形佈局下此錯誤:

The following classes could not be instantiated: 
    - test.example.myfirstapp.CameraActivity.DrawView (Open Class, Show Error Log) 
    See the Error Log (Window > Show View) for more details. 
    Tip: Use View.isInEditMode() in your custom views to skip code when shown in Eclipse 
+0

我建議你粘貼更多的logcat,因爲實際的堆棧跟蹤將低於'java.lang.RuntimeException'。 –

回答

0

的塗料應在構造函數中

0

雖然充氣在XML自定義視圖的第一件事就是不要忘記添加到您的主視圖容器

xmlns:app="http://schemas.android.com/apk/res/my.package.name"

第二件事就是看看如果你正在使用正確的包名稱在名稱之前膨脹你的視圖並檢查拼寫。如果一切正確。

只需複製並粘貼您的整個XML文件的代碼,並確切地在您的視圖是哪個包。

,並確保您覆蓋您onDraw(Canvas canvas)方法在你看來..

編輯

,我可以看到你的XML是這樣的:

<test.example.myfirstapp 
    android:id="@+id/mostatil" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignBottom="@+id/button_capture" 
    android:layout_alignParentRight="true"/> 

但在這裏,你是不使用您的自定義視圖,它應該看起來像這樣:

<test.example.myfirstapp.DrawView 
     android:id="@+id/mostatil" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/button_capture" 
     android:layout_alignParentRight="true"/> 
+3

請記住,如果您的View沒有通過XML加載自定義屬性,則不需要'xmlns:app'。 –

+0

我編輯了這篇文章,並添加了所有的xml文件 – Navid777

+0

@TomLeese是的,你是正確的! – hardartcore

0

在eclipse中進行清理,然後從設備/模擬器中卸載應用程序,然後重試。看起來你的資源混亂了。

+0

如何在Eclipse中做一個乾淨的? – Navid777

+0

項目>清潔。 – nmw

+0

以及如何卸載應用程序? – Navid777

0

這些步驟爲我工作被實例化:

在你的.xml

<view class="test.example.myfirstapp.DrawViewd" 
android:id="@+id/mostatil" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_alignBottom="@+id/button_capture" 
android:layout_alignParentRight="true"/> 
0

嘗試把類在Java類本身,而不是下一個子類。 不要忘記更新XML標籤。

相關問題