我有2个类(1是基本类,第二个扩展了Thread类),我试图访问一个对象(类),该对象在我的线程类中初始化setText()
从另一个线程访问一个对象
public class TThread extends Thread{
Patcher pf;
public TThread(String string) {
setName(string);
start();
}
@Override
public void run() {
pf = new Patcher("Checking Serial key..."); //<=== Class initialized here in a separate thread
}
public void setText(String string) {
pf.setText(string); //<=== Trying to access patcher here, throws NullPointerException
}
}
这是我怎么称呼TThread
public void myCall(){
TThread tpf = new TThread("pf thread");
//some code later
try{
tpf.setText("blabla");
}
的pf.setText()
抛出NullPointerException
当我试图从另一个线程访问补丁。
如何才能到达该线程并从另一个班级或此班级访问修补程序?
在'run'方法中只有构造函数是非常不寻常的,你确定在调用'yourTThreadObject.start()'之后调用'setText()'*吗? – kajacx
@kajacx我确定我没有在'start()'之前调用'setText()'。我在一个类中初始化我的线程类,然后在10行后调用'setText()'。 –