當我嘗試打印到藍牙打印機時,出現主線程上的很多活動錯誤。我試圖改變它使用一個線程,但然後我的AlterDialog不起作用。 是否有另一種方法可以使其顯示AlterDialog? 謝謝。AlertDialog無法在線程中運行Android
public class PrinterFunctions{
private static void sendCommand(final Context context,final String portName, final String portSettings, final ArrayList<Byte> byteList) {
new Thread() {
@Override
public void run() {
StarIOPort port = null;
try {
port = StarIOPort.getPort(portName, portSettings, 10000, context);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
}
StarPrinterStatus status = port.beginCheckedBlock();
if (true == status.offline) {
throw new StarIOPortException("A printer is offline");
}
byte[] commandToSendToPrinter = convertFromListByteArrayTobyteArray(byteList);
port.writePort(commandToSendToPrinter, 0,
commandToSendToPrinter.length);
port.setEndCheckedBlockTimeoutMillis(30000);
status = port.endCheckedBlock();
if (true == status.coverOpen) {
throw new StarIOPortException("Printer cover is open");
} else if (true == status.receiptPaperEmpty) {
throw new StarIOPortException("Receipt paper is empty");
} else if (true == status.offline) {
throw new StarIOPortException("Printer is offline");
}
} catch (StarIOPortException e) {
Builder dialog = new AlertDialog.Builder(context);
dialog.setNegativeButton("Ok", null);
AlertDialog alert = dialog.create();
alert.setTitle("Failure");
alert.setMessage(e.getMessage());
alert.setCancelable(false);
alert.show();
} finally {
if (port != null) {
try {
StarIOPort.releasePort(port);
} catch (StarIOPortException e) {
}
}
}
}
}
}.start();
}
}
我會添加這個een catch(StarIOPortException e){最後在我上面的代碼中? – 2014-10-14 21:15:22
以上是不能正常工作的應用程序崩潰。 – 2014-10-23 21:04:31