android问题加载小部件-在logcat中没有错误

mcdcgff0  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(434)

我在java中创建了一个模拟时钟小部件,由于某些原因,我总是遇到“加载小部件时出现问题”,当我尝试放置小部件时,从logcat中得到的唯一东西是:

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

小部件只是一个黑色的正方形,上面写着“加载小部件时出现问题”。
下面是小部件类:

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

        }
    }

}

这是我用来绘制时钟的绘图函数:

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

这是我的小部件布局文件,请记住,我每次都将3个小部件中的2个设置为可见性为“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>

时钟小部件.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>
wixjitnu

wixjitnu1#

检查小部件视图中使用的元素。。
文档链接
remoteviews对象(以及相应的应用程序小部件)可以支持以下布局类:
框架布局
线性布局
相对布局
网格布局
以及以下小部件类:
模拟时钟
按钮
计时器
图像按钮
图像视图
控件
文本视图
视图翻转器
列表视图
网格视图
堆栈视图
AdapterViewLipper适配器
使用禁制元素会导致
加载小部件消息时出现问题,但没有说明它发生在哪里。

相关问题