【问题标题】:Error in adding my own custom view to xml layout将我自己的自定义视图添加到 xml 布局时出错
【发布时间】:2017-07-10 22:28:11
【问题描述】:

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

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

【问题讨论】:

  • 我建议您粘贴更多的 logcat,因为实际的堆栈跟踪将低于 java.lang.RuntimeException

标签: android android-custom-view


【解决方案1】:

在您的 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"/>

【讨论】:

  • 请记住,如果您的 View 没有通过 XML 加载的自定义属性,则不需要 xmlns:app
  • 输入错误,我的xml文件实际上也包含“DrawView”
  • 另外,甚至没有必要覆盖onDraw()
  • 因此,如果您仍然遇到相同的错误,请在 xml 文件第 13 行显示您所拥有的内容。
  • 第 13 行是“
【解决方案2】:

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

【讨论】:

  • 如何在 Eclipse 中进行清理?
  • 通过设备/模拟器上的设置。
【解决方案3】:

应该在构造函数中实例化绘制

【讨论】:

    【解决方案4】:

    这些步骤对我有用:

    在您的 .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"/>
    

    【讨论】:

      【解决方案5】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-29
        相关资源
        最近更新 更多