打开小部件选择器时系统设置强制关闭
Posted
技术标签:
【中文标题】打开小部件选择器时系统设置强制关闭【英文标题】:System settings force close when opening widget picker 【发布时间】:2014-10-10 10:55:35 【问题描述】:我的用户报告在尝试使用我的应用程序(不是启动器应用程序)选择小部件时发生崩溃。 我使用的是标准的小部件选择器代码,它始终可以在所有设备上运行,LG G2 和 LG G3 除外。
我发现了一些类似的问题,但它们都提到了 android 2.1 遭受的崩溃,该崩溃已通过空数据解决方法(已在我的代码中实现)解决。
还使用alternative way 获取小部件,无需官方选择器 - 这可以工作,但需要应用程序成为 BIND_APPWIDGET 权限的系统应用程序。
任何人也遇到过这个问题,也许找到了解决方法或解决方案?
谢谢
代码如下:
public static void selectWidget(AppWidgetHost appWidgetHost, final Activity activity)
try
int appWidgetId = appWidgetHost.allocateAppWidgetId();
Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
addEmptyData(pickIntent);
activity.startActivityForResult(pickIntent, R.id.REQUEST_PICK_APPWIDGET);
catch (ActivityNotFoundException e)
activity.runOnUiThread(new Runnable()
@Override
public void run()
Toast.makeText(activity, activity.getString(R.string.widget_config_no_activity), Toast.LENGTH_SHORT).show();
);
/**
* This avoids a bug in the com.android.settings.AppWidgetPickActivity, which is used
* to select widgets. This just adds empty extras to the intent, avoiding the bug. See
* more: http://code.google.com/p/android/issues/detail?id=4272
*/
private static void addEmptyData(Intent pickIntent)
ArrayList customInfo = new ArrayList();
pickIntent.putParcelableArrayListExtra(AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo);
ArrayList customExtras = new ArrayList();
pickIntent.putParcelableArrayListExtra(AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras);
这是崩溃堆栈跟踪:
java.lang.RuntimeException: Unable to start activity ComponentInfocom.android.settings/com.android.settings.AppWidgetPickActivity: android.content.res.Resources$NotFoundException: File res/drawable-xhdpi-1280x960/ic_launcher_calendar.png from drawable resource ID #0x7f02007e
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2200)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2250)
at android.app.ActivityThread.access$800(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1200)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5105)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.content.res.Resources$NotFoundException: File res/drawable-xhdpi-1280x960/ic_launcher_calendar.png from drawable resource ID #0x7f02007e
at android.content.res.Resources.loadDrawable(Resources.java:2156)
at android.content.res.Resources.getDrawableForDensity(Resources.java:789)
at com.android.settings.AppWidgetPickActivity.createItem(AppWidgetPickActivity.java:139)
at com.android.settings.AppWidgetPickActivity.createItem(AppWidgetPickActivity.java:47)
at com.android.settings.AppWidgetLoader.putAppWidgetItems(AppWidgetLoader.java:145)
at com.android.settings.AppWidgetLoader.putInstalledAppWidgets(AppWidgetLoader.java:209)
at com.android.settings.AppWidgetLoader.getItems(AppWidgetLoader.java:182)
at com.android.settings.AppWidgetPickActivity.getItems(AppWidgetPickActivity.java:92)
at com.android.settings.ActivityPicker.onCreate(ActivityPicker.java:143)
at com.android.settings.AppWidgetPickActivity.onCreate(AppWidgetPickActivity.java:70)
at android.app.Activity.performCreate(Activity.java:5275)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2164)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2250)
at android.app.ActivityThread.access$800(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1200)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5105)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.io.FileNotFoundException: res/drawable-xhdpi-1280x960/ic_launcher_calendar.png
at android.content.res.AssetManager.openNonAssetNative(Native Method)
at android.content.res.AssetManager.openNonAsset(AssetManager.java:415)
at android.content.res.Resources.loadDrawable(Resources.java:2147)
at android.content.res.Resources.getDrawableForDensity(Resources.java:789)
at com.android.settings.AppWidgetPickActivity.createItem(AppWidgetPickActivity.java:139)
at com.android.settings.AppWidgetPickActivity.createItem(AppWidgetPickActivity.java:47)
at com.android.settings.AppWidgetLoader.putAppWidgetItems(AppWidgetLoader.java:145)
at com.android.settings.AppWidgetLoader.putInstalledAppWidgets(AppWidgetLoader.java:209)
at com.android.settings.AppWidgetLoader.getItems(AppWidgetLoader.java:182)
at com.android.settings.AppWidgetPickActivity.getItems(AppWidgetPickActivity.java:92)
at com.android.settings.ActivityPicker.onCreate(ActivityPicker.java:143)
at com.android.settings.AppWidgetPickActivity.onCreate(AppWidgetPickActivity.java:70)
at android.app.Activity.performCreate(Activity.java:5275)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2164)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2250)
at android.app.ActivityThread.access$800(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1200)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5105)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
at dalvik.system.NativeStart.main(Native Method)
【问题讨论】:
LG G2 和 LG G3 的像素密度都非常高。是否有可能存在 ic_launcher_calendar.png 的损坏版本? @DaveS 也许但这是系统拥有的可绘制对象(com.android.settings)...我知道 WearableWidgets 的开发人员设法使用我上面链接的库找到了解决方法,但我有不知道他是如何设法拒绝使用 BIND_APPWIDGET 权限的。 您可以使用您获得的 ID 从 AppWidgetManager 获取 AppWidgetProviderInfo,该 ID 具有图标属性,也许您可以验证并至少处理问题而不是崩溃。 @DaveS 试过......它也返回 null 。可能也是因为缺少权限以及我无法将小部件绑定到生成的 id。 【参考方案1】:似乎我设法使用AppWidgetManager.ACTION_APPWIDGET_BIND
Intent 解决了 Android 4.1 及更高版本(API 16)上的问题。出于某种原因,Google 声明我们必须将BIND_APPWIDGET
权限添加到AndroidManifest.xml
才能使其正常工作,但无论如何它对我有用(在多个设备上测试)。我使用bindAppWidgetIdIfAllowed
方法检查是否需要请求绑定。
我仍然没有弄清楚如何在 Android 4.1 之前绑定小部件,即使反射到 bindAppWidgetId
也是如此。这对我来说已经足够好了,但我会接受任何其他关于 pre-JellyBean 解决方案的答案(Android 低于 4.1)
【讨论】:
目前只有 1% 的设备使用低于 4.1 的 Android (developer.android.com/about/dashboards/index.html),因此 Android 版本应该不再是问题。以上是关于打开小部件选择器时系统设置强制关闭的主要内容,如果未能解决你的问题,请参考以下文章
Flutter - 在 AlertDialog 小部件之外点击后关闭系统键盘