有一个是内部类的重写某接口的方法,其中一段代码是
[Java] 纯文本查看 复制代码
HYKLDeviceCore sharedCore = HYKLDeviceCore.sharedCore();
HYKLDancingActivity hYKLDancingActivity2 = HYKLDancingActivity.this;
sharedCore.init(hYKLDancingActivity2, hYKLDancingActivity2.mMAC);
它的意思是先从HYKLDeviceCore创建一个sharedCore对象,再获取外部类HYKLDacingActivity的实例,最后调用sharedCore的init方法,并传递外部类实例及这个实例的mMAC。
这段代码前面有各种判断,我想跳过判断,直接执行这段代码,于是我在外部类的方法体里,写成这样
[Java] 纯文本查看 复制代码
HYKLDeviceCore.sharedCore().init(this, this.mMAC);
接着看init方法,完整代码如下
[Java] 纯文本查看 复制代码
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日志显示
[Java] 纯文本查看 复制代码
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)
错误就出在这行代码
[Java] 纯文本查看 复制代码
if (this.mMainWriteHandler == null) {
this.mMainWriteHandler = new Handler(context.getMainLooper());
按传递逻辑来看,context就是此方法接收的HYKLDacingActivity对象,那么为什么这段代码在使用时报空对象引用错误呢?难道我写的调用init方法传递参数表达错了吗?
(再贴一遍我在HYKLDacingActivity外部类写的代码)
[Java] 纯文本查看 复制代码
HYKLDeviceCore.sharedCore().init(this, this.mMAC);
附上sharedCore方法代码
[Java] 纯文本查看 复制代码
[color=#0000ff][b]private[/b][/color][color=#000000] [/color][color=#0000ff][b]static[/b][/color][color=#000000] [/color][color=#0000ff][b]volatile[/b][/color][color=#000000] [/color][color=#000000]HYKLDeviceCore[/color][color=#000000] [/color][color=#000000]instance[/color][color=#000000];[/color]
public static HYKLDeviceCore sharedCore() {
if (instance == null) {
synchronized (HYKLDeviceCore.class) {
if (instance == null) {
instance = new HYKLDeviceCore();
}
}
}
return instance;
}
|