Android自定义视图ClassNotFound

gma*_*ter 0 c# java xml android xamarin

我敢肯定这是一个愚蠢的问题,但是我在任何地方都找不到答案。我正在尝试在Android中创建一个自定义的View XML元素(使用Xamarin,因此从技术上讲它是C#,尽管我认为这并不重要)。我找到了很多教程,但似乎没人能解释路径的来源。例如,google示例如下所示:

<?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>
Run Code Online (Sandbox Code Playgroud)

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">
Run Code Online (Sandbox Code Playgroud)

现在,我已经在布局中得到了这个:

<AndroidDemo.AndroidDemo.DragRectView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/dragRect" />
Run Code Online (Sandbox Code Playgroud)

这将导致Android.View.InflateException和java.lang.ClassNotFoundException。

我确定我的DragRectView(我的班级)路径错误;谁能给我一些指导,以弄清它是什么?

Joh*_*gun 5

com.example.customviews.charting是自定义视图所在的包,因此com.example.customviews.charting.PieChart是com.example.customviews.charting包中的PieChart类。

如果com.example.customviews.charting中实际上没有类PieChart,则将获得ClassNotFoundException。

在您的自定义视图类文件中,查找以下代码行以了解您所处的软件包:

package com.mypackage;
Run Code Online (Sandbox Code Playgroud)

如果不存在,则您位于默认软件包中,建议您添加它以简化生活。

你的课应该是这样的

package com.mypackage;
public class PieChart extends View {
    ...Your Implementation of PieChart goes here...
}
Run Code Online (Sandbox Code Playgroud)


Kar*_*uri 5

在 XML 布局中使用自定义视图时,将使用完全限定的类名(包括包名)。使用自定义 View 类所在的包名称。