1

我正在尝试在 ViewRootImpl.java 中创建一个带有 looper 和处理程序的线程,但我收到了这个错误:

02-03 14:04:14.266   884   884 E AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.os.Handler.sendMessage(android.os.Message)' on a null object reference
02-03 14:04:14.266   884   884 E AndroidRuntime:    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1344)
02-03 14:04:14.266   884   884 E AndroidRuntime:    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1150)
02-03 14:04:14.266   884   884 E AndroidRuntime:    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6076)
02-03 14:04:14.266   884   884 E AndroidRuntime:    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
02-03 14:04:14.266   884   884 E AndroidRuntime:    at android.view.Choreographer.doCallbacks(Choreographer.java:670)
02-03 14:04:14.266   884   884 E AndroidRuntime:    at android.view.Choreographer.doFrame(Choreographer.java:606)
02-03 14:04:14.266   884   884 E AndroidRuntime:    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
02-03 14:04:14.266   884   884 E AndroidRuntime:    at android.os.Handler.handleCallback(Handler.java:739)
02-03 14:04:14.266   884   884 E AndroidRuntime:    at android.os.Handler.dispatchMessage(Handler.java:95)
02-03 14:04:14.266   884   884 E AndroidRuntime:    at android.os.Looper.loop(Looper.java:148)
02-03 14:04:14.266   884   884 E AndroidRuntime:    at com.android.server.SystemServer.run(SystemServer.java:283)
02-03 14:04:14.266   884   884 E AndroidRuntime:    at com.android.server.SystemServer.main(SystemServer.java:168)
02-03 14:04:14.266   884   884 E AndroidRuntime:    at java.lang.reflect.Method.invoke(Native Method)
02-03 14:04:14.266   884   884 E AndroidRuntime:    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
02-03 14:04:14.266   884   884 E AndroidRuntime:    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
02-03 14:04:14.348   884   884 I Process : Sending signal. PID: 884 SIG: 9

我的代码在 performTraversals 函数中,只是在线程之间发送消息。这是我的代码:

课程:

public class Viewhandler extends Handler{

    Viewhandler(){}
    @Override
    public void handleMessage(Message msg) {
        Log.v("Viewhandler","msg-" + msg.arg1);

    }

}

public class Vthread extends Thread{

    Viewhandler mViewhandler;
    Handler mhandler;

    Vthread(Viewhandler handler){
        mViewhandler = handler;
    }

    @Override
    public void run(){
        Looper.prepare();
        mhandler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                Log.v("Vthread","msg-" + msg.arg1);
                Message mes = Message.obtain();
                mes.arg1 = 2000;
                mViewhandler.sendMessage(mes);
            }
        };
       Looper.loop();
    }
}

performTraversals 函数中的代码:

if(mViewhandler == null){
    Log.v(TAG, "viewhandler");
    mViewhandler = new Viewhandler();
}
if(mVthread == null){
    Log.v(TAG, "vthread");
    mVthread = new Vthread(mViewhandler);
    mVthread.start();
}

Log.v(TAG, "message");
Message msg = Message.obtain();
msg.arg1 = 1000;
mVthread.mhandler.sendMessage(msg);

我不能在这个文件中创建线程吗?

另一个问题:每个应用程序都有自己的框架副本吗?这对 android 框架有什么作用?

谢谢

4

1 回答 1

0

mVthread.mhandlernull

在没有适当同步的情况下,您不能依赖run()调用后内部发生的事情。start()

当您阅读时,很可能该线程尚未启动mhandler

移动mhandler = new Handler()...Vthread构造函数。

于 2017-02-03T15:15:03.797 回答