Android Custom Widget Inflate Exception

时间:2011-02-18 00:07:06

标签: android

XML

  <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res/com.org.BatteryManager"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="vertical"
      >
    <com.org.BatteryManager.BatteryView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:textColor="#ffffffff"
    />
    </LinearLayout>

logcat的

  

02-17 18:49:49.392:WARN / AppWidgetHostView(124):updateAppWidget找不到任何视图,使用错误视图           02-17 18:49:49.392:WARN / AppWidgetHostView(124):android.view.InflateException:二进制XML文件行#9:错误膨胀类com.org.BatteryManager.BatteryView           02-17 18:49:49.392:WARN / AppWidgetHostView(124):在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:576)           02-17 18:49:49.392:WARN / AppWidgetHostView(124):在android.view.LayoutInflater.rInflate(LayoutInflater.java:618)           02-17 18:49:49.392:WARN / AppWidgetHostView(124):在android.view.LayoutInflater.inflate(LayoutInflater.java:407)           02-17 18:49:49.392:WARN / AppWidgetHostView(124):在android.view.LayoutInflater.inflate(LayoutInflater.java:320)           02-17 18:49:49.392:WARN / AppWidgetHostView(124):在android.widget.RemoteViews.apply(RemoteViews.java:930)           02-17 18:49:49.392:WARN / AppWidgetHostView(124):在android.appwidget.AppWidgetHostView.updateAppWidget(AppWidgetHostView.java:219)           02-17 18:49:49.392:WARN / AppWidgetHostView(124):在android.appwidget.AppWidgetHost.updateAppWidgetView(AppWidgetHost.java:250)           02-17 18:49:49.392:WARN / AppWidgetHostView(124):在android.appwidget.AppWidgetHost $ UpdateHandler.handleMessage(AppWidgetHost.java:73)           02-17 18:49:49.392:WARN / AppWidgetHostView(124):在android.os.Handler.dispatchMessage(Handler.java:99)           02-17 18:49:49.392:WARN / AppWidgetHostView(124):在android.os.Looper.loop(Looper.java:123)           02-17 18:49:49.392:WARN / AppWidgetHostView(124):在android.app.ActivityThread.main(ActivityThread.java:4627)           02-17 18:49:49.392:WARN / AppWidgetHostView(124):at java.lang.reflect.Method.invokeNative(Native Method)           02-17 18:49:49.392:WARN / AppWidgetHostView(124):at java.lang.reflect.Method.invoke(Method.java:521)           02-17 18:49:49.392:WARN / AppWidgetHostView(124):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868)           02-17 18:49:49.392:WARN / AppWidgetHostView(124):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)           02-17 18:49:49.392:WARN / AppWidgetHostView(124):at dalvik.system.NativeStart.main(Native Method)           02-17 18:49:49.392:WARN / AppWidgetHostView(124):引起:java.lang.ClassNotFoundException:com.org.BatteryManager.BatteryView in loader dalvik.system.PathClassLoader [。]           02-17 18:49:49.392:WARN / AppWidgetHostView(124):at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)           02-17 18:49:49.392:WARN / AppWidgetHostView(124):at java.lang.ClassLoader.loadClass(ClassLoader.java:573)           02-17 18:49:49.392:WARN / AppWidgetHostView(124):at java.lang.ClassLoader.loadClass(ClassLoader.java:532)           02-17 18:49:49.392:WARN / AppWidgetHostView(124):在android.view.LayoutInflater.createView(LayoutInflater.java:466)           02-17 18:49:49.392:WARN / AppWidgetHostView(124):在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:565)           02-17 18:49:49.392:WARN / AppWidgetHostView(124):... 15更多

2 个答案:

答案 0 :(得分:6)

我有同样令人困惑的问题。要修复它,您的自定义视图应该为构造函数提供两个参数:Context和AttributeSet,如建议here

答案 1 :(得分:0)

什么是com.org.BatteryManager.BatteryView?

如果您想使用自定义视图,则可以使用。这样做的方法是在Java中创建一个自定义视图类,扩展一些基本视图组件类。

例如,我在很多应用程序中使用自定义Gallery组件。图库类只会在滑动时向左或向右移动一帧,这与默认行为不同。

我制作自定义图库的方法是扩展基本的Gallery类 -

package com.testing.whatever;

public class CustomGallery extends Gallery {
    //CODE OVERRIDES HERE
}

然后在我的XML代码中,代码看起来与您已有的代码相似 -

<com.testing.whatever.CustomGallery android:layout_width="fill_parent" android:layout_height="fill_parent" />

我怀疑你收到了你的错误,因为你没有编写com.org.BatteryManager.BatteryView或者你的java文件中的错误位置。