gpt4 book ai didi

android - 如何在不破坏 Robolectric 的情况下添加 WidgetProvider

转载 作者:行者123 更新时间:2023-11-30 04:01:11 25 4
gpt4 key购买 nike

我有一个使用 Robolectric 1.1 和 Roboguice 2 的测试套件。它运行良好。

我将 AppWidget 添加到应用程序 list 中,如下所示:

<receiver android:name="NearestOpenCragWidgetProvider" android:exported="false">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/nearest_open_crag_widget_info" />
</receiver>

我创建了一个类

public class NearestOpenCragWidgetProvider extends AppWidgetProvider {
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
final int N = appWidgetIds.length;

// Perform this loop procedure for each App Widget that belongs to this provider
for (int i=0; i<N; i++) {
int appWidgetId = appWidgetIds[i];

//get the layout
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.nearest_open_crag_widget);

// Tell the AppWidgetManager to perform an update on the current app widget
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
}
}
}

和 res/xml 中的 AppWidgetProviderInfo xml

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" >
android:minWidth="294dp"
android:minHeight="72dp"
android:updatePeriodMillis="86400000"
android:initialLayout="@layout/nearest_open_crag_widget"
android:resizeMode="none">
</appwidget-provider>

并且还添加了布局 nearest_open_crag_widget

现在,当我运行我的测试时,它们会立即失败

java.lang.ArrayIndexOutOfBoundsException: -1
at com.xtremelabs.robolectric.ApplicationResolver.replaceLastDotWith$IfInnerStaticClass(ApplicationResolver.java:55)
at com.xtremelabs.robolectric.ApplicationResolver.registerBroadcastReceivers(ApplicationResolver.java:47)
at com.xtremelabs.robolectric.ApplicationResolver.resolveApplication(ApplicationResolver.java:36)
at com.xtremelabs.robolectric.RobolectricTestRunner.createApplication(RobolectricTestRunner.java:491)
at com.xtremelabs.robolectric.RobolectricTestRunner.setupApplicationState(RobolectricTestRunner.java:368)
at com.xtremelabs.robolectric.RobolectricTestRunner.internalBeforeTest(RobolectricTestRunner.java:307)
at com.xtremelabs.robolectric.RobolectricTestRunner.methodBlock(RobolectricTestRunner.java:285)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

应用程序以 2.3 为目标,如果我在 Android Manifest 中注释掉 Widget 声明,测试将再次开始运行。

我显然遇到了一些问题,想知道主脑是否能发现它。

最佳答案

原来问题应该是“如何添加小部件提供者而不是白痴”。如果我在 android list 中使用 Provider 的完全限定名称,那么一切都很好

<receiver android:name="uk.co.some.package.NearestOpenCragWidgetProvider" android:exported="false">
<blah />
</receiver>

关于android - 如何在不破坏 Robolectric 的情况下添加 WidgetProvider,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12492483/

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