Android Problem loading widget - without errors in the logcat
Asked Answered
T

1

11

I created an analog clock widget in java, and for some reason I get "Problem loading widget" all the time, the only things I get from the logcat when I try to place the widget are:

2020-11-18 19:19:13.162 9482-9482/? E/Zygote: isWhitelistProcess - Process is Whitelisted
2020-11-18 19:19:13.162 9482-9482/? E/Zygote: accessInfo : 1
2020-11-18 19:19:13.166 9482-9482/? I/ckcustomizatio: Late-enabling -Xcheck:jni
2020-11-18 19:19:13.182 9482-9482/? E/ckcustomizatio: Unknown bits set in runtime_flags: 0x8000
2020-11-18 19:19:13.196 9482-9482/com.l_es.analogclockcustomization D/ActivityThread: setConscryptValidator
2020-11-18 19:19:13.196 9482-9482/com.l_es.analogclockcustomization D/ActivityThread: setConscryptValidator - put

and the widget is only a black square with "Problem loading widget" on it.

here is the widget class:

public class MainWidget extends AppWidgetProvider {

    static RemoteViews views;
    //preferences
    private static SharedPreferences custClockPrefs;
    //number of possible designs
    private static int numClocks;
    //IDs of Analog Clock elements
    static int[] clockDesigns;

    public void onReceive(Context context, Intent intent) {

        numClocks = context.getResources().getInteger(R.integer.num_clocks);
        clockDesigns = new int[numClocks];
        for(int d = 0; d < numClocks; d++){
            clockDesigns[d] = context.getResources().getIdentifier
                    ("AnalogClock"+d, "id", context.getPackageName());
        }
        //find out the action
        String action = intent.getAction();
        //is it time to update
        if(AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(action)) {
            views = new RemoteViews(context.getPackageName(),
                    R.layout.clock_widget_layout);

            AppWidgetManager.getInstance(context).updateAppWidget
                    (intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS), views);

            custClockPrefs = context.getSharedPreferences("CustomClockPrefs", 0);
            int chosenDesign = custClockPrefs.getInt("clockdesign", 0);
            if(chosenDesign >= 0){
                for(int d = 0; d < numClocks; d++){
                    if(d != chosenDesign)
                        views.setViewVisibility(clockDesigns[d], View.INVISIBLE);
                }
                views.setViewVisibility(clockDesigns[chosenDesign], View.VISIBLE);
            }

            Intent choiceIntent = new Intent(context, ClockChoice.class);
            PendingIntent clickPendIntent = PendingIntent.getActivity
                    (context, 0, choiceIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            views.setOnClickPendingIntent(R.id.custom_clock_widget, clickPendIntent);

        }
    }

}

here is the draw function I use in order to draw the clock:

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    final float w = (float) getWidth() / 2;
    final float h = (float) getHeight() / 2;

    final int saveCount = canvas.save();
    canvas.translate(w, h);
    final float scale = Math.min((float) w / mDial.getIntrinsicWidth(),
            (float) h / mDial.getIntrinsicHeight());
    if (scale < 1f) {
        canvas.scale(scale, scale, 0f, 0f);
    }
    mDial.draw(canvas);

    final float hourAngle = mTime.get(Calendar.HOUR) * 30f;
    canvas.rotate(hourAngle, 0f, 0f);
    mHourHand.draw(canvas);

    final float minuteAngle = mTime.get(Calendar.MINUTE) * 6f;
    canvas.rotate(minuteAngle - hourAngle, 0f, 0f);
    mMinuteHand.draw(canvas);

    if (mEnableSeconds) {
        final float secondAngle = mTime.get(Calendar.SECOND) * 6f;
        canvas.rotate(secondAngle - minuteAngle, 0f, 0f);
        mSecondHand.draw(canvas);
    }
    canvas.restoreToCount(saveCount);
}

Here is my layout file of the widget, keep in mind that I set at every time 2 out of the 3 to be with visibility of "gone":

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/custom_clock_widget"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="@dimen/clock_margin"
    android:gravity="center">

    <com.l_es.analogclockcustomization.AnalogClockDesign
        android:id="@+id/AnalogClock0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:showSecondHand="false"
        app:dial="@drawable/clock_dial_2"
        app:hour="@drawable/clock_hour_hand_2"
        app:minute="@drawable/clock_minute_hand_2"/>

    <com.l_es.analogclockcustomization.AnalogClockDesign
        android:id="@+id/AnalogClock1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:showSecondHand="false"
        app:dial="@drawable/clock_dial_3"
        app:hour="@drawable/clock_hour_hand_3"
        app:minute="@drawable/clock_minute_hand_3"/>

    <com.l_es.analogclockcustomization.AnalogClockDesign
        android:id="@+id/AnalogClock2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:showSecondHand="false"
        app:dial="@drawable/clock_dial_4"
        app:hour="@drawable/clock_hour_hand_4"
        app:minute="@drawable/clock_minute_hand_4"/>

</RelativeLayout>

clock_widget.xml:

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:initialLayout="@layout/clock_widget_layout"
    android:minWidth="146dp"
    android:minHeight="146dp"
    android:previewImage="@mipmap/ic_launcher"
    android:resizeMode="vertical|horizontal"
    android:updatePeriodMillis="0"
    android:widgetCategory="home_screen">
</appwidget-provider>
Triplicity answered 18/11, 2020 at 17:31 Comment(14)
Please post the layout fileAlimentation
I added the widget layout file, please look, thank youTriplicity
Have you overrided onUpdate() on MainWidget?Alimentation
as you can see in the code, I didn't, I posted the entire class of it, there are only variables and onReceiveTriplicity
From where is onReceive() (MainWidget) called?Alimentation
it's a function that gets called automatically developer.android.com/reference/android/appwidget/…Triplicity
Let us continue this discussion in chat.Alimentation
Is it so? Isn't onUpdate() called automatically?Alimentation
I didn't override the onUpdate method, I use the onReceiveTriplicity
Oh! You are using <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> intent-filter. Right?Alimentation
yes, in the manifest, and also there's this one: <meta-data android:name="android.appwidget.provider" android:resource="@xml/clock_widget" />Triplicity
Yeah. Back to the point... I have also got this error. It was due to unsupported layout components. I can't figure out the reason here...Alimentation
Hi, can you just show me clock_widget.xml?Pincushion
I added the clock_widget.xmlTriplicity
P
4

Check elements you used in the view in a Widget..

Documentation link

A RemoteViews object (and, consequently, an App Widget) can support the following layout classes:

  • FrameLayout

  • LinearLayout

  • RelativeLayout

  • GridLayout

And the following widget classes:

  1. AnalogClock
  2. Button
  3. Chronometer
  4. ImageButton
  5. ImageView
  6. ProgressBar
  7. TextView
  8. ViewFlipper
  9. ListView
  10. GridView
  11. StackView
  12. AdapterViewFlipper

Using forbidden elements causes this very

Problem Loading Widget message, without saying where did it happen.

Pincushion answered 23/11, 2020 at 21:4 Comment(5)
I used only the allowed layouts and classes on the widget layout as you can see in the cod eI postedTriplicity
com.l_es.analogclockcustomization.AnalogClockDesign This is a layout not present in above list but in your clock_widget_layout.xmlPincushion
its instead of the AnalogClock you specify in your list, b/c the AnalogClock is deprecated I took the origin code and used it instead of the android libraryTriplicity
You have to use exactly those classes - you can't create a copy of AnalogClock that works with RemoteViews.Lugar
the AnalogClock is deprecated, so how can I use it? I used the exact same code 1 to 1 from the android developer siteTriplicity

© 2022 - 2024 — McMap. All rights reserved.