【问题标题】:Cannot instantiate custom class in android Activity layout XML file无法在 android Activity 布局 XML 文件中实例化自定义类
【发布时间】:2013-03-24 13:37:54
【问题描述】:

所以我的 XML 看起来像这样:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".Calendar" >

<com.fortysevendeg.android.swipelistview.SwipeListView
    xmlns:swipe="http://schemas.android.com/apk/res-auto"
    android:id="@+id/example_lv_list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:listSelector="#00000000"
    swipe:swipeActionLeft="reveal"
    swipe:swipeActionRight="reveal"
    swipe:swipeAnimationTime="10"
    swipe:swipeBackView="@+id/back"
    swipe:swipeCloseAllItemsWhenMoveList="true"
    swipe:swipeFrontView="@+id/front"
    swipe:swipeMode="right"
    swipe:swipeOffsetLeft="80dip"
    swipe:swipeOffsetRight="80dip"
    swipe:swipeOpenOnLongPress="false" />

</RelativeLayout>

但是,当我在 Eclipse 中进入图形视图时,我收到此错误:

找不到以下类: - com.fortysevendeg.android.swipelistview.SwipeListView(修复构建路径、编辑 XML、创建类)

在 Eclipse 中,我的包资源管理器窗口中有 3 个包。

  • android-swipelistview-master
  • 我的应用程序
  • 库(android-swipelistview-master 的依赖项)

看起来像这样:SCREENSHOT

奇怪的是,我在 android-swipelist-view-master 和库包中都没有任何错误。唯一的警告是针对未使用的变量,如果在其他地方使用它,我会犹豫是否要删除它。我想我只是没有正确实例化它?我不知道。这个我想不通!

Here 是 myApp 包属性 > Android 设置。

【问题讨论】:

  • 您是否将库添加到构建路径?
  • android-swipelistview-master 和库都在构建路径中。两个原生库位置都指向工作区项 android-swipelistview-master 和 library。
  • 嘿,问题解决了吗?我不知何故让它实例化,但它在 frontView 抛出一个空指针,如果我更改我的自定义行,它不会在运行时得到反映

标签: java android xml eclipse github


【解决方案1】:

正确引用所有库后,我将 XML 更改为引用 SwipeListView,如下所示:

<main.java.com.fortysevendeg.android.swipelistview.SwipeListView
    xmlns:swipe="http://schemas.android.com/apk/res-auto"
    android:id="@+id/example_lv_list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:listSelector="#00000000"
    swipe:swipeActionLeft="reveal"
    swipe:swipeActionRight="reveal"
    swipe:swipeAnimationTime="10"
    swipe:swipeBackView="@+id/back"
    swipe:swipeCloseAllItemsWhenMoveList="true"
    swipe:swipeFrontView="@+id/front"
    swipe:swipeMode="right"
    swipe:swipeOffsetLeft="80dip"
    swipe:swipeOffsetRight="80dip"
    swipe:swipeOpenOnLongPress="false" />

【讨论】:

    【解决方案2】:

    您说您在自己的项目中拥有这三个包?

    如果您将源代码复制到 android-swipelistview-master 包中,那么您已经更改了 swipelistview 所在的包名。

    尝试在您创建的新结构中找出它位于哪个包中。我对您的描述并不肯定,但请尝试以下操作:

    <android-swipelistview-master.com.fortysevendeg.android.swipelistview.SwipeListView
    xmlns:swipe="http://schemas.android.com/apk/res-auto"
    android:id="@+id/example_lv_list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:listSelector="#00000000"
    swipe:swipeActionLeft="reveal"
    swipe:swipeActionRight="reveal"
    swipe:swipeAnimationTime="10"
    swipe:swipeBackView="@+id/back"
    swipe:swipeCloseAllItemsWhenMoveList="true"
    swipe:swipeFrontView="@+id/front"
    swipe:swipeMode="right"
    swipe:swipeOffsetLeft="80dip"
    swipe:swipeOffsetRight="80dip"
    swipe:swipeOpenOnLongPress="false" />
    

    如果你真的是说“android-swipelistview-master”是它自己的包。

    如果您改为从 Github 中检出该库的整个项目目录,将其导入 Eclipse,并将其作为库项目引用,则您可以以更简洁的方式获得所需的结果。如需更多阅读,请查看:the Android documentation on library projects

    【讨论】:

    • 我添加了我的包资源管理器的屏幕截图。我是 Eclipse 新手,你能告诉我我是否将 android-swipelistview-master 添加为自己的包吗?
    • 好的,您的屏幕截图显示您没有将代码复制到项目中它自己的包中,而是在一个单独的项目中。很好,您正在遵循我在回答末尾建议的库结构。您应该右键单击您的项目并将 android-swipelistview-master 项目添加为库。此外,您需要右键单击 android-swipelistview-master 并将“library”作为库项目添加到其中。
    • 库和 android-swipelistview-master 的构建目标是否应该像 myApp 一样都是 Android 4.2.2?
    • 我认为库和 android-swipelistview-master 的构建目标可以不同。它们可以设置为开发人员最初设置的任何值。
    【解决方案3】:

    确保您的滑动项目看起来像这样(我必须将代码从 src/main/java 移动到 src):

    然后将项目定义为库项目,将 NineOldAndroids 添加为库 (git://github.com/JakeWharton/NineOldAndroids.git) 以及 Android 支持库

    之后,您可以将 android-swipelistview 项目添加到您的主项目中,这应该可以工作(对我有用)。

    【讨论】:

    • 我已经完成了所有这些事情,但由于某种原因这不起作用。我应该将库重命名为 NineOldAndroids 吗?
    • 列表代码仍在 main/java 中,所以我想知道为什么它会编译...你能发布你的项目属性/Android 吗?
    • 已发布。这是 myApp 的属性/Android 窗口的屏幕截图。
    • 再次为什么是 src/main/java/... 中的列表代码?你能把它移动到文件夹 src/com.fortysevendeg.android.swipelistviews 中(而不是 src/main/java/com.fortysevendeg.android.swipelistview)
    【解决方案4】:

    在 JellyBean 和 Eclipse Juno 上遇到了这个确切的问题。不过,视图在模拟器/设备中正确显示,所以这是图形布局编辑器特有的问题。

    确保 java 文件位置正确。另外,尝试更改此行:

    xmlns:swipe="http://schemas.android.com/apk/res-auto"
    

    xmlns:swipe="http://schemas.android.com/apk/Location"
    

    其中 location 是自定义小部件的 .java 文件的完整位置(包括包)。此外,如果您在开始时声明命名空间(使用 android 和工具命名空间),则不必在每次使用自定义小部件时都重复它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-01
      • 2012-12-02
      相关资源
      最近更新 更多