gpt4 book ai didi

android - 尝试从配置 Activity 配置 widjget 时出现空指针异常

转载 作者:搜寻专家 更新时间:2023-11-01 08:01:50 24 4
gpt4 key购买 nike

我有一个小部件,因为它将由一个 Activity 配置,因为我可以从 Activity 中更改小部件的外观...只有当用户尝试添加小部件时, Activity 才会在第一次打开,此时只会调用 Activity 现在首先用户更改它的设置,当按下保存按钮时,小部件将被更新。

11-27 12:50:59.546: E/AndroidRuntime(15346): FATAL EXCEPTION: main
11-27 12:50:59.546: E/AndroidRuntime(15346): java.lang.NullPointerException
11-27 12:50:59.546: E/AndroidRuntime(15346): at com.android.launcher2.Launcher.getMinSpanForWidget(Launcher.java:1234)
11-27 12:50:59.546: E/AndroidRuntime(15346): at com.android.launcher2.Launcher.completeAddAppWidget(Launcher.java:1270)
11-27 12:50:59.546: E/AndroidRuntime(15346): at com.android.launcher2.Launcher.access$1000(Launcher.java:121)
11-27 12:50:59.546: E/AndroidRuntime(15346): at com.android.launcher2.Launcher$4.run(Launcher.java:766)
11-27 12:50:59.546: E/AndroidRuntime(15346): at com.android.launcher2.Workspace$9.run(Workspace.java:3474)
11-27 12:50:59.546: E/AndroidRuntime(15346): at com.android.launcher2.DragLayer$3.onAnimationEnd(DragLayer.java:670)
11-27 12:50:59.546: E/AndroidRuntime(15346): at android.animation.ValueAnimator.endAnimation(ValueAnimator.java:1018)
11-27 12:50:59.546: E/AndroidRuntime(15346): at android.animation.ValueAnimator.access$400(ValueAnimator.java:51)
11-27 12:50:59.546: E/AndroidRuntime(15346): at android.animation.ValueAnimator$AnimationHandler.doAnimationFrame(ValueAnimator.java:623)
11-27 12:50:59.546: E/AndroidRuntime(15346): at android.animation.ValueAnimator$AnimationHandler.run(ValueAnimator.java:639)
11-27 12:50:59.546: E/AndroidRuntime(15346): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:747)
11-27 12:50:59.546: E/AndroidRuntime(15346): at android.view.Choreographer.doCallbacks(Choreographer.java:567)
11-27 12:50:59.546: E/AndroidRuntime(15346): at android.view.Choreographer.doFrame(Choreographer.java:535)
11-27 12:50:59.546: E/AndroidRuntime(15346): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:733)
11-27 12:50:59.546: E/AndroidRuntime(15346): at android.os.Handler.handleCallback(Handler.java:615)
11-27 12:50:59.546: E/AndroidRuntime(15346): at android.os.Handler.dispatchMessage(Handler.java:92)
11-27 12:50:59.546: E/AndroidRuntime(15346): at android.os.Looper.loop(Looper.java:153)
11-27 12:50:59.546: E/AndroidRuntime(15346): at android.app.ActivityThread.main(ActivityThread.java:5086)
11-27 12:50:59.546: E/AndroidRuntime(15346): at java.lang.reflect.Method.invokeNative(Native Method)
11-27 12:50:59.546: E/AndroidRuntime(15346): at java.lang.reflect.Method.invoke(Method.java:511)
11-27 12:50:59.546: E/AndroidRuntime(15346): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
11-27 12:50:59.546: E/AndroidRuntime(15346): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
11-27 12:50:59.546: E/AndroidRuntime(15346): at dalvik.system.NativeStart.main(Native Method)
11-27 12:50:59.548: V/Provider/Setting(353): from settings cache , name = dropbox:system_app_crash value = null

这是我的代码,当我点击保存按钮时下面的代码将被执行

 Intent intent = new Intent(getBaseContext(),
Configuration.class);

intent.putExtra(
AppWidgetManager.EXTRA_APPWIDGET_ID,
0);

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

PendingIntent pendingIntent = PendingIntent
.getActivity(getBaseContext(),
0,
intent,
PendingIntent.FLAG_UPDATE_CURRENT);

// Getting an instance of
// WidgetManager
AppWidgetManager appWidgetManager = AppWidgetManager
.getInstance(getBaseContext());

// Instantiating the class RemoteViews
// with widget_layout
RemoteViews views = new RemoteViews(
getBaseContext().getPackageName(),
R.layout.main);

appWidgetManager.updateAppWidget(0, views);

// Return RESULT_OK from this activity
Intent resultValue = new Intent();
resultValue.putExtra(
AppWidgetManager.EXTRA_APPWIDGET_ID,
0);
setResult(RESULT_OK, resultValue);
finish();

最佳答案

嗯,@Komal 我知道你的问题了,widget_id 有问题

在代码中,您传递的小部件 ID 为零

        resultValue.putExtra(
AppWidgetManager.EXTRA_APPWIDGET_ID,
0);

这里是主要问题,而不是“0”,你必须首先通过以下方式获取当前小部件的 appwidget_id

先声明全局变量

     private int mAppWidgetId = 0; 

现在在 onCreate() 方法上通过以下方式获取应用程序小部件 ID

 mAppWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,AppWidgetManager.INVALID_APPWIDGET_ID);

现在把这个mAppWidgetId放在

Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);

代替

 resultValue.putExtra(
AppWidgetManager.EXTRA_APPWIDGET_ID,
0);

您将解决问题。 :)

关于android - 尝试从配置 Activity 配置 widjget 时出现空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20236262/

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