1

我的应用程序中有一个自定义视图:

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
4

5 回答 5

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"/>
于 2013-10-27T15:10:44.227 回答
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"/>
于 2013-01-06T14:19:23.097 回答
0

在 Eclipse 中进行清理,然后从设备/模拟器中卸载该应用程序,然后重试。看起来你的资源被搞砸了。

于 2013-01-06T15:58:01.467 回答
0

油漆应该在构造函数中实例化

于 2013-08-10T08:57:59.567 回答
0

尝试将类放在 java 类中,而不是放在子类下。不要忘记更新 XML 标记。

于 2017-07-10T14:28:11.260 回答