Thursday, August 7, 2014

NullPointerException when new PhoneStateListener

When I new PhoneStateListener() I am getting this error

java.lang.NullPointerException
  at android.os.Handler.<init>(Handler.java:229)
  at android.os.Handler.<init>(Handler.java:137)
  at android.telephony.PhoneStateListener$2.<init>(PhoneStateListener.java:420)
  at android.telephony.PhoneStateListener.<init>(PhoneStateListener.java:420)

I looked at the Handler.java:. I believe the reason for this error is mQueue = looper.mQueue; so looper is null.  Only way to fix this problem is to call Looper.prepareMainLooper(); at the beginning of the process

1 comment:

  1. I have the exact same problem. Were do you exactly calls the prepareMainLooper()-method? I tried to call it before instantiating the PhoneStateListener, but I am getting an exception, claiming that only one Looper can exist per thread.

    ReplyDelete