我确信这是一个愚蠢的问题,但我在任何地方都找不到答案。我正在尝试在 Android 中创建自定义 View XML 元素(使用 Xamarin,因此技术上它是 C#,尽管我认为这在这里并不重要)。我找到了一堆教程,但似乎没有人解释这条路径到底来自哪里。例如,谷歌示例如下所示:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res/com.example.customviews">
<com.example.customviews.charting.PieChart
custom:showText="true"
custom:labelPosition="left" />
</LinearLayout>
com.example.customviews.charting 来自哪里?我找到的示例都没有解释如何创建此路径。我发现有人说这是包名,但我的包名看起来不像那样(也许我在生成文件时做错了什么?):
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="AndroidDemo.AndroidDemo" android:versionCode="1" android:versionName="1.0" android:installLocation="auto">
现在我的布局中有这个:
<AndroidDemo.AndroidDemo.DragRectView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/dragRect" />
这会导致 Android.View.InflateException 和 java.lang.ClassNotFoundException。
我确信我的 DragRectView (我的类)的路径是错误的;谁能指导我如何弄清楚它是什么?
最佳答案
当您在 XML 布局中使用自定义 View 时,您可以使用完全限定的类名称(其中包括包名称)。使用自定义 View 类所在的包名称。
关于java - Android 自定义 View ClassNotFound,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31457741/