2

To be short...I have a widget, you can see the important part below

public class ExampleWidget extends AppWidgetProvider {
private PhoneStateListener listener;
private TelephonyManager telephonyManager;

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int[] appWidgetIds) {

    listener = new PhoneStateListener() {
        @Override
        public void onDataConnectionStateChanged(int state) {
            // removed the code
        }

    };

    telephonyManager = (TelephonyManager) context
            .getSystemService(Context.TELEPHONY_SERVICE);
    telephonyManager.listen(listener,
            PhoneStateListener.LISTEN_DATA_CONNECTION_STATE);

}

@Override
public void onDisabled(Context context) {
    telephonyManager.listen(listener, PhoneStateListener.LISTEN_NONE);
    super.onDisabled(context);
}
}

and I am receiving nullpointer exception at telephonyManager.listen(listener, PhoneStateListener.LISTEN_NONE); when I am removing the widget from the main screen.

What am I missing?

  • Where does the NPE happen exactly? Right within the line you stated or inside .listen()? Do you have a config-activity for your widget? – marsbear Aug 09 '11 at 11:44

4 Answers4

2

context.getSystemService() may return null and you do not prove telephonyManager if it is null. If the name identified by Context.TELEPHONY_SERVICE does not exist in the system the telephonyManager will be null.

In addition to your comment:

@Override
public void onDisabled(Context context) {
    if (telephonyManager!=null){
        telephonyManager.listen(listener, PhoneStateListener.LISTEN_NONE);
    }
    super.onDisabled(context);
}

If you need to run this code in the onDisabled Method you should initialize the telephonyManager. It smells like the onDisabled method is called somehow before onUpdate or if you have two different instances.

fyr
  • 20,227
  • 7
  • 37
  • 53
  • It's not null, because I am using it, and it works, I just removed the code where I am using it. Just in onDisabled() it says it's null. –  Aug 09 '11 at 11:49
  • The method isn't called before onUpdate(). I tracked with Log. –  Aug 09 '11 at 11:55
  • Did you also track if it is the same instance of this AppWidgetProvider ? – fyr Aug 09 '11 at 11:56
  • No, i didn't. How do I do that? –  Aug 09 '11 at 11:59
  • The one in onCreate() is 1156858336, the one in onDisabled() is 0. –  Aug 09 '11 at 12:05
  • @AlexAdy let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/2266/discussion-between-fyr-and-alex-ady) – fyr Aug 09 '11 at 12:06
0
public class ExampleWidget extends AppWidgetProvider {
private PhoneStateListener listener;
private TelephonyManager telephonyManager;

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int[] appWidgetIds) {


    telephonyManager = (TelephonyManager) context
            .getSystemService(Context.TELEPHONY_SERVICE);
    telephonyManager.listen(listener,
            PhoneStateListener.LISTEN_DATA_CONNECTION_STATE);

}

@Override
public void onDisabled(Context context) {
    telephonyManager.listen(listener, PhoneStateListener.LISTEN_NONE);
    super.onDisabled(context);
}
listener = new PhoneStateListener() {
        @Override
        public void onDataConnectionStateChanged(int state) {
            // removed the code
        }

    };

}
jainal
  • 2,973
  • 2
  • 20
  • 18
0

Just check telephonyManager to see if it is null.

if(telephonyManager==null){
    telephonyManager = (TelephonyManager) context
        .getSystemService(Context.TELEPHONY_SERVICE);
}
telephonyManager.listen(listener, PhoneStateListener.LISTEN_NONE);
Jett Hsieh
  • 3,159
  • 27
  • 33
  • If I make it like that it doesn't remove the listener. I am trying to figure out why is telephonyManager null. –  Aug 09 '11 at 11:53
0

I got same probem. Actually problem disapeared after I made all null fields static. Looks like it;s really another instance of provider who gets onDisabled.

Vadim
  • 1,582
  • 2
  • 15
  • 16