2012-08-31 5 views
0

Android 용 위젯에서 작업했습니다. 이를 위해 읽지 않은 메시지의 양과 현재 시간을 표시하려고합니다. 현재 시간은 정상적으로 작동하지만, 이제는로드하려고 할 때 충돌하는 메시지 부분을 추가했습니다. 이것은 내 코드입니다.Android에서 새 SMS 메시지 수신시 오류가 발생했습니다.

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

} 

오류 코드가 어디에 있는지 찾는 데 어려움이 있습니다. 그래서 사람들이 저를 도울 수 있기를 바랍니다.

+0

당신이 오류의 종류를 알고 로그 고양이를 게시 할 수 없습니다 : -

빠른 팁, 당신은 try 사용할 수있는 코드에서 오류를 발견 할 catch 및보고하여 여기

LogCat은 예입니다 너 점점? – Swayam

답변

0

편집 :

첫째, c.deactivate()since it is deprecated를 제거합니다.

나는 ContentResolver에 익숙하지 않으므로, 필자가 제공 한 조언은 저의 연구 결과에 기반하고 있습니다. 귀하의 문제는 일 수 있습니다. Cursorquery()WHERE 조항이이 될 수 있으십니까? this answer을 확인하여 도움이되는지 확인하십시오. 당신은 당신이 문제/예외가있을 때마다 당신의 가장 친한 친구가,/그래서 우리는 더 나은 도움이 될 수 있습니다 당신이 질문에 로그 캣 출력을 붙여 넣기해야 오류가 그/그녀의 if(c != null)c.moveToFirst()


로그 캣과 매우 유사한 무언가를 할 것입니다 .

@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); 
} 
+0

답변 해 주셔서 감사합니다. Cursor에서 반복되는 null 포인터 예외가 있음을 발견했습니다. – Zero

+0

도와 드리겠습니다. 도움이 필요하거나 도움이 필요하십니까? – jnthnjns

+0

예, 제게 초보자 인 것 같습니다 :). – Zero

관련 문제