java - Android:小部件在列表中不可见

标签 java android android-studio widget

我正在尝试更新现有的 Android 4+ 应用程序以提供小部件。我遵循了 developer.android.com 上的教程但无法让它运行。该小部件未显示在小部件列表中,因此无法在主屏幕上安装/使用。

这是我将小部件添加到应用程序所做的:

<强>1。在 list 文件中添加了 WidgetProvider 和设置 Activity :

    <!-- Widget -->
    <receiver
        android:name=".Widget.WidgetProvider"
        android:label="Example Widget"
        android:icon="@drawable/some_drawable">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>

        <meta-data
            android:name="android.appwidget.provider"
            android:resource="@xml/appwidget_info" />
    </receiver>
    <activity
        android:name=".Widget.WidgetSettingsActivity"
        android:label="@string/Settings" >
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_CONFIGURE"/>
        </intent-filter>
    </activity>

<强>2。添加了 WidgetProvider

package com.mycompany.myapp.Widget;

import android.appwidget.AppWidgetProvider;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;

public class WidgetProvider extends AppWidgetProvider{
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {

        for (int i = 0; i < appWidgetIds.length; ++i) {
            // Do something
        }
        super.onUpdate(context, appWidgetManager, appWidgetIds);
    }
}

<强>3。添加了 res/xml/appwidget_info.xml

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android">
    android:minWidth="50dp"
    android:minHeight="50dp"
    android:minResizeHeight="50dp"
    android:minResizeWidth="50dp"
    android:updatePeriodMillis="0"
    android:initialLayout="@layout/widget__content"
    android:configure="com.mycompany.myapp.Widget.WidgetSettingsActivity
    android:resizeMode="horizontal|vertical"
    android:widgetCategory="home_screen">
</appwidget-provider>

<强>4。添加了设置 Activity

// From the developer.android.com Example
package com.mycompany.myapp.Widget;

import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;

import android.os.Bundle;
import android.view.View;

import com.mycompany.myapp.R;

public class WidgetSettingsActivity extends Activity {
    private int appWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Set the result to CANCELED.  This will cause the widget host to cancel
        // out of the widget placement if they press the back button.
        setResult(RESULT_CANCELED);

        // Set the view layout resource to use.
        setContentView(R.layout.widget__settings);

        // Find the EditText
        //mUsername = (EditText)findViewById(R.id.username);
        //mPassword = (EditText)findViewById(R.id.password);

        // Bind the action for the save button.
        //findViewById(R.id.save_button).setOnClickListener(mOnClickListener);

        // Find the widget id from the intent.
        Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        if (extras != null)
            appWidgetId =     extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);

        // If they gave us an intent without the widget id, just bail.
        if (appWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID)
            finish();
    }
}

在/res/layout/中进行布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"
    android:layout_height="match_parent"     android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.mycompany.myapp.Widget.WidgetSettingsActivity">

    <TextView android:text="@string/Settings" android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</RelativeLayout>

<强>5。在/res/layout中添加了Widget Layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginLeft="@dimen/margin_default"
        android:layout_marginRight="@dimen/margin_default"
        android:layout_marginTop="@dimen/margin_default"
        android:layout_weight="20" >

        <Button
            android:id="@+id/TButton"
            style="@style/BigButton"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_gravity="center_vertical"
            android:layout_marginRight="@dimen/margin_medium"
            android:layout_weight="1"
            android:text="@string/Test" />

        <Button
            android:id="@+id/IButton"
            style="@style/BigButton"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="@dimen/margin_medium"
            android:layout_weight="1"
            android:text="@string/New" />
    </LinearLayout>
</LinearLayout>

应用程序编译并运行没有任何错误。但我无法在任何地方访问/查看/使用该小部件。

我发现了与此主题相关的其他问题,但它们都有不同的解决方案(例如,缺少 intent-filter、安装在 SD 卡上而不是内部存储器等),但这里不是这种情况。

知道我可能做错了什么吗?

最佳答案

明白了!又搜索了两个小时后,我发现了错误。感谢另一个question中的提示:

appwidget_info.xml 格式错误:

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

第二行末尾的 > 是错误的,因为标签在倒数第二行关闭:...="home_screen">

所以正确的 xml 看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth="50dp"
    android:minHeight="50dp"
    android:minResizeHeight="50dp"
    android:minResizeWidth="50dp"
    android:updatePeriodMillis="0"
    android:initialLayout="@layout/widget__content"
    android:configure="com.mycompany.myapp.Widget.WidgetSettingsActivity
    android:resizeMode="horizontal|vertical"
    android:widgetCategory="home_screen">
</appwidget-provider>

我想知道为什么 Android Studio 没有显示任何错误或警告。会节省我很多时间......此外,我不知道为什么我犯了与上述问题中链接的用户相同的错误。也许我从一个不好的例子中复制了代码?

但是:如果有人遇到同样的问题,请先检查您的 XML :-)

关于java - Android:小部件在列表中不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32716721/

相关文章:

java - 在 Eclipse e4 中禁用表查看器中的行

android - 将 PayPal 与 Android 应用程序集成,无需用户拥有 PayPal 帐户

android - 生成签名的 apk 失败并出现 build\app\intermediates\flutter\profile\libs.jar(系统找不到指定的路径)

JSONArray 将响应 null 元素转换为 "null"

android - 按照 Android 开发者文档添加 fragment 不起作用?

java - 不能是@Required 或@NotNull

java - 名称冲突 : The method add(Object) of type test2 has the same erasure as add(E) of type HashSet<E> but does not override it

java - 投影中的变量 'x' 不存在于 GROUP BY 中

java - 如何将数据从过滤器传递到 Jersey 中的端点

Android:为什么 broadcastreceiver 没有收到 intent?