方法接受对象后在使用时发生空对象引用问题
有一个是内部类的重写某接口的方法,其中一段代码是HYKLDeviceCore sharedCore = HYKLDeviceCore.sharedCore();
HYKLDancingActivity hYKLDancingActivity2 = HYKLDancingActivity.this;
sharedCore.init(hYKLDancingActivity2, hYKLDancingActivity2.mMAC);
它的意思是先从HYKLDeviceCore创建一个sharedCore对象,再获取外部类HYKLDacingActivity的实例,最后调用sharedCore的init方法,并传递外部类实例及这个实例的mMAC。
这段代码前面有各种判断,我想跳过判断,直接执行这段代码,于是我在外部类的方法体里,写成这样
HYKLDeviceCore.sharedCore().init(this, this.mMAC);
接着看init方法,完整代码如下
public synchronized void init(Context context, String str) {
this.mIsQueryDevice = false;
this.stateListener = null;
this.commandListener = null;
this.mContext = context;
AnalyTools.setWaterCodeListener(this);
if (str.substring(0, 2).equalsIgnoreCase("00")) {
if (this.mbtService == null) {
BluetoothService sharedManager = BluetoothService.sharedManager();
this.mbtService = sharedManager;
sharedManager.setHandlerContext(this.mHandler);
}
} else if (this.mbtBLEService == null) {
EventBus.getDefault().register(this);
BleBluetoothService sharedManager2 = BleBluetoothService.sharedManager(context);
this.mbtBLEService = sharedManager2;
sharedManager2.setHandlerContext(this.mBLEHandler);
}
if (this.mHandlerThread == null) {
HandlerThread handlerThread = new HandlerThread("KLWriteThread");
this.mHandlerThread = handlerThread;
handlerThread.start();
this.mWriteHandler = new Handler(this.mHandlerThread.getLooper());
}
if (this.mMainWriteHandler == null) {
this.mMainWriteHandler = new Handler(context.getMainLooper());
}
IntentFilter intentFilter = new IntentFilter("android.bluetooth.adapter.action.STATE_CHANGED");
intentFilter.addAction("android.bluetooth.device.action.BOND_STATE_CHANGED");
this.mContext.registerReceiver(this.mStatusReceive, intentFilter);
但是init方法在执行时报错,logcat日志显示
07-03 17:27:05.708 12211 12211 E AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.aaaaaa.bbbbbbbbb/com.aaaaaa.bbbbbbbbb.activity.device.HYKLDancingActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Looper android.content.Context.getMainLooper()' on a null object reference
07-03 17:27:05.708 12211 12211 E AndroidRuntime: at com.aaaaaa.bbbbbbbbb.core.HYKLDeviceCore.init(HYKLDeviceCore.java:133)
错误就出在这行代码
if (this.mMainWriteHandler == null) {
this.mMainWriteHandler = new Handler(context.getMainLooper());
按传递逻辑来看,context就是此方法接收的HYKLDacingActivity对象,那么为什么这段代码在使用时报空对象引用错误呢?难道我写的调用init方法传递参数表达错了吗?
(再贴一遍我在HYKLDacingActivity外部类写的代码)
HYKLDeviceCore.sharedCore().init(this, this.mMAC);
附上sharedCore方法代码
private static volatile HYKLDeviceCore instance;
public static HYKLDeviceCore sharedCore() {
if (instance == null) {
synchronized (HYKLDeviceCore.class) {
if (instance == null) {
instance = new HYKLDeviceCore();
}
}
}
return instance;
}
我咋感觉是你传的this有问题呢。。。 就是这段代码HYKLDeviceCore.sharedCore().init(this, this.mMAC);这个this你确定不是空的么 你可以打断点看下 看错误提示貌似是context为空,所以导致context..getMainLooper()直接报空指针异常 Maxhaha 发表于 2024-7-11 07:04
我咋感觉是你传的this有问题呢。。。 就是这段代码HYKLDeviceCore.sharedCore(). ...
就是不知道我写的这句语法对不对,软件原本是在内部类里调用init方法,是先获取了外部类的实例对象,然后再把这个实例对象作为参数传递,我想直接在外部类调用init方法,不确定写this是否就传递了当前实例,我问了chatgpt,他说是可以的,但是执行报错了 du1263794094 发表于 2024-7-11 17:08
就是不知道我写的这句语法对不对,软件原本是在内部类里调用init方法,是先获取了外部类的实例对象,然后 ...
你可以试试把this 改成 HYKLDancingActivity.this debug对比一下前后两种写法传到context的是啥不就清楚了吗
页:
[1]