我的应用程序中有一个自定义视图:
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.example.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