我在使用android.support.v7.widget.GridLayout
支持库时遇到困难。它总是给我以下错误:
08-09 23:49:55.746: W/AppWidgetHostView(132): Error inflating AppWidget AppWidgetProviderInfo(provider=ComponentInfo{com.myapp.AppWidget}): android.view.InflateException: Binary XML file line #33: Error inflating class android.support.v7.widget.GridLayout
08-09 23:49:56.716: W/AppWidgetHostView(132): updateAppWidget couldn't find any view, using error view
08-09 23:49:56.716: W/AppWidgetHostView(132): android.view.InflateException: Binary XML file line #33: Error inflating class android.support.v7.widget.GridLayout
08-09 23:49:56.716: W/AppWidgetHostView(132): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:581)
08-09 23:49:56.716: W/AppWidgetHostView(132): at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
08-09 23:49:56.716: W/AppWidgetHostView(132): at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
08-09 23:49:56.716: W/AppWidgetHostView(132): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
08-09 23:49:56.716: W/AppWidgetHostView(132): at android.widget.RemoteViews.apply(RemoteViews.java:939)
08-09 23:49:56.716: W/AppWidgetHostView(132): at android.appwidget.AppWidgetHostView.updateAppWidget(AppWidgetHostView.java:218)
08-09 23:49:56.716: W/AppWidgetHostView(132): at android.appwidget.AppWidgetHost.updateAppWidgetView(AppWidgetHost.java:250)
08-09 23:49:56.716: W/AppWidgetHostView(132): at android.appwidget.AppWidgetHost$UpdateHandler.handleMessage(AppWidgetHost.java:73)
08-09 23:49:56.716: W/AppWidgetHostView(132): at android.os.Handler.dispatchMessage(Handler.java:99)
08-09 23:49:56.716: W/AppWidgetHostView(132): at android.os.Looper.loop(Looper.java:123)
08-09 23:49:56.716: W/AppWidgetHostView(132): at android.app.ActivityThread.main(ActivityThread.java:3683)
08-09 23:49:56.716: W/AppWidgetHostView(132): at java.lang.reflect.Method.invokeNative(Native Method)
08-09 23:49:56.716: W/AppWidgetHostView(132): at java.lang.reflect.Method.invoke(Method.java:507)
08-09 23:49:56.716: W/AppWidgetHostView(132): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-09 23:49:56.716: W/AppWidgetHostView(132): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-09 23:49:56.716: W/AppWidgetHostView(132): at dalvik.system.NativeStart.main(Native Method)
08-09 23:49:56.716: W/AppWidgetHostView(132): Caused by: java.lang.ClassNotFoundException: android.support.v7.widget.GridLayout in loader dalvik.system.PathClassLoader[.]
08-09 23:49:56.716: W/AppWidgetHostView(132): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
08-09 23:49:56.716: W/AppWidgetHostView(132): at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
08-09 23:49:56.716: W/AppWidgetHostView(132): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
08-09 23:49:56.716: W/AppWidgetHostView(132): at android.view.LayoutInflater.createView(LayoutInflater.java:471)
08-09 23:49:56.716: W/AppWidgetHostView(132): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:570)
08-09 23:49:56.716: W/AppWidgetHostView(132): ... 15 more
提到的第33行是支持lib标签开始的部分。看起来因为任何原因无法找到班级,但我不知道为什么......
我想使用这个lib的原因是因为我希望与Android v10保持兼容。无论如何我选择v10或v4.2.2作为项目构建目标。清单的minSdkVersion
设置为10.我跟踪了所有这些steps并尝试将.jar放入应用程序的引用库并勾选了导出它的选项。
这是我的layout.xml
,用于创建app-widget:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/appWidget_relativeLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:orientation="vertical">
<TextView
android:id="@+id/appWidget_textView_profileName"
android:layout_width="320dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:clickable="false"
android:enabled="true"
android:gravity="center_horizontal"
android:longClickable="true"
android:scrollHorizontally="false"
android:text="profile name"
android:textAppearance="?android:attr/textAppearanceLarge" />
<FrameLayout
android:id="@+id/appWidget_frameLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/appWidget_textView_profileName" >
</FrameLayout>
<android.support.v7.widget.GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</android.support.v7.widget.GridLayout>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</RelativeLayout>
更新:我设法将其缩小到与应用小部件相关的GridLayout
的使用范围。看来,当您对它们使用GridLayout
时,只会出现此问题。当我为普通Activity
使用相同的布局时,它可以正常工作。关于如何解决这个问题的任何建议?
答案 0 :(得分:1)
制作小部件时,您受限于实现View
机制的RemoteViews
。该机制的工作方式是远程“主机”进程是进行实际膨胀的进程。因此,如果该远程进程(承载该窗口小部件的启动程序)在其类路径中没有GridLayout
类,则绝对没有办法在您自己的进程中绘制View并将其设置为Bitmap
上的ImageView
。不过,我不会这样做。
通常,在制作小部件时,请将自己限制在框架附带的Views
。