尝试ZBarFragment,获取空指针异常

时间:2014-09-21 22:12:34

标签: android fragment zbar

以前我使用ZxingFragment作为我的项目并且它有效,但相机方向错误。所以现在我正在尝试Zbar。我正在使用ZbarFragment库。但问题是我得到Nullpointerexception并且从调试值看来,扫描器类的某些变量似乎为null。我做错了什么?

public class ScanFragment extends Fragment implements ZBarFragment.ResultListener {

    private static final int ZBAR_SCANNER_REQUEST = 0;
    private static final int ZBAR_QR_SCANNER_REQUEST = 1;
    private static final String TAG = "ZBarFragment";
    private ZBarScanner scanner;
    public ScanFragment(){}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View rootView = inflater.inflate(R.layout.fragment_scan, container, false);
        FragmentManager fragmentManager = getFragmentManager();
        ZBarFragment cameraFrag = new ZBarFragment();
        fragmentManager.beginTransaction()
                .replace(R.id.sample, (Fragment)cameraFrag).commit();
        scanner = cameraFrag.getScanner();

        int[] modes = new int[] {ZBarConstants.CODE128, ZBarConstants.QRCODE};
        scanner.setModes(modes);
        return rootView;
    }



    @Override
    public void onResult(String result) {
        Toast.makeText(getActivity(), result, Toast.LENGTH_LONG).show();

    }
}

错误日志:

  

09-22 00:10:14.538:E / AndroidRuntime(27267):致命异常:主要   09-22 00:10:14.538:E / AndroidRuntime(27267):   java.lang.NullPointerException 09-22 00:10:14.538:   E / AndroidRuntime(27267):at   com.trhura.android.zbar.scanner.ZBarFragment.setModes(ZBarFragment.java:105)   09-22 00:10:14.538:E / AndroidRuntime(27267):at   com.example.devicerentalsystem.ScanFragment.onCreateView(ScanFragment.java:41)   09-22 00:10:14.538:E / AndroidRuntime(27267):at   android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)   09-22 00:10:14.538:E / AndroidRuntime(27267):at   android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)   09-22 00:10:14.538:E / AndroidRuntime(27267):at   android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)   09-22 00:10:14.538:E / AndroidRuntime(27267):at   android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)   09-22 00:10:14.538:E / AndroidRuntime(27267):at   android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)   09-22 00:10:14.538:E / AndroidRuntime(27267):at   android.support.v4.app.FragmentManagerImpl $ 1.run(FragmentManager.java:440)   09-22 00:10:14.538:E / AndroidRuntime(27267):at   android.os.Handler.handleCallback(Handler.java:730)09-22   00:10:14.538:E / AndroidRuntime(27267):at   android.os.Handler.dispatchMessage(Handler.java:92)09-22   00:10:14.538:E / AndroidRuntime(27267):at   android.os.Looper.loop(Looper.java:137)09-22 00:10:14.538:   E / AndroidRuntime(27267):at   android.app.ActivityThread.main(ActivityThread.java:5103)09-22   00:10:14.538:E / AndroidRuntime(27267):at   java.lang.reflect.Method.invokeNative(Native Method)09-22   00:10:14.538:E / AndroidRuntime(27267):at   java.lang.reflect.Method.invoke(Method.java:525)09-22 00:10:14.538:   E / AndroidRuntime(27267):at   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:737)   09-22 00:10:14.538:E / AndroidRuntime(27267):at   com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)09-22   00:10:14.538:E / AndroidRuntime(27267):at   dalvik.system.NativeStart.main(原生方法)

1 个答案:

答案 0 :(得分:0)

试试看:

FragmentManager fragmentManager = getActivity()。getSupportFragmentManager();

而不是

FragmentManager fragmentManager = getFragmentManager();