Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Problem loading widget - without errors in the logcat

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>
like image 446
Lidor Eliyahu Shelef Avatar asked Nov 18 '20 17:11

Lidor Eliyahu Shelef


People also ask

What are widgets in Android user interface?

A widget is a View object that serves as an interface for interaction with the user. Android provides a set of fully implemented widgets, like buttons, checkboxes, and text-entry fields, so you can quickly build your UI. Some widgets provided by Android are more complex, like a date picker, a clock, and zoom controls.

How do I use logcat in Android Studio?

Logcat window in Android Studio is used to display real-time system messages and messages that are added in the Log class of the app. To open Logcat Click View > Tool Windows > Logcat (Alt + 6 or from the toolbar window). Sometimes logcat shows nothing and it’s completely blank.

How to fix “problem loading widget” error on Android devices?

Widgets associated with apps stored on the SD card tend to malfunction and as a result, the error message “Problem Loading Widget” pops up on the screen. The only way to fix this problem is by transferring these apps to your internal storage. A lot of Android users have been able to fix this problem by removing apps from the SD card.

How to fix logcat not showing up?

To open Logcat Click View > Tool Windows > Logcat (Alt + 6 or from the toolbar window). Sometimes logcat shows nothing and it’s completely blank. This happens often and there can be different reasons. Below are some quick solutions that might work. You need to press Alt + 6 twice to restart the Logcat.

What is the use of logcat?

Logcat is a command-line tool that dumps a log of system messages, including stack traces when the device throws an error and messages that you have written from your app with the Log class.


1 Answers

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.

like image 80
Sarah Khan Avatar answered Nov 13 '22 10:11

Sarah Khan