我正在開發一個需要用戶啓用藍牙的應用程序。爲此,一旦點擊了所需的按鈕,就會拋出啓用藍牙的意圖。我正在使用下面的代碼:IllegalStateException如果嘗試從onActivityResult顯示對話框
public static final int ENABLE_BT = 1;
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
BluetoothAdapter mAdapter = BluetoothAdapter
.getDefaultAdapter();
if (!mAdapter.isEnabled()) {
Intent enableBTIntent = new Intent(
BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
startActivityForResult(enableBTIntent, ENABLE_BT);
}
}
});
此外,如果用戶不啓用藍牙,我想要一個對話框顯示。這個類是如下:
public class MessageDialog extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage("Please enable Bluetooth to proceed")
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
// TODO Auto-generated method stub
}
});
return builder.create();
}
}
的onActivityResult()
方法如下:
protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {
if (requestCode == ENABLE_BT)
if (resultCode == Activity.RESULT_CANCELED) {
//button.setText("err");
DialogFragment dialog = new MessageDialog();
dialog.show(getSupportFragmentManager(), "Warning !");
}
}
的問題是,只要我不支持藍牙,應用程序終止與和IllegalStateException
。日誌相關的日誌:
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=0, data=null} to activity {com.iskh.btchat.start/com.iskh.btchat.start.FirstActivity}: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
我無法理解如何顯示Dialog
是非法的。我已經檢查過,只有當顯示Dialog
的代碼存在時,這個錯誤纔是。如果該代碼被註釋掉,那麼應用程序按預期運行。而且我還測試了Dialog
。其他地方按預期顯示。
謝謝。