我是Android开发的初学者,我收到一个错误,几天前用doInBackground()
方法无法解决。
我的错误发生在我的代码末尾,
e2 = e11;
createInsecureRfcommSocketToServiceRecord = null;
this.sender = e2.getMessage();
createInsecureRfcommSocketToServiceRecord.close();
错误是
未处理的异常:java.io.IO.EXCEPTION
如果您有解决方案,请提前感谢! 我的代码:
protected Object doInBackground(Object[] objArr) {
BluetoothSocket createInsecureRfcommSocketToServiceRecord;
IOException e;
Throwable th;
IButton a;
Boolean valueOf;
boolean z = true;
this.jobType = (JobType) objArr[0];
this.sender = (MainActivity) objArr[1];
this.device = (BluetoothDevice) objArr[2];
try {
createInsecureRfcommSocketToServiceRecord =
device.createInsecureRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
try {
createInsecureRfcommSocketToServiceRecord.connect();
} catch (IOException e2) {
Log.e("Bluetooth", e2.getMessage());
Log.e("Bluetooth", "trying fallback");
try {
createInsecureRfcommSocketToServiceRecord = (BluetoothSocket)
this.device.getClass().getMethod("createInsecureRfcommSocket", new Class[]{Integer.TYPE}).invoke(this.device, new Object[]{Integer.valueOf(1)});
} catch (Exception e3) {
Log.e("Bluetooth", e3.getMessage());
}
try {
createInsecureRfcommSocketToServiceRecord.connect();
} catch (IOException e4) {
e2 = e4;
try {
this.error = e2.getMessage();
try {
createInsecureRfcommSocketToServiceRecord.close();
} catch (IOException e5) {
}
return null;
} catch (Throwable th2) {
th = th2;
try {
createInsecureRfcommSocketToServiceRecord.close();
} catch (IOException e6) {
}
throw th;
}
}
}
if (this.jobType == JobType.read) {
a = readDevice(createInsecureRfcommSocketToServiceRecord);
try {
createInsecureRfcommSocketToServiceRecord.close();
return a;
} catch (IOException e7) {
return a;
}
} else
if (this.jobType == JobType.stopMission) {
a = (IButton) objArr[3];
if (!(stopMission(createInsecureRfcommSocketToServiceRecord) &&
writeDevice(createInsecureRfcommSocketToServiceRecord, a))) {
z = false;
}
valueOf = Boolean.valueOf(z);
try {
createInsecureRfcommSocketToServiceRecord.close();
return valueOf;
} catch (IOException e8) {
return valueOf;
}
} else
if (this.jobType == JobType.startMission) {
boolean z2 = writeDevice(createInsecureRfcommSocketToServiceRecord, (IButton) objArr[3]) &&
startMission(createInsecureRfcommSocketToServiceRecord);
valueOf = Boolean.valueOf(z2);
try {
createInsecureRfcommSocketToServiceRecord.close();
return valueOf;
} catch (IOException e9) {
return valueOf;
}
} else {
try {
createInsecureRfcommSocketToServiceRecord.close();
} catch (IOException e10) {
}
return null;
}
} catch (IOException e11) {
e2 = e11;
createInsecureRfcommSocketToServiceRecord = null;
this.sender = e2.getMessage();
createInsecureRfcommSocketToServiceRecord.close();
return null;
} catch (Throwable th3) {
th = th3;
createInsecureRfcommSocketToServiceRecord = null;
createInsecureRfcommSocketToServiceRecord.close();
throw th;
}
return null;
}
答案 0 :(得分:0)
close()
也会抛出您需要处理的IOException
异常。此外,您应该使用finally
块进行清理,并注意不要在关闭之前将变量设置为NullPointerExeption
来激发null
。
您可以改善代码,例如
try {
....
} catch (Exception e) {
....
} finally {
try {
createInsecureRfcommSocketToServiceRecord.close();
createInsecureRfcommSocketToServiceRecord = null;
} catch (IOException e) {
}
}
答案 1 :(得分:0)
我用这个修改我的代码,但我是java.lang.NullPointerException ... 功能doInBackground()很难...... 感谢您的帮助!
我的代码修改
protected Object doInBackground(Object[] objArr) {
BluetoothSocket createInsecureRfcommSocketToServiceRecord = null;
IOException e;
Throwable th;
IButton a;
Boolean valueOf;
boolean z = true;
this.jobType = (JobType) objArr[0];
this.sender = (MainActivity) objArr[1];
this.device = (BluetoothDevice) objArr[2];
try {
createInsecureRfcommSocketToServiceRecord =
device.createInsecureRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
try {
createInsecureRfcommSocketToServiceRecord.connect();
} catch (IOException e2) {
Log.e("Bluetooth", e2.getMessage());
Log.e("Bluetooth", "trying fallback");
try {
createInsecureRfcommSocketToServiceRecord = (BluetoothSocket)
this.device.getClass().getMethod("createInsecureRfcommSocket", new Class[]{Integer.TYPE}).invoke(this.device, new Object[]{Integer.valueOf(1)});
} catch (Exception e3) {
Log.e("Bluetooth", e3.getMessage());
}
try {
createInsecureRfcommSocketToServiceRecord.connect();
} catch (IOException e4) {
e2 = e4;
try {
this.error = e2.getMessage();
try {
createInsecureRfcommSocketToServiceRecord.close();
} catch (IOException e5) {
}
return null;
} catch (Throwable th2) {
th = th2;
try {
createInsecureRfcommSocketToServiceRecord.close();
} catch (IOException e6) {
}
throw th;
}
}
}
if (this.jobType == JobType.read) {
a = readDevice(createInsecureRfcommSocketToServiceRecord);
try {
createInsecureRfcommSocketToServiceRecord.close();
return a;
} catch (IOException e7) {
return a;
}
} else if (this.jobType == JobType.stopMission) {
a = (IButton) objArr[3];
if (!(stopMission(createInsecureRfcommSocketToServiceRecord) &&
writeDevice(createInsecureRfcommSocketToServiceRecord, a))) {
z = false;
}
valueOf = Boolean.valueOf(z);
try {
createInsecureRfcommSocketToServiceRecord.close();
return valueOf;
} catch (IOException e8) {
return valueOf;
}
} else if (this.jobType == JobType.startMission) {
boolean z2 = writeDevice(createInsecureRfcommSocketToServiceRecord, (IButton) objArr[3]) &&
startMission(createInsecureRfcommSocketToServiceRecord);
valueOf = Boolean.valueOf(z2);
try {
createInsecureRfcommSocketToServiceRecord.close();
return valueOf;
} catch (IOException e9) {
return valueOf;
}
} else {
try {
createInsecureRfcommSocketToServiceRecord.close();
} catch (IOException e10) {
}
return null;
}
} catch (IOException e11) {
try {
createInsecureRfcommSocketToServiceRecord.close();
} catch (IOException e1) {
e1.printStackTrace();
}
createInsecureRfcommSocketToServiceRecord = null;
return null;
} catch (Throwable th3) {
th = th3;
} finally {
try {
createInsecureRfcommSocketToServiceRecord.close();
createInsecureRfcommSocketToServiceRecord = null;
} catch (IOException e2) {
}
}
return null;
}