如果我通过ZXing 2.0库执行了QR码扫描,我在启动片段时遇到了一些问题。
我有这个例外:
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent { act=com.google.zxing.client.android.SCAN flg=0x80000 (has extras) }} to activity {fraiss.yucrowd/fraiss.yucrowd.activities.ScanQrCode}: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState*
当我执行此代码时:
@Override
public void onCreate(Bundle bundle) {
// TODO Auto-generated method stub
super.onCreate(bundle);
setContentView(R.layout.scan_qr_code);
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 0);
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0)
{
if (resultCode == RESULT_OK)
{
String contents = intent.getStringExtra("SCAN_RESULT");
String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
// Handle successful scan
Log.d("MENU", "Scan result: " + contents + " as " + format);
// Load details for qr code
FragmentTransaction fragmentTransaction = getSupportFragmentManager()
.beginTransaction();
Fragment fragmentDetails = new FragmentDetails(
contents);
fragmentTransaction
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
fragmentTransaction.replace(R.id.fragment_main,
fragmentDetails);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
else if (resultCode == RESULT_CANCELED)
{
// Handle cancel
Log.d("MENU", "Scan canceled");
}
}
}
我想知道为什么会这样。是否无法在ActivityResult结束时启动Fragment-Transaction?
请事先提供帮助。
答案 0 :(得分:8)
可以在onActivityResult
之前调用onResume
,并且在活动至少调用onResume
之前无法添加新的片段。
因此,您需要存储onActivityResult
中的数据,在onResume
中检查并在那里执行片段更改。
答案 1 :(得分:2)
这基本上是因为您的活动暂停,onActivityResult
之后调用onSaveInstanceState
,因此消息:'onSaveInstanceState后无法执行此操作'。保存requestCode,resultCode和intent,并检查onResume
是否有任何可用的工作。