2011-06-04 64 views
5

我在我的應用程序中遇到問題。我有一些名單,每個名單都有一個日期。如果列表的日期是今天我做了一個通知,並在我的通知中,我把這個列表中的項目。問題是,如果我的清單空我得到強制關閉。我只是在光標不爲空的情況下才會發出通知,但看起來這不起作用。你能幫我找出問題在哪裏嗎?Android中的空遊標

下面是代碼的一部分:獲取數據()之前

if (cr != null && cr.moveToFirst()) { 
    // ... 
} 

您需要moveToFirst:

cr = db.fetchListId(id); 
startManagingCursor(cr); 

s = new StringBuilder(100); 
if (cr != null) { 
    do { 
    String myList = ""; 
    myList += cr.getString(2) + " " + cr.getString(3) 
     + cr.getString(4) + "," + "\n"; 
    s.append(myList); 

    } while (cr.moveToNext()); 
    s.append('.'); 
    System.out.println("!!heyeheeye!" + s + "!!!!"); 
    hey(list); 
} 

where hey(list) is a function where is made the notification. 

    private void hey(String list) { 
    notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
    CharSequence NotificationTicket = "Hey"; 

    long when = System.currentTimeMillis(); 

    Notification notification = new Notification(R.drawable.icon, 
               NotificationTicket, when); 

    RemoteViews contentView = new RemoteViews(getPackageName(), 
              R.layout.proba); 
    contentView.setImageViewResource(R.id.image, R.drawable.icon); 
    contentView.setTextViewText(R.id.notif, 
           "Today,you must go for shopping for your list '" + list + "'. " 
           + "Don't forget!!!" + "\n" + "You must buy :" + "\n" 
           + s.toString() + "\n"); 
    notification.contentView = contentView; 


    Context context = getApplicationContext(); 

    Intent notifyIntent = new Intent(context, Lists.class); 

    Intent notificationIntent = new Intent(this, Lists.class); 
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, 
                  notificationIntent, 0); 
    notification.contentIntent = contentIntent; 

    notificationManager.notify(ID_NOTIFICATION, notification); 
    notifyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP 


       | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
} 

回答

6

添加下面的檢查,看是否遊標有任何結果。因爲你正在使用do ...而不是while(),所以你沒有正確地初始化光標。

+0

謝謝。你說得對。現在,它的工作.. – Gaby 2011-06-04 11:40:40