System settings are forced to close when the widget is opened

My users report crashes when they try to select a widget with my application (not an application to launch). I use the standard widget selection code, and it always worked and works on all devices except LG G2 and LG G3 .

I found several similar questions, but they all relate to the crash that Android 2.1 encountered, which was resolved using an empty informational workaround (which is already implemented in my code).

An alternative method is also used to get widgets without official choice - which works, but requires the application to be a system application for permission BIND_APPWIDGET.

Has anyone also experienced this problem and possibly found a workaround or solution?

thanks

Here is the code:

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); } 

Here's the crash stacktrace:

  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.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) 
+5
source share
1 answer

It seems I was able to solve the problem on Android 4.1 and above (API 16) using AppWidgetManager.ACTION_APPWIDGET_BIND Intent. For some reason, Google says we need to add the BIND_APPWIDGET permission for AndroidManifest.xml for this to work, but it works for me anyway (tested on multiple devices). I use the bindAppWidgetIdIfAllowed method to check if I need to request a binding or not.

I still don’t know how to link widgets to Android 4.1, even with reflection on bindAppWidgetId . This is good enough for me, but I will agree to any other answer with a solution for preliminary JellyBean (Android below 4.1).

+1
source

Source: https://habr.com/ru/post/1200522/


All Articles