0

I am getting a NullPointerException (sometimes) and my appwidget crashes. It happens with any remote view's setTextViewText. I have checked the values I want to set and it is definitely not null.

Here's the code snippet :

public void onReceive(Context context, Intent intent) {
     if(intent.getAction().equalsIgnoreCase(UPDATE)){
             ComponentName thisWidget = new ComponentName(context, abc.class);

            try
            {

                String currentDay = todayDay();
                String current = todayDate();
                System.out.println("On Receive : " + R.id.day + currentDay);
                views.setTextViewText(R.id.day, currentDay);
                views.setTextViewText(R.id.date, current);
                performUpdate(context);
            }
            catch(NullPointerException e)
            {
                e.printStackTrace();
            }

            AppWidgetManager manager = AppWidgetManager.getInstance(context);

            //update widget
            manager.updateAppWidget(thisWidget, views);

        }
}
public static String todayDay(){
    Calendar calendar = Calendar.getInstance();
    int day = calendar.get(Calendar.DAY_OF_WEEK); 

    String dayToReturn="";

    switch(day){
    case 1: dayToReturn = "Sunday";
            break;
    case 2: dayToReturn = "Monday";
            break;
    case 3: dayToReturn = "Tuesday";
            break;
    case 4: dayToReturn = "Wednesday";
            break;
    case 5: dayToReturn = "Thursday";
            break;
    case 6: dayToReturn = "Friday";
            break;
    case 7: dayToReturn = "Saturday";
            break;
    }
    return dayToReturn;
}

Logcat error:

01-04 14:38:32.055: W/System.err(26368): java.lang.NullPointerException
01-04 14:38:32.060: W/System.err(26368):    at com.appwidgets.abc.onReceive(abc.java:167)
01-04 14:38:32.060: W/System.err(26368):    at android.app.ActivityThread.handleReceiver(ActivityThread.java:2133)
01-04 14:38:32.060: W/System.err(26368):    at android.app.ActivityThread.access$1500(ActivityThread.java:128)
01-04 14:38:32.060: W/System.err(26368):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1211)
01-04 14:38:32.060: W/System.err(26368):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-04 14:38:32.060: W/System.err(26368):    at android.os.Looper.loop(Looper.java:137)
01-04 14:38:32.060: W/System.err(26368):    at android.app.ActivityThread.main(ActivityThread.java:4514)
01-04 14:38:32.060: W/System.err(26368):    at java.lang.reflect.Method.invokeNative(Native Method)
01-04 14:38:32.060: W/System.err(26368):    at java.lang.reflect.Method.invoke(Method.java:511)
01-04 14:38:32.060: W/System.err(26368):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
01-04 14:38:32.060: W/System.err(26368):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
01-04 14:38:32.060: W/System.err(26368):    at dalvik.system.NativeStart.main(Native Method)

Although I've added try catch block to avoid this, it does not help. The same problem just appears off and on. Any help is much appreciated :)

Annonymous
  • 83
  • 1
  • 8

1 Answers1

0

After calling the remoteViews.setTextViewText you need to update the widget with a call to updateAppWidget.

AppWidgetManager manager = AppWidgetManager.getInstance(context);
manager.updateAppWidget(thisWidget, remoteViews);
Rahul Bisht
  • 482
  • 3
  • 5
  • Can you try posting the whole code plz? the current code is too less to pick out the error. ;) – Rahul Bisht Jan 04 '13 at 06:47
  • Am not sure but maybe the getaction() is not working as it is supposed to. Try giving onNewIntent() a try instead of intent.getAction(). – Rahul Bisht Jan 04 '13 at 07:04
  • hmm the getaction() is to identify which intent is received. It went in the loop so I guess its working fine there...the error appears randomly and not everytime. thats why I have no clue at all :( thanks anyway! :D – Annonymous Jan 04 '13 at 08:14