我正在开发一个由时钟组成的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错误,代码运行成功,但视图未更新。
答案 0 :(得分:1)
您的MainActivity第57行出现了NullPointerException:
com.itcse.weather.MainActivity$1.onReceive(MainActivity.java:57)
你应该在那里放一个断点,看看什么是空的。
如果您无法让调试器停在那里(有时会在Receivers中发生),请调试Debug.waitForDebugger();在你的断点之前。