1

我想用一个非常简单的方法构建一个小部件GridLayout,但我得到了一个 InflateException:

Binary XML file line #2: Error inflating class android.widget.GridLayout

只发生在 4.0 和 4.2 上是没有问题的(版本之间有区别吗?)有人知道吗?

堆栈跟踪

03-12 12:27:49.100: W/AppWidgetHostView(528): updateAppWidget couldn't find any view, 
using error view
03-12 12:27:49.100: W/AppWidgetHostView(528): android.view.InflateException: Binary XML file line #2: Error inflating class android.widget.GridLayout
03-12 12:27:49.100: W/AppWidgetHostView(528):   at android.view.LayoutInflater.createView(LayoutInflater.java:606)
03-12 12:27:49.100: W/AppWidgetHostView(528):   at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
03-12 12:27:49.100: W/AppWidgetHostView(528):   at android.view.LayoutInflater.onCreateView(LayoutInflater.java:653)
03-12 12:27:49.100: W/AppWidgetHostView(528):   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:678)
03-12 12:27:49.100: W/AppWidgetHostView(528):   at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
03-12 12:27:49.100: W/AppWidgetHostView(528):   at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
03-12 12:27:49.100: W/AppWidgetHostView(528):   at android.widget.RemoteViews.apply(RemoteViews.java:1581)
03-12 12:27:49.100: W/AppWidgetHostView(528):   at android.appwidget.AppWidgetHostView.updateAppWidget(AppWidgetHostView.java:289)
03-12 12:27:49.100: W/AppWidgetHostView(528):   at android.appwidget.AppWidgetHost.createView(AppWidgetHost.java:238)
03-12 12:27:49.100: W/AppWidgetHostView(528):   at com.sonyericsson.home.widget.WidgetManager.getWidgetView(WidgetManager.java:142)
03-12 12:27:49.100: W/AppWidgetHostView(528):   at com.sonyericsson.home.layer.AdapterHelper.getWidgetInfoView(AdapterHelper.java:280)
03-12 12:27:49.100: W/AppWidgetHostView(528):   at com.sonyericsson.home.layer.desktop.DesktopAdapter.getView(DesktopAdapter.java:100)
03-12 12:27:49.100: W/AppWidgetHostView(528):   at com.sonyericsson.paneview.PaneView.ensureItemView(PaneView.java:2343)
03-12 12:27:49.100: W/AppWidgetHostView(528):   at com.sonyericsson.paneview.PaneView.layoutItem(PaneView.java:2294)
03-12 12:27:49.100: W/AppWidgetHostView(528):   at com.sonyericsson.paneview.PaneView.onLayout(PaneView.java:1199)
03-12 12:27:49.100: W/AppWidgetHostView(528):   at android.view.View.layout(View.java:11390)
03-12 12:27:49.100: W/AppWidgetHostView(528):   at android.view.ViewGroup.layout(ViewGroup.java:4332)
03-12 12:27:49.100: W/AppWidgetHostView(528):   at android.widget.RelativeLayout.onLayout(RelativeLayout.java:930)
03-12 12:27:49.100: W/AppWidgetHostView(528):   at android.view.View.layout(View.java:11390)
03-12 12:27:49.100: W/AppWidgetHostView(528):   at android.view.ViewGroup.layout(ViewGroup.java:4332)
03-12 12:27:49.100: W/AppWidgetHostView(528):   at android.widget.RelativeLayout.onLayout(RelativeLayout.java:930)
03-12 12:27:49.100: W/AppWidgetHostView(528):   at android.view.View.layout(View.java:11390)
03-12 12:27:49.100: W/AppWidgetHostView(528):   at android.view.ViewGroup.layout(ViewGroup.java:4332)
03-12 12:27:49.100: W/AppWidgetHostView(528):   at android.widget.RelativeLayout.onLayout(RelativeLayout.java:930)
03-12 12:27:49.100: W/AppWidgetHostView(528):   at android.view.View.layout(View.java:11390)
03-12 12:27:49.100: W/AppWidgetHostView(528):   at android.view.ViewGroup.layout(ViewGroup.java:4332)
03-12 12:27:49.100: W/AppWidgetHostView(528):   at android.widget.RelativeLayout.onLayout(RelativeLayout.java:930)
03-12 12:27:49.100: W/AppWidgetHostView(528):   at android.view.View.layout(View.java:11390)
03-12 12:27:49.100: W/AppWidgetHostView(528):   at android.view.ViewGroup.layout(ViewGroup.java:4332)
03-12 12:27:49.100: W/AppWidgetHostView(528):   at android.widget.FrameLayout.onLayout(FrameLayout.java:444)
03-12 12:27:49.100: W/AppWidgetHostView(528):   at android.view.View.layout(View.java:11390)
03-12 12:27:49.100: W/AppWidgetHostView(528):   at android.view.ViewGroup.layout(ViewGroup.java:4332)
03-12 12:27:49.100: W/AppWidgetHostView(528):   at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1653)
03-12 12:27:49.100: W/AppWidgetHostView(528):   at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1511)
03-12 12:27:49.100: W/AppWidgetHostView(528):   at android.widget.LinearLayout.onLayout(LinearLayout.java:1416)
03-12 12:27:49.100: W/AppWidgetHostView(528):   at android.view.View.layout(View.java:11390)
03-12 12:27:49.100: W/AppWidgetHostView(528):   at android.view.ViewGroup.layout(ViewGroup.java:4332)
03-12 12:27:49.100: W/AppWidgetHostView(528):   at android.widget.FrameLayout.onLayout(FrameLayout.java:444)
03-12 12:27:49.100: W/AppWidgetHostView(528):   at android.view.View.layout(View.java:11390)
03-12 12:27:49.100: W/AppWidgetHostView(528):   at android.view.ViewGroup.layout(ViewGroup.java:4332)
03-12 12:27:49.100: W/AppWidgetHostView(528):   at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1490)
03-12 12:27:49.100: W/AppWidgetHostView(528):   at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2455)
03-12 12:27:49.100: W/AppWidgetHostView(528):   at android.os.Handler.dispatchMessage(Handler.java:99)
03-12 12:27:49.100: W/AppWidgetHostView(528):   at android.os.Looper.loop(Looper.java:137)
03-12 12:27:49.100: W/AppWidgetHostView(528):   at android.app.ActivityThread.main(ActivityThread.java:4424)
03-12 12:27:49.100: W/AppWidgetHostView(528):   at java.lang.reflect.Method.invokeNative(Native Method)
03-12 12:27:49.100: W/AppWidgetHostView(528):   at java.lang.reflect.Method.invoke(Method.java:511)
03-12 12:27:49.100: W/AppWidgetHostView(528):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:817)
03-12 12:27:49.100: W/AppWidgetHostView(528):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
03-12 12:27:49.100: W/AppWidgetHostView(528):   at dalvik.system.NativeStart.main(Native Method)
03-12 12:27:49.100: W/AppWidgetHostView(528): Caused by: android.view.InflateException: Binary XML file line #2: Class not allowed to be inflated android.widget.GridLayout
03-12 12:27:49.100: W/AppWidgetHostView(528):   at android.view.LayoutInflater.failNotAllowed(LayoutInflater.java:618)
03-12 12:27:49.100: W/AppWidgetHostView(528):   at android.view.LayoutInflater.createView(LayoutInflater.java:558)
03-12 12:27:49.100: W/AppWidgetHostView(528):   ... 49 more

布局 XML

<?xml version="1.0" encoding="utf-8"?>
 <GridLayout
    xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"
    android:layout_height="match_parent"

    android:useDefaultMargins="true"
    android:alignmentMode="alignBounds"
    android:columnOrderPreserved="false"

    android:columnCount="4"
    >

<TextView
        android:text="Email setup"
        android:textSize="32dip"

        android:layout_columnSpan="4"
        android:layout_gravity="center_horizontal"
        />

<TextView
        android:text="You can configure email in just a few steps:"
        android:textSize="16dip"

        android:layout_columnSpan="4"
        android:layout_gravity="left"
        />

<TextView
        android:text="Email address:"

        android:layout_gravity="right"
        />

<EditText
        android:ems="10"
        />

<TextView
        android:text="Password:"

        android:layout_column="0"
        android:layout_gravity="right"
        />

<EditText
        android:ems="8"
        />

<Space
        android:layout_row="4"
        android:layout_column="0"
        android:layout_columnSpan="3"
        android:layout_gravity="fill"
        />

<Button
        android:text="Next"

        android:layout_row="5"
        android:layout_column="3"
        />

    package com.path.testwidget;

 import android.appwidget.AppWidgetManager;
 import android.appwidget.AppWidgetProvider;
 import android.content.Context;
 import android.widget.RemoteViews;


public class TestWidget extends AppWidgetProvider {

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {
        final int N = appWidgetIds.length;

        RemoteViews rvs= new RemoteViews(context.getPackageName(), R.layout.balance_widget);

        //update all N widgets
        for (int i=0; i<N; i++) {

            int appWidgetId = appWidgetIds[i];
            appWidgetManager.updateAppWidget(appWidgetId, rvs);
        }

    }

 }
4

1 回答 1

4

RemoteView仅允许某些布局在其内容中膨胀,您可以在此处查看列表

例如,您可以看到不允许使用TableView 。您的 logcat 消息说不允许使用GridView ,而文档说是。我猜这个功能是相当新的,它在 4.2 之前的版本上是不允许的。或 4.1。

您可能应该在远程视图中使用另一种布局类型。

于 2013-03-12T13:08:41.070 回答