运行时错误应用Android

时间:2014-09-09 10:10:50

标签: java android

我在acer Z120智能手机上试用我的应用程序(例如Android 4.1.1),我遇到了这个运行时错误:

09-09 12:03:24.827: W/dalvikvm(6414): threadid=11: thread exiting with uncaught exception (group=0x419af908)
09-09 12:03:24.830: E/AndroidRuntime(6414): FATAL EXCEPTION: Thread-546
09-09 12:03:24.830: E/AndroidRuntime(6414): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
09-09 12:03:24.830: E/AndroidRuntime(6414):     at android.os.Handler.<init>(Handler.java:121)
09-09 12:03:24.830: E/AndroidRuntime(6414):     at android.app.Dialog.<init>(Dialog.java:107)
09-09 12:03:24.830: E/AndroidRuntime(6414):     at android.app.AlertDialog.<init>(AlertDialog.java:114)
09-09 12:03:24.830: E/AndroidRuntime(6414):     at android.app.AlertDialog$Builder.create(AlertDialog.java:913)
09-09 12:03:24.830: E/AndroidRuntime(6414):     at android.app.AlertDialog$Builder.show(AlertDialog.java:931)
09-09 12:03:24.830: E/AndroidRuntime(6414):     at aqua.example.aquasalt.MainActivity.setAlertMsg(MainActivity.java:753)
09-09 12:03:24.830: E/AndroidRuntime(6414):     at aqua.example.aquasalt.MainActivity$ConnectedThread.run(MainActivity.java:250)
09-09 12:03:24.931: W/MMUMapper(6414): fail to register MVA, unsupported format(0x5)

应用程序适用于我的智能手机(三星Galaxy S Advance)。 我可以吗?

2 个答案:

答案 0 :(得分:0)

您正在尝试在后台线程中显示对话框。只有主UI线程可能会显示对话框,否则会触摸UI。

要将Runnable发布到主UI线程处理程序,您可以在活动中使用runOnUiThread()

答案 1 :(得分:0)

这是我的代码:

public void connect() {
    // address is public variable  
    BluetoothDevice device = myBluetoothAdapter.getRemoteDevice(address);
    try {
        btSocket = createBluetoothSocket(device);
    } catch (IOException e1) {

    }
    myBluetoothAdapter.cancelDiscovery();

    // Establish the connection. This will block until it connects.
    try {
        btSocket.connect();
        connesso = true;
    } catch (IOException e) { //here I have exception
        try {
            btSocket.close();
            connesso = false;
        } catch (IOException e2) {}
    }
    if(connesso){
        mConnectedThread = new ConnectedThread(btSocket);
        mConnectedThread.start();
    }
}


private BluetoothSocket createBluetoothSocket(BluetoothDevice device) throws IOException {
    BluetoothSocket socket = null;
    if (Build.VERSION.SDK_INT >= 10) {
        try {
                socket = device.createInsecureRfcommSocketToServiceRecord(MY_UUID);
                Method m = device.getClass().getMethod("createInsecureRfcommSocket", new Class[] {int.class});
                socket = (BluetoothSocket) m.invoke(device, Integer.valueOf(1));


        } catch (Exception e) {
            System.out.println(e);
        }
    }
    return socket;

}