【问题标题】:Getting error android.view.InflateException: Binary XML file line #10: Error inflating class fragment获取错误 android.view.InflateException: Binary XML file line #10: Error inflating class fragment
【发布时间】:2017-06-07 06:30:15
【问题描述】:

我正在测试来自 github https://github.com/dm77/barcodescanner 的 zxing 条码扫描器。

我有片段 A、片段 ScanFragment 和片段 ProductDetail。 Fragment ScanFragment 具有 SimpleScannerFragment,其中包含条形码扫描仪的代码。我正在使用替换片段事务并将片段放入后台堆栈。

我从碎片往以下方向走。

Fragment A -> ScanFragment -> Fragment 产品详情 -> ScanFragment

然后,我按返回以同样的方式返回, ScanFragment -> Fragment 产品详情 -> 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)

Fragment 一个仅包含传递给 ScanFragment 的按钮。 Fragment 产品详细信息有打开 ScanFragment 以获取条形码扫描仪的按钮。

我在以下事情上需要帮助。 1.我这里使用了replace fragment transaction。但我不确定是否应该在这里使用添加或替换片段。在哪种情况下我们应该使用添加或替换? 2.我应该制作第二个片段“ScanFragment2”以避免这个错误吗?或者我可以重复使用相同的 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>

【问题讨论】:

  • 去掉这一行 "imageViewBack = (ImageView) view.findViewById(R.id.imageViewBack);"因为你已经在这里绑定了这个变量 @BindView(R.id.imageViewBack) ImageView imageViewBack;所以你不需要再次初始化它。
  • 为什么要写两次 imageViewBack.setOnClickListener()。同时删除 linearLayout_Skip = (LinearLayout) view.findViewById(R.id.linearLayout_skip);这一行已经在这里绑定了 @BindView(R.id.linearLayout_skip) LinearLayout linearLayout_Skip;
  • @andy,对不起兄弟..那些是复制粘贴错误。无论如何,留下那些 UI 错误,你能帮助提供我提出的这两个问题的观点吗?何时在片段事务上使用添加/替换?而且,如果我可以多次使用相同的 ScanFragment(包含条形码扫描仪代码),如上所示。谢谢

标签: android android-fragments zxing


【解决方案1】:

删除

android:layout_weight="1"

来自&lt;FrameLayout&gt;标签中的xml文件

【讨论】:

  • 该错误与 SimpleScannerFragment 有关。如果我删除了这个片段,那么就会出错......
猜你喜欢
  • 2014-06-18
  • 2017-09-13
  • 2013-10-07
  • 2015-05-01
  • 2015-01-13
  • 2013-09-10
  • 2015-04-15
  • 2014-06-24
  • 1970-01-01
相关资源
最近更新 更多