我一直在为 Android 开发一个小部件。为此,我试图显示未读消息的数量以及当前时间。当前时间工作得很好,但是现在我已经添加了消息部分,当我尝试加载它时它会崩溃。这是我的代码:
public class MyTime extends TimerTask {
RemoteViews remoteViews;
AppWidgetManager appWidgetManager;
ComponentName thisWidget;
Context context;
java.text.DateFormat format = SimpleDateFormat.getTimeInstance(
SimpleDateFormat.SHORT, Locale.getDefault());
public MyTime(Context context, AppWidgetManager appWidgetManager) {
this.appWidgetManager = appWidgetManager;
remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
thisWidget = new ComponentName(context, LeafClockWidget.class);
}
@Override
public void run() {
SimpleDateFormat sdf1 = new SimpleDateFormat("EEEE, MMMM dd");
Date d = new Date(System.currentTimeMillis());
String time1 = sdf1.format(d);
final Uri SMS_INBOX = Uri.parse("content://sms/inbox");
Cursor c = context.getContentResolver().query(SMS_INBOX, null, "read = 0", null, null);
int unreadMessagesCount = c.getCount();
c.deactivate();
remoteViews.setTextViewText(R.id.widget_textview3, "You have " + c + " unread SMS messages.");
remoteViews.setTextViewText(R.id.widget_textview1, time1);
remoteViews.setTextViewText(R.id.widget_textview2, "The time is "
+ format.format(new Date(System.currentTimeMillis())));
appWidgetManager.updateAppWidget(thisWidget, remoteViews);
}
}
我很难找到我的代码中的错误,所以我真的希望你们能帮助我!