[英]Android: Widget not visible in list
I am trying to update an existing Android 4+ app to supply a widget. 我正在尝试更新现有的Android 4+应用程序以提供小部件。 I followed the tutorial on developer.android.com but was not able to get it running. 我就跟着教程developer.android.com但无法得到它运行。 The widget does not show up in the widget list and can thus not be installed/used on the home screen. 该小部件未显示在小部件列表中,因此无法在主屏幕上安装/使用。
This is what I did to add the widget to the app: 这是我将小部件添加到应用程序的过程:
1. Added the WidgetProvider and Settings Activity to the Manifest file: 1.将WidgetProvider和Settings活动添加到清单文件中:
<!-- 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. Added the WidgetProvider 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. Added the res/xml/appwidget_info.xml 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. Added the Settings Activity 4.添加了设置活动
// 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();
}
}
Und das Layout in /res/layout/ / res / layout /中的Und das布局
<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. Added the Widget Layout in /res/layout 5.在/ res / 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>
The app compiles and runs without any error. 该应用程序可以编译并运行,没有任何错误。 But I cannot access/see/use the widget anywhere. 但是我无法在任何地方访问/查看/使用小部件。
I found other questions regarding this topic but they all had different solutions (eg missing intent-filter, installed on SD card instead in internal memory, etc.) which are not the case here. 我发现了与此主题有关的其他问题,但是它们都有不同的解决方案(例如,缺少意图过滤器,安装在SD卡上而不是内部存储器等),在这里不是这种情况。
Any idea what I might be doing wrong? 知道我做错了什么吗?
Got it! 得到它了! After another two hours of searching I found the error. 经过另外两个小时的搜索,我发现了错误。 Thanks to a hint in another question : 由于另一个问题的提示:
The appwidget_info.xml was malformed: 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>
The >
at the end of the second line is wrong since the tag is closed in the second last line: ...="home_screen">
第二行末尾的>
是错误的,因为该标记在最后第二行中已关闭: ...="home_screen">
So the correct xml looks like this: 因此正确的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>
I wonder why Android Studio does not show any error or warning about this. 我不知道为什么Android Studio不会对此显示任何错误或警告。 Would have saved me a lot of time... Additionally I have no idea why I made the same mistake as the user linked in the question above. 本来可以节省我很多时间...另外,我不知道为什么我犯了与上述问题中链接的用户相同的错误。 Maybe I copied the code from a bad example? 也许我从一个不好的例子中复制了代码?
However: If any one encounters the same issue, check your XML first :-) 但是:如果任何一个遇到相同的问题,请先检查您的XML :-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.