我在我的應用程序的自定義視圖:錯誤添加自己的自定義視圖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
我建議你粘貼更多的logcat,因爲實際的堆棧跟蹤將低於'java.lang.RuntimeException'。 –