我有一个小部件,它将由一个活动配置,因为我可以从活动中更改小部件的外观...活动将仅在用户尝试添加小部件时才会打开第一次只会调用活动首先现在用户更改它的设置,当按下保存按钮小部件时将更新。
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();
答案 0 :(得分:3)
嗯@Komal我得到你的问题widget_id有问题
这里在代码中你传递widget id为零
resultValue.putExtra(
AppWidgetManager.EXTRA_APPWIDGET_ID,
0);
这是主要问题,而不是“0”你必须首先获得当前小部件的appwidget_id
首先声明全局变量
private int mAppWidgetId = 0;
现在onCreate()方法通过
获取app widget 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);
你将解决问题。 :)
答案 1 :(得分:1)
根据AppWidgetManager.updateAppWidget,第一个参数应该是appWidgetId。你确定这应该是0吗?
此外,不使用此行的结果:
PendingIntent pendingIntent = PendingIntent
.getActivity(getBaseContext(),
0,
intent,
PendingIntent.FLAG_UPDATE_CURRENT);