【问题标题】:define xml element from library class从库类中定义 xml 元素
【发布时间】:2014-10-26 14:25:53
【问题描述】:

我的项目中包含一个库,并且想在我的 XML 文件中定义和元素,这个元素类型来自驻留在这个库中的一个类,当我只是用它的名称声明它时它给了我一个错误运行:

  <ClassType
    android:id="@+id/my_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

当只是将其声明为 View 然后在运行时将其转换为 ClassCastException

   <View
    android:id="@+id/my_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

然后在 Java 中,

ClassType my_view = (ClassType) findViewById(R.id.my_view);

更新:

尝试添加整个命名空间时的总日志:

10-26 14:45:47.753: E/AndroidRuntime(1430): FATAL EXCEPTION: main
10-26 14:45:47.753: E/AndroidRuntime(1430): Process: com.lockscreen, PID: 1430
10-26 14:45:47.753: E/AndroidRuntime(1430): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lockscreen/com.lockscreen.LockScreenAppActivity}: android.view.InflateException: Binary XML file line #8: Error inflating class com.example.ClassType
10-26 14:45:47.753: E/AndroidRuntime(1430):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
10-26 14:45:47.753: E/AndroidRuntime(1430):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
10-26 14:45:47.753: E/AndroidRuntime(1430):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
10-26 14:45:47.753: E/AndroidRuntime(1430):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
10-26 14:45:47.753: E/AndroidRuntime(1430):     at android.os.Handler.dispatchMessage(Handler.java:102)
10-26 14:45:47.753: E/AndroidRuntime(1430):     at android.os.Looper.loop(Looper.java:136)
10-26 14:45:47.753: E/AndroidRuntime(1430):     at android.app.ActivityThread.main(ActivityThread.java:5017)
10-26 14:45:47.753: E/AndroidRuntime(1430):     at java.lang.reflect.Method.invokeNative(Native Method)
10-26 14:45:47.753: E/AndroidRuntime(1430):     at java.lang.reflect.Method.invoke(Method.java:515)
10-26 14:45:47.753: E/AndroidRuntime(1430):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
10-26 14:45:47.753: E/AndroidRuntime(1430):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
10-26 14:45:47.753: E/AndroidRuntime(1430):     at dalvik.system.NativeStart.main(Native Method)
10-26 14:45:47.753: E/AndroidRuntime(1430): Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class com.example.ClassType
10-26 14:45:47.753: E/AndroidRuntime(1430):     at android.view.LayoutInflater.createView(LayoutInflater.java:620)
10-26 14:45:47.753: E/AndroidRuntime(1430):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
10-26 14:45:47.753: E/AndroidRuntime(1430):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
10-26 14:45:47.753: E/AndroidRuntime(1430):     at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
10-26 14:45:47.753: E/AndroidRuntime(1430):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
10-26 14:45:47.753: E/AndroidRuntime(1430):     at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
10-26 14:45:47.753: E/AndroidRuntime(1430):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
10-26 14:45:47.753: E/AndroidRuntime(1430):     at android.app.Activity.setContentView(Activity.java:1929)
10-26 14:45:47.753: E/AndroidRuntime(1430):     at com.lockscreen.LockScreenAppActivity.onCreate(LockScreenAppActivity.java:75)
10-26 14:45:47.753: E/AndroidRuntime(1430):     at android.app.Activity.performCreate(Activity.java:5231)
10-26 14:45:47.753: E/AndroidRuntime(1430):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-26 14:45:47.753: E/AndroidRuntime(1430):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
10-26 14:45:47.753: E/AndroidRuntime(1430):     ... 11 more
10-26 14:45:47.753: E/AndroidRuntime(1430): Caused by: java.lang.reflect.InvocationTargetException
10-26 14:45:47.753: E/AndroidRuntime(1430):     at java.lang.reflect.Constructor.constructNative(Native Method)
10-26 14:45:47.753: E/AndroidRuntime(1430):     at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
10-26 14:45:47.753: E/AndroidRuntime(1430):     at android.view.LayoutInflater.createView(LayoutInflater.java:594)
10-26 14:45:47.753: E/AndroidRuntime(1430):     ... 22 more
10-26 14:45:47.753: E/AndroidRuntime(1430): Caused by: java.lang.NoClassDefFoundError: com.squareup.timessquare.R$styleable
10-26 14:45:47.753: E/AndroidRuntime(1430):     at com.example.ClassType.<init>(ClassType.java:99)
10-26 14:45:47.753: E/AndroidRuntime(1430):     ... 25 more

【问题讨论】:

  • 在第一种情况下它会给你什么错误?

标签: android xml android-library


【解决方案1】:

把整个命名空间放

 <com.library.namespace.ClassType
    android:id="@+id/my_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

【讨论】:

  • 如何获取整个命名空间,它是一个库,作为 jar 文件包含在我的 libs 文件夹中。
  • 你说你也尝试过强制转换,所以你必须知道命名空间。为了能够投射它,您添加了一个 import,这就是您要查找的命名空间。
  • 不,我不是说我不知道​​命名空间,我知道,但是如何让 XML 得到它。在 java 中,我只是用它的名称空间导入类,但是在 xml 中编写整个名称空间时它崩溃并在运行时给我“错误膨胀类”
  • XML 的命名空间应该没有任何问题,即使它来自库。你有更多关于错误的细节吗?
  • 我添加了将整个命名空间添加到xml时获得的完整日志
【解决方案2】:

您必须在 xml 中使用完全限定名称 (FQN)

<com.example.ClassType
android:id="@+id/my_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-19
    • 2021-12-06
    • 1970-01-01
    • 1970-01-01
    • 2018-10-27
    • 1970-01-01
    • 2021-03-30
    相关资源
    最近更新 更多