我正在开发一个由时钟组成的 android 小部件。到目前为止,我能够成功地将时钟设置为当前时间,但无法更新它。我正在使用Intent.ACTION_TIME_TICK
广播接收器。我正在注册广播接收器onEnable()
并删除onDeleted()
. 接收器正在正确注册和注销,但是当我更新views
. 这是我正在使用的代码
@Override
public void onEnabled(final Context context) {
// TODO Auto-generated method stub
System.out.println("Entering onstart");
_broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context ctx, Intent intent) {
System.out.println("Registering BroadCast");
if (intent.getAction().compareTo(Intent.ACTION_TIME_TICK) == 0) {
System.out.println(_sdfWatchTime.format(new Date()));
//class to get drawable images that will be used to denote hours and minutes
GetDrawableResources getDrawableResources = new GetDrawableResources();
int time[] = new int[new GetTime().getTime(context
.getApplicationContext()).length];
//class to get each digit of time seperatly eg 10:10, separating 1 0 1 0 working correctly
time = new GetTime().getTime(context
.getApplicationContext());
//changing views
for (int i = 0; i < awID.length; i++) {
System.out.println("awID for " + awID[i]);
RemoteViews views = new RemoteViews(
context.getApplicationContext().getPackageName(),
R.layout.activity_main);
views.setImageViewResource(R.id.ivHour0,
getDrawableResources.setTimeDrawable(time[0]));
views.setImageViewResource(R.id.ivHour1,
getDrawableResources.setTimeDrawable(time[1]));
views.setImageViewResource(R.id.ivMinute0,
getDrawableResources.setTimeDrawable(time[2]));
views.setImageViewResource(R.id.ivMinute1,
getDrawableResources.setTimeDrawable(time[3]));
//problem is pointing to line below
awm.updateAppWidget(awID[i], views);
}
}
}
};
context.getApplicationContext().registerReceiver(_broadcastReceiver,
new IntentFilter(Intent.ACTION_TIME_TICK));
super.onEnabled(context);
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// TODO Auto-generated method stub
super.onUpdate(context, appWidgetManager, appWidgetIds);
final int N = appWidgetIds.length; // more than 1 same widget added
awID = new int[N];
awID = appWidgetIds;
awm = appWidgetManager;
for (int t : awID) {
System.out.println("awID onUpdate" + t);
}
}
我得到的错误
06-28 11:27:00.078: E/AndroidRuntime(453): FATAL EXCEPTION: main
06-28 11:27:00.078: E/AndroidRuntime(453): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.intent.action.TIME_TICK flg=0x40000004 (has extras) } in com.itcse.weather.MainActivity$1@40520228
06-28 11:27:00.078: E/AndroidRuntime(453): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:722)
06-28 11:27:00.078: E/AndroidRuntime(453): at android.os.Handler.handleCallback(Handler.java:587)
06-28 11:27:00.078: E/AndroidRuntime(453): at android.os.Handler.dispatchMessage(Handler.java:92)
06-28 11:27:00.078: E/AndroidRuntime(453): at android.os.Looper.loop(Looper.java:123)
06-28 11:27:00.078: E/AndroidRuntime(453): at android.app.ActivityThread.main(ActivityThread.java:3683)
06-28 11:27:00.078: E/AndroidRuntime(453): at java.lang.reflect.Method.invokeNative(Native Method)
06-28 11:27:00.078: E/AndroidRuntime(453): at java.lang.reflect.Method.invoke(Method.java:507)
06-28 11:27:00.078: E/AndroidRuntime(453): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-28 11:27:00.078: E/AndroidRuntime(453): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-28 11:27:00.078: E/AndroidRuntime(453): at dalvik.system.NativeStart.main(Native Method)
06-28 11:27:00.078: E/AndroidRuntime(453): Caused by: java.lang.NullPointerException
06-28 11:27:00.078: E/AndroidRuntime(453): at com.itcse.weather.MainActivity$1.onReceive(MainActivity.java:57)
06-28 11:27:00.078: E/AndroidRuntime(453): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:709)
06-28 11:27:00.078: E/AndroidRuntime(453): ... 9 more
更新 null 错误被删除,代码运行成功,但视图没有更新。