我正在测试github https://github.com/dm77/barcodescanner的zxing条形码扫描器。
我有片段A,片段ScanFragment和片段ProductDetail。片段ScanFragment具有SimpleScannerFragment,其中包含条形码扫描程序的代码。我正在使用替换片段事务并将片段放在backstack中。
我按照以下方向离开碎片。
片段A - > ScanFragment - >片段产品细节 - > ScanFragment
然后,我按回去以同样的方式回来, ScanFragment - >片段产品细节 - > ScanFragment(此处出错)
Exception in MessageQueue callback: handleReceiveCallback
android.view.InflateException: Binary XML file line #10: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:719)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
at np.com.ddm1.myandroidtoolshelper.BarcodeTest.BarcodeScanner.ScanFragment.onCreateView(ScanFragment.java:56)
片段A仅包含传递给ScanFragment的按钮。 片段产品详细信息具有打开ScanFragment以获取条形码扫描仪的按钮。
我需要帮助来处理事情。 我在这里使用了替换片段事务。但我不确定我是否应该在这里使用添加或替换片段。在哪种情况下我们应该使用添加或替换? 2.我应该制作第二个片段" ScanFragment2"避免这个错误?或者我可以重复使用相同的ScanFragment。
ScanFragment:
public class ScanFragment extends Fragment {
private static final String TAG = "ScanFragment";
View view;
TextView textViewToolbarTitle;
ImageView imageViewBack;
LinearLayout linearLayout_Skip;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PrintLog.showTag(TAG,"== onCreate ==");
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
PrintLog.showTag(TAG,"== onViewCreated ==");
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
PrintLog.showTag(TAG,"--- onCreateView ---");
view = inflater.inflate(R.layout.bc_fragment_scan,container,false);
ButterKnife.setDebug(true);
ButterKnife.bind(this,view);
initializeUI();
onClick();
return view;
}
private void initializeUI(){
textViewToolbarTitle = (TextView) view.findViewById(R.id.textViewToolbarTitle);
imageViewBack = (ImageView) view.findViewById(R.id.imageViewBack);
textViewToolbarTitle.setText("Scan Fragment");
imageViewBack.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
getActivity().onBackPressed();
}
});
linearLayout_Skip = (LinearLayout) view.findViewById(R.id.linearLayout_skip);
}
private void onClick(){
linearLayout_Skip.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
releaseCamera();
ProductDetailFormFragment productDetailFormFragment = new ProductDetailFormFragment();
getActivity().getSupportFragmentManager().beginTransaction()
.replace(R.id.mainFragment, productDetailFormFragment).addToBackStack("scan_fragment").commit();
}
});
}
private void releaseCamera(){
SimpleScannerFragment simpleScannerFragment = (SimpleScannerFragment) getChildFragmentManager()
.findFragmentById(R.id.scanner_fragment);
simpleScannerFragment.releaseCamera();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
PrintLog.show("CALLED ON ACTIVITY RESULT");
}
}
SimpleScannerFragment:
public class SimpleScannerFragment extends
Fragment implements ZXingScannerView.ResultHandler {
private static final String TAG = "SimpleScannerFragment";
private ZXingScannerView mScannerView;
ScanListener scanListener;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mScannerView = new ZXingScannerView(getActivity());
PrintLog.showTag(TAG,"=== onCreateView() ==");
return mScannerView;
}
@Override
public void onResume() {
super.onResume();
PrintLog.showTag(TAG,"== onResume() ==");
mScannerView.setResultHandler(this);
mScannerView.startCamera();
}
@Override
public void handleResult(Result rawResult) {
PrintLog.showTag(TAG,"== handleResult() ==");
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
mScannerView.resumeCameraPreview(SimpleScannerFragment.this);
}
}, 2000);
//--- case for calling from different fragments
if(getArguments() != null){
if(getArguments().getString("parent_fragment").equals("product_details")){
getActivity().getSupportFragmentManager().popBackStack();
scanListener.onScanComplete(rawResult.getText());
}
}else{
ProductDetailFormFragment productDetailFormFragment = new ProductDetailFormFragment();
Bundle bundle = new Bundle();
bundle.putString("serial_number",rawResult.getText());
productDetailFormFragment.setArguments(bundle);
getActivity().getSupportFragmentManager().beginTransaction()
.add(R.id.mainFragment, productDetailFormFragment).addToBackStack("scan_fragment").commit();
}
}
public void releaseCamera(){
PrintLog.showTag(TAG,"== releaseCamera() ==");
mScannerView.stopCamera();
}
public interface ScanListener{
public void onScanComplete(String serialNumber);
}
@Override
public void onPause() {
super.onPause();
PrintLog.showTag(TAG,"== onPause() ==");
mScannerView.stopCamera();
}
@Override
public void onDestroyView() {
super.onDestroyView();
PrintLog.showTag(TAG,"== onDestroyView() ==");
}
}
ScanFragment布局:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:clickable="true"
>
<fragment android:name="np.com.ddm1.myandroidtoolshelper.BarcodeTest.BarcodeScanner.SimpleScannerFragment"
android:id="@+id/scanner_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
答案 0 :(得分:0)
删除
android:layout_weight="1"
来自<FrameLayout>
标记