Vivek Mishra
Vivek Mishra

Reputation: 5705

TelephonyManager returns null in fragment

I am trying to get Device Id from Telephony Manager in my app but I am getting null pointer exception. I debugged it and TelephonyManger object is null when instantiated. I found few solutions regarding this but I have already done all that things. I have also given appropriate permission in manifest file.

Here is my code

TelephonyManager TM=(TelephonyManager)getActivity. getSystemService(android.cotent.Context.TELEPHONY_SERVICE);
id=TM.getDeviceId();

Permission in manifest

  <uses-permission android:name="android.permission.READ_PHONE_STATE" />

Upvotes: 1

Views: 1103

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317322

The javadoc for getSystemService() says that it may return null if it's not a supported system service. So you should always check for null in this case and deal with it accordingly.

Upvotes: 1

Related Questions