0

I have been working on a widget for Android. For this I am trying to display the amount of unread messages as well as the current time. The current time was working just fine, but now that I have added the messages part it crashes when I try to load it. This is my code:

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);
}

}

I am having trouble finding where the error is in my code, so I really hope you guys can help me out!

Zero
  • 1,864
  • 3
  • 21
  • 39

1 Answers1

0

Edit:

First, remove c.deactivate() since it is deprecated.

I am unfamiliar with ContentResolver's, so just note that any advice I give is based on my own research. I think your issue might be your WHERE clause in your Cursor query()? Check out this answer to see if it helps any. You'll want something very similar to his/her if(c != null) and c.moveToFirst()


LogCat is your best friend, whenever you have an issue / exception / error you should paste your LogCat output in the question so we can better help.

Quick tip, to find errors in your code you can use try - catch and watch your LogCat

Here is an example:

@Override
public void run() {
    try {
        SimpleDateFormat sdf1 = new SimpleDateFormat("EEEE, MMMM dd");
        Date d = new Date(System.currentTimeMillis());
        String time1 = sdf1.format(d);
    } catch(Exception e1) {
        Log.e("SimpleDateFormat", "FAILED: " + e1.getMessage());
    }


    final Uri SMS_INBOX = Uri.parse("content://sms/inbox");

    try {
        Cursor c = context.getContentResolver().query(SMS_INBOX, null, "read = 0", null, null);
        int unreadMessagesCount = c.getCount();
        c.deactivate();
    } catch (Exception e2) {
        Log.e("Cursor", "FAILED: " + e2.getMessage());
    }

    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);
}
Community
  • 1
  • 1
jnthnjns
  • 8,962
  • 4
  • 42
  • 65
  • Thanks for your response! I found that there is a repeating null pointer exception at Cursor. – Zero Aug 31 '12 at 16:57
  • Glad I could help, did you get it fixed or still need help? – jnthnjns Aug 31 '12 at 16:58
  • Yes please, I am still somewhat of a novice :) . – Zero Aug 31 '12 at 17:06
  • Ok the problem seems to be the "read = 0". I changed it to "read = SMS_INBOX" and now it doesn't crash anymore, but it also still doesn't display the amount of new messages. – Zero Aug 31 '12 at 17:51
  • I also noticed that the "c" in the setTextViewText should be "unreadMessagesCount" :) . – Zero Aug 31 '12 at 17:53
  • Oh and even though it doesn't crash anymore, I still get the null pointer exception at Cursor... – Zero Aug 31 '12 at 17:53