gpt4 book ai didi

android - Widget Application ForceCloses 安装

转载 作者:行者123 更新时间:2023-11-29 02:10:36 25 4
gpt4 key购买 nike

我遇到了障碍。我遇到了一系列强制关闭。当我在模拟器中运行我的程序时,它会在我放置小部件之前强制关闭安装。

本质上,我试图制作一个带有按钮的小部件。该按钮假定启动服务以将小部件更改为新布局(菜单)。还有一个配置 Activity 。

如果我需要添加更多信息,请告诉我。我一直在使用 android 开发页面和 Lars Vogels 教程作为引用。 http://www.vogella.de/articles/AndroidWidgets/article.html

谢谢安息吧,SJ。我从来都不是 mac-ie,但我很欣赏你的工作。

提供者:

public class CodeSummitWidgetProvider extends AppWidgetProvider{

public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {

Intent intent = new Intent(context.getApplicationContext(), TestService.class);
PendingIntent pendingIntent = PendingIntent.getService(context.getApplicationContext(), 0, intent, 0);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.codesummit_widget_provider_layout);
views.setOnClickPendingIntent(R.id.tomenu, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds, views);
context.startService(intent);

}
}

服务:

public class TestService extends Service {

@Override
public void onStart(Intent intent, int startId) {


AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this.getApplicationContext());

int[] appWidgetIds = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);

if (appWidgetIds.length > 0) {
for(int widgetId : appWidgetIds) {
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.codesummit_widget_menu_layout);
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
stopSelf();
}

super.onStart(intent, startId);
}

list :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.main.codesummit"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />

<application android:icon="@drawable/icon" android:label="@string/app_name">
<receiver android:name="CodeSummitWidgetProvider">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/codesummit_appwidget_info" />
</receiver>
<activity android:name=".CodeSummitWidgetConfigure">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
</intent-filter>
</activity>
<service android:name=".TestService" />

</application>

LogCat:

10-06 23:59:04.925: INFO/ActivityManager(65): Start proc com.main.codesummit for broadcast com.main.codesummit/.CodeSummitWidgetProvider: pid=312 uid=10036 gids={}
10-06 23:59:05.596: DEBUG/AndroidRuntime(312): Shutting down VM
10-06 23:59:05.606: WARN/dalvikvm(312): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
10-06 23:59:05.665: ERROR/AndroidRuntime(312): FATAL EXCEPTION: main
10-06 23:59:05.665: ERROR/AndroidRuntime(312): java.lang.RuntimeException: Unable to start service com.main.codesummit.TestService@44ee5a38 with Intent { cmp=com.main.codesummit/.TestService }: java.lang.NullPointerException
10-06 23:59:05.665: ERROR/AndroidRuntime(312): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3063)
10-06 23:59:05.665: ERROR/AndroidRuntime(312): at android.app.ActivityThread.access$3600(ActivityThread.java:125)
10-06 23:59:05.665: ERROR/AndroidRuntime(312): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2096)
10-06 23:59:05.665: ERROR/AndroidRuntime(312): at android.os.Handler.dispatchMessage(Handler.java:99)
10-06 23:59:05.665: ERROR/AndroidRuntime(312): at android.os.Looper.loop(Looper.java:123)
10-06 23:59:05.665: ERROR/AndroidRuntime(312): at android.app.ActivityThread.main(ActivityThread.java:4627)
10-06 23:59:05.665: ERROR/AndroidRuntime(312): at java.lang.reflect.Method.invokeNative(Native Method)
10-06 23:59:05.665: ERROR/AndroidRuntime(312): at java.lang.reflect.Method.invoke(Method.java:521)
10-06 23:59:05.665: ERROR/AndroidRuntime(312): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-06 23:59:05.665: ERROR/AndroidRuntime(312): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-06 23:59:05.665: ERROR/AndroidRuntime(312): at dalvik.system.NativeStart.main(Native Method)
10-06 23:59:05.665: ERROR/AndroidRuntime(312): Caused by: java.lang.NullPointerException
10-06 23:59:05.665: ERROR/AndroidRuntime(312): at com.main.codesummit.TestService.onStart(TestService.java:23)
10-06 23:59:05.665: ERROR/AndroidRuntime(312): at android.app.Service.onStartCommand(Service.java:420)
10-06 23:59:05.665: ERROR/AndroidRuntime(312): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3053)
10-06 23:59:05.665: ERROR/AndroidRuntime(312): ... 10 more
10-06 23:59:10.696: INFO/Process(312): Sending signal. PID: 312 SIG: 9
10-06 23:59:10.795: INFO/ActivityManager(65): Process com.main.codesummit (pid 312) has died.

最佳答案

在您的 onUpdate 方法中,您在几个地方使用了错误的上下文。传递给 onUpdate 的上下文仅在 onUpdate 函数完成执行之前有效。所以你不能将它用于任何你想活得比这个函数运行更长的东西,例如你的服务。应用程序上下文在应用程序的生命周期内存在。要从 onUpdate 调用 context.getApplicationContext() 获取它。

因此您需要更改这些行的上下文:

Intent intent = new Intent(context, TestService.class);
PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);

收件人:

Intent intent = new Intent(context.getApplicationContext(), TestService.class);
PendingIntent pendingIntent = PendingIntent.getService(context.getApplicationContext(), 0, intent, 0);

该类的访问级别也不正确

class TestService extends Service {

将其设为公共(public)类(class)。由于它不是公开的,操作系统在尝试启动服务时无法使用它。

关于android - Widget Application ForceCloses 安装,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7669717/

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