Android 自定义视图 ClassNotFound [英] Android Custom View ClassNotFound

查看:40
本文介绍了Android 自定义视图 ClassNotFound的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确定这是一个愚蠢的问题,但我在任何地方都找不到答案.我正在尝试在 Android 中创建一个自定义的 View XML 元素(使用 Xamarin,所以它在技术上是 C#,尽管我认为这并不重要).我找到了一堆教程,但似乎没有人解释这条路到底从哪里来.例如,google 示例如下所示:

I'm sure this is a stupid question but I just can't find an answer anywhere. I'm trying to create a custom View XML element in Android (using Xamarin, so it's technically C#, although I don't think that matters here). I've found a bunch of tutorials, but no one seems to explain where on earth the path comes from. For example, the google example looks like:

<?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 从何而来?我发现的所有示例都没有解释此路径是如何创建的.我发现有人说这是包名,但我的包名看起来不像(也许我在生成文件时做错了什么?):

Where did com.example.customviews.charting come from? None of the examples I've found explain how this path is created. I found someone saying it was the package name, but my package name doesn't look anything like that (maybe I did something wrong when generating the file?):

<?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">

现在我的布局中有这个:

Right now I've got this in my layout:

<AndroidDemo.AndroidDemo.DragRectView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/dragRect" />

这会导致 Android.View.InflateException 和 java.lang.ClassNotFoundException.

Which results in an Android.View.InflateException and a java.lang.ClassNotFoundException.

我确定我的 DragRectView(我的班级)的路径是错误的;谁能给我一些关于如何弄清楚它是什么的方向?

I'm sure I've got the path to DragRectView (my class) wrong; can anyone give me some direction on how to figure out what it is?

推荐答案

com.example.customviews.charting 是你的自定义视图所在的包,所以 com.example.customviews.charting.PieChart 是包中的类 PieChartcom.example.customviews.charting.

com.example.customviews.charting is the package your custom view lives in, so com.example.customviews.charting.PieChart is the class PieChart in the package com.example.customviews.charting.

如果您在 com.example.customviews.charting 中实际上没有 PieChart 类,您将收到 ClassNotFoundException.

If you don't actually have the class PieChart in com.example.customviews.charting you will get your ClassNotFoundException.

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

In your custom view class file look for this line of code to know what package you are in:

package com.mypackage;

如果它不在默认包中,我建议您添加它以使生活更轻松.

If it's not there you're in the default package and I would suggest adding it to make life easier.

你的类应该是这样的

package com.mypackage;
public class PieChart extends View {
    ...Your Implementation of PieChart goes here...
}

这篇关于Android 自定义视图 ClassNotFound的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆