gpt4 book ai didi

java - 在 ViewRootImpl.java 中创建线程

转载 作者:太空宇宙 更新时间:2023-11-04 11:49:06 26 4
gpt4 key购买 nike

我试图在 ViewRootImpl.java 中创建一个带有循环器和处理程序的线程,但收到此错误:

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函数中,只是在线程之间发送消息。这是我的代码:

类(class):

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 框架是如何工作的?

谢谢

最佳答案

mVthread.mhandlernull

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

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

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

关于java - 在 ViewRootImpl.java 中创建线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42027264/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com