Usman Ali
Usman Ali

Reputation: 433

Type Mismatched on Fragment

I am working on Android app which uses Zxing to Scan QR Code on fragment, For achieving that I followed this Tutorial to get the Desired result.Link I Followed to Scan QR on Fragment

The Code I used to Scan QR On **QRCodeFragment ** Fragment looks

class QRCodeFragment : Fragment() {

internal var txtName: TextView? = null
internal var txtSiteName: TextView? = null
internal var btnScan: Button? = null
internal var qrScanIntegrator: IntentIntegrator? = null


override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    return inflater?.inflate(R.layout.fragment_qr_code, container, false)
}


override fun onViewCreated(view: View, savedInstanceState: Bundle?) {

    txtName = view.findViewById(R.id.name)
    txtSiteName = view.findViewById(R.id.site_name)

    btnScan = view.findViewById(R.id.btnScan)
    btnScan!!.setOnClickListener { performAction() }

    qrScanIntegrator = IntentIntegrator.forFragment(this)
    qrScanIntegrator?.setOrientationLocked(false)

    // Different Customization option...
    qrScanIntegrator?.setPrompt(getString(R.string.scan_a_code))
    qrScanIntegrator?.setCameraId(0)  // Use a specific camera of the device
    qrScanIntegrator?.setBeepEnabled(false)
    qrScanIntegrator?.setBarcodeImageEnabled(true)

    super.onViewCreated(view, savedInstanceState)
}

private fun performAction() {
    qrScanIntegrator?.initiateScan()
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    val result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data)
    if (result != null) {
        // If QRCode has no data.
        if (result.contents == null) {
            Toast.makeText(activity, R.string.result_not_found, Toast.LENGTH_LONG).show()
        } else {
            // If QRCode contains data.
            try {
                // Converting the data to json format
                val obj = JSONObject(result.contents)
                // Show values in UI.
                txtName?.text = obj.getString("name")
                txtSiteName?.text = obj.getString("site_name")

            } catch (e: JSONException) {
                e.printStackTrace()

                // Data not in the expected format. So, whole object as toast message.
                Toast.makeText(activity, result.contents, Toast.LENGTH_LONG).show()
            }

        }
    } else {
        super.onActivityResult(requestCode, resultCode, data)
    }
}

}

From Main Activity I am Calling the the Fragment is like

    private fun ShowQRCodeFragment() {
    val newFragment = QRCodeFragment()
    val transaction1: FragmentTransaction = supportFragmentManager.beginTransaction();
    transaction1.replace(R.id.frameLayout, newFragment);
    transaction1.addToBackStack(null);
    transaction1.commit();
}

In the Main Fragment It shows me Type mismatched, I pointed on it it goes to Fragment name that is Extending Fragment()

 public Fragment() {
    initLifecycle();
}

Error Image Looks Image

if I change this It shows the Error on the Activity, From Where I am calling the Scan QR Fragment Error After Changing Fragment

Upvotes: 0

Views: 211

Answers (4)

Debarshi Bhattacharjee
Debarshi Bhattacharjee

Reputation: 830

In onViewCreated you're first doing your stuff and then calling super.onViewCreated.

this is referring to the Base Fragment instance and as your code goes before the super call, it assumes your Fragment type is Fragment and not QRCodeFragment To solve this

First call super.onViewCreated in onViewCreated and then do your code part

Do this-

override fun onViewCreated(view:View, savedInstanceState:Bundle? ) 
{
 super.onViewCreated(view, savedInstanceState) 
//your code part
} 

Upvotes: 0

IR42
IR42

Reputation: 9682

If your QRCodeFragment extends androidx.fragment.app.Fragment then you should use IntentIntegrator.forSupportFragment

Upvotes: 1

snachmsm
snachmsm

Reputation: 19233

your QRCodeFragment class is extending Fragment, which? (check imports on top of a file)

we have android.app.Fragment and androidx.fragment.app.Fragment - pick proper needed by your method

built-in android.app.Fragment is currently deprecated, you should use androidx version, but there is a chance that some libraries still needs original one (sadly)

Upvotes: 1

Evyatar Cohen
Evyatar Cohen

Reputation: 342

I think it is because of your imports

you need to change your import from android.support.v4.app.Fragment to android.app.Fragment or vice versa

Upvotes: 0

Related Questions