简体   繁体   中英

Android Looper thread crashes with NullPointerException

I'm trying to get a looper thread to work but despite all attempts, it crashes. I'm not using the HandlerThread class, as I'm doing quite a bit of 802.11 related stuff in the thread itself and posting Runnables with lots of duplicate code doesn't seem to be the right way to go here.

Here the skeleton of the looper thread class:

public class WiFiScanner extends Thread {
    Looper mLooper;
    Handler mHandler;

    @Override
    public void run() {
        Looper.prepare();
        synchronized(this) {
            mLooper = Looper.myLooper();
            notifyAll();
        }

        mHandler = new Handler() {

            @Override
            public void handleMessage(Message msg) {
                ..... // message parsing
            }
        }
    }
    /* waits for Looper initialization */
    public boolean waitForLooper() {
        synchronized(this) {
            while(mLooper == null) {
                try {
                    wait();
                } catch (InterruptedException e) {}
            }
        }
        return true;
    }
}

and the code initializing it in the main activity:

wifiScanner = new WiFiScanner(
    ... // callback stuff
);
wifiScanner.start();
wifiScanner.waitForLooper();
wifiScanner.initialize();

initialize is a simple function in the WiFiScanner class, posting a message bundle to the message queue:

public void initialize() {
    Message msg = mHandler.obtainMessage();
    Bundle b = new Bundle();
    WiFiMsg msgId = WiFiMsg.INITIALIZE;
    b.putSerializable("msgId", msgId);
    msg.obj = b;
    mHandler.sendMessage(msg);
}

Despite waitForLooper() returning successfully, the call to initialize() causes a null pointer exception. I assume the looper isn't really in a state to have message dispatched into the queue as a static delay of 500ms between waitForLooper() and initialize() circumvents this issue.

Any ideas on how to fix this in a reasonably elegant way?

PS: Now by popular demand, a stack trace:

E/AndroidRuntime( 3178):        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
E/AndroidRuntime( 3178):        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
E/AndroidRuntime( 3178):        at android.app.ActivityThread.access$600(ActivityThread.java:141)
E/AndroidRuntime( 3178):        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
E/AndroidRuntime( 3178):        at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 3178):        at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime( 3178):        at android.app.ActivityThread.main(ActivityThread.java:5103)
E/AndroidRuntime( 3178):        at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 3178):        at java.lang.reflect.Method.invoke(Method.java:525)
E/AndroidRuntime( 3178):        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
E/AndroidRuntime( 3178):        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
E/AndroidRuntime( 3178):        at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 3178): Caused by: java.lang.NullPointerException
E/AndroidRuntime( 3178):        at de.uni_leipzig.informatik.rvs.videodownload.wifi.WiFiScanner.initialize(WiFiScanner.java:204)
E/AndroidRuntime( 3178):        at de.uni_leipzig.informatik.rvs.videodownload.MainActivity.onCreate(MainActivity.java:270)
E/AndroidRuntime( 3178):        at android.app.Activity.performCreate(Activity.java:5133)
E/AndroidRuntime( 3178):        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
E/AndroidRuntime( 3178):        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
E/AndroidRuntime( 3178):        ... 11 more

您的通知应该在mHandler设置之后,而不是mLooper - 因为您的NPE在mHandler上。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM