Android自定义小部件膨胀exception

<?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> 02-17 18:49:49.392: WARN/AppWidgetHostView(124): updateAppWidget couldn't find any view, using error view 02-17 18:49:49.392: WARN/AppWidgetHostView(124): android.view.InflateException: Binary XML file line #9: Error inflating class com.org.BatteryManager.BatteryView 02-17 18:49:49.392: WARN/AppWidgetHostView(124): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:576) 02-17 18:49:49.392: WARN/AppWidgetHostView(124): at android.view.LayoutInflater.rInflate(LayoutInflater.java:618) 02-17 18:49:49.392: WARN/AppWidgetHostView(124): at android.view.LayoutInflater.inflate(LayoutInflater.java:407) 02-17 18:49:49.392: WARN/AppWidgetHostView(124): at android.view.LayoutInflater.inflate(LayoutInflater.java:320) 02-17 18:49:49.392: WARN/AppWidgetHostView(124): at android.widget.RemoteViews.apply(RemoteViews.java:930) 02-17 18:49:49.392: WARN/AppWidgetHostView(124): at android.appwidget.AppWidgetHostView.updateAppWidget(AppWidgetHostView.java:219) 02-17 18:49:49.392: WARN/AppWidgetHostView(124): at android.appwidget.AppWidgetHost.updateAppWidgetView(AppWidgetHost.java:250) 02-17 18:49:49.392: WARN/AppWidgetHostView(124): at android.appwidget.AppWidgetHost$UpdateHandler.handleMessage(AppWidgetHost.java:73) 02-17 18:49:49.392: WARN/AppWidgetHostView(124): at android.os.Handler.dispatchMessage(Handler.java:99) 02-17 18:49:49.392: WARN/AppWidgetHostView(124): at android.os.Looper.loop(Looper.java:123) 02-17 18:49:49.392: WARN/AppWidgetHostView(124): at 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): Caused by: 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): at android.view.LayoutInflater.createView(LayoutInflater.java:466) 02-17 18:49:49.392: WARN/AppWidgetHostView(124): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:565) 02-17 18:49:49.392: WARN/AppWidgetHostView(124): ... 15 more 

Solutions Collecting From Web of "Android自定义小部件膨胀exception"

我有同样的困惑问题。 为了解决这个问题,你的自定义视图应该提供一个带有两个参数的构造函数:Context和AttributeSet。

什么是com.org.BatteryManager.BatteryView?

如果你想使用自定义视图,那么这是可能的。 实现它的方法是在Java中创build一个自定义视图类,以扩展一些基本视图组件类。

例如,我在我的许多应用程序中使用自定义图库组件。 画廊类将只在滑动中左右移动一帧,这与默认行为不同。

我制作自定义图库的方式是扩展基本库类 –

 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文件中的错误的地方。