Question

I want to use this lib: https://code.google.com/p/barcodefraglibv2/

But I can't figure out how to add BarcodeFragment in code. Here's a sample:

public class MainActivity extends Activity implements IScanResultHandler{
        BarcodeFragment fragment;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            fragment = new BarcodeFragment();
            FragmentTransaction ft = getFragmentManager().beginTransaction();
            ft.add(R.id.sample, fragment).commit();
            fragment.setScanResultHandler(this);

        }
    }

But I've got an error here:

ft.add(R.id.sample, fragment).commit();

The method add(int, Fragment) in the type FragmentTransaction is not applicable for the arguments (int, BarcodeFragment)

Thank you.

Was it helpful?

Solution

This is an import problem. Your Activity doesn't use the support library, so FragmentTransaction.add is expecting a Fragment of type android.app.Fragment whereas you are providing a BarcoreFragment which extends android.support.v4.app.Fragment;

You can fix that by changing your MainActivity and make it implement FragmentActivity (see doc here), then fixing your imports and making the following changes :

import android.app.FragmentTransaction;
import android.app.Activity;
MainActivity extends Activity
FragmentTransaction ft = getFragmentManager().beginTransaction();

becomes

import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.FragmentActivity;
MainActivity extends FragmentActivity
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top