Android:小部件显示错误视图

时间:2018-07-13 20:37:02

标签: android widget

我已经使用向导创建了一个小部件。然后,我自定义了布局。 我尝试运行,但是得到的只是窗口小部件“ 问题加载窗口小部件”的错误视图。

检查日志,发现此错误:

W/AppWidgetHostView: updateAppWidget couldn't find any view, using error view

它说系统找不到任何视图,但是我的布局在res/layout/中定义,并在小部件信息xml文件中声明。

我想念什么?这是the project on GitHub

1 个答案:

答案 0 :(得分:2)

您没有做错太多。整个应用程序小部件已正确设置。这纯粹是一个布局问题。似乎对playing_widget.xml文件的复杂性并不满意。我将文件更改为以下内容:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/playback_content"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/poster_imageview"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:src="@drawable/ic_headset_mic_light_black_140dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:id="@+id/title_textview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/app_name"
            android:textAlignment="center"
            android:textColor="@color/primaryTextColor"
            android:textSize="16sp" />

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="@dimen/default_text_padding">

            <ImageView
                android:id="@+id/player_previous"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginEnd="32dp"
                android:layout_marginRight="32dp"
                android:layout_toLeftOf="@+id/player_play_pause"
                android:src="@drawable/ic_skip_previous_white_32dp" />

            <ImageView
                android:id="@+id/player_play_pause"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:src="@drawable/ic_play_arrow_white_40dp" />

            <ImageView
                android:id="@+id/player_next"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginLeft="32dp"
                android:layout_marginStart="32dp"
                android:layout_toRightOf="@id/player_play_pause"
                android:src="@drawable/ic_skip_next_white_32dp" />


        </RelativeLayout>
    </LinearLayout>
</LinearLayout>

窗口小部件视图现在看起来很好。我意识到这可能不是您想要的确切布局,但是您只需要尝试一下,直到找到满意的东西即可。

主屏幕小部件可能有点棘手,因为您的视图集更加有限,布局的灵活性也更低。

我还没有检查,但是不确定主屏幕小部件中是否允许以下​​视图:

 <View
    android:layout_width="match_parent"
    android:layout_height="4dp"
    android:background="?colorAccent" />

如果您无法解决该错误,也可以避免。销毁主屏幕小部件,然后重新启动手机。在开发过程中,它们有时会陷入呆滞状态。

但是在任何情况下,在playback_widget.xml更改后,您的项目对我来说都工作正常。 Android 8.0。