gpt4 book ai didi

java - Android问题加载小部件-logcat中没有错误

转载 作者:行者123 更新时间:2023-12-03 11:17:41 25 4
gpt4 key购买 nike

我在 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
小部件只是一个黑色方 block ,上面有“问题加载小部件”。
这是小部件类:
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 个设置为“消失”的可见性:
<?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>

最佳答案

检查您在小部件中的 View 中使用的元素..
Documentation link
RemoteViews 对象(以及相应的 App Widget)可以支持以下布局类:

  • 框架布局
  • 线性布局
  • 相对布局
  • 网格布局

  • 以及以下小部件类:
  • 模拟时钟
  • 按钮
  • 天文台表
  • 图片按钮
  • 图像查看
  • 进度条
  • TextView
  • ViewFlipper
  • ListView
  • GridView
  • 堆栈 View
  • AdapterViewFlipper

  • 使用禁止元素会导致这种情况
    加载小部件时出现问题
    消息,没有说它发生在哪里。

    关于java - Android问题加载小部件-logcat中没有错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64898244/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com