片段类型不匹配

问题描述

我正在使用Zxing扫描片段上的QR代码的Android应用程序,为实现这一目标,我按照本教程获取了所需的结果。Link I Followed to Scan QR on Fragment

我用来在** QRCodeFragment **片段上扫描QR的代码

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,data)
    }
}

}

在主要活动中,我称片段为

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

在主片段中,显示出类型不匹配,我指出这是扩展了 Fragment()

的片段名称
 public Fragment() {
    initLifecycle();
}

错误图片的外观

Image

如果我更改此设置,它将在活动中显示错误,我从那里调用扫描QR片段

Error After Changing Fragment

解决方法

您的QRCodeFragment类正在扩展Fragment,这是什么? (选中文件顶部的import

我们有android.app.Fragmentandroidx.fragment.app.Fragment-根据您的方法选择适当的

内置android.app.Fragment当前已被弃用,您应该使用androidx版本,但是有些库仍然有(仍然)需要原始库的可能性

,

如果您的QRCodeFragment扩展了androidx.fragment.app.Fragment,则应使用IntentIntegrator.forSupportFragment

,

我认为这是由于您的进口

您需要将导入从android.support.v4.app.Fragment更改为android.app.Fragment,反之亦然

,

在onViewCreated 中,您首先要做的是,然后调用 super.onViewCreated

是指基本片段实例,并且由于您的代码位于 super 调用之前,因此假定您的 Fragment类型 Fragment ,而不是 QRCodeFragment 为了解决这个问题

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

执行此操作-

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