片段类型不匹配
Type Mismatched on Fragment
我正在开发 Android 应用程序,它使用 Zxing 扫描片段上的二维码,为了实现这一点,我按照本教程获得了想要的结果。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, resultCode, data)
}
}
}
来自 Main Activity 我正在调用 Fragment 就像
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();
}
错误图像外观
如果我更改它它会在 Activity 上显示错误,从我调用扫描 QR 片段的位置
我认为是因为你的进口
您需要将导入从 android.support.v4.app.Fragment
更改为 android.app.Fragment
,反之亦然
您的 QRCodeFragment
class 正在扩展 Fragment
,哪个? (检查文件顶部的 import
s)
我们有 android.app.Fragment and androidx.fragment.app.Fragment - 选择您的方法所需的适当方法
built-in android.app.Fragment
目前已弃用,您应该使用 androidx
版本,但有可能某些库仍然需要原始版本(遗憾)
在 onViewCreated 中,你先做你的事情然后调用 super.onViewCreated.
this 指的是 Base Fragment 实例 并且您的代码在 super[=29= 之前] 调用,它假定您的 Fragment type 是 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
}
如果你的 QRCodeFragment
扩展 androidx.fragment.app.Fragment
那么你应该使用 IntentIntegrator.forSupportFragment
我正在开发 Android 应用程序,它使用 Zxing 扫描片段上的二维码,为了实现这一点,我按照本教程获得了想要的结果。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, resultCode, data)
}
}
}
来自 Main Activity 我正在调用 Fragment 就像
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();
}
错误图像外观
如果我更改它它会在 Activity 上显示错误,从我调用扫描 QR 片段的位置
我认为是因为你的进口
您需要将导入从 android.support.v4.app.Fragment
更改为 android.app.Fragment
,反之亦然
您的 QRCodeFragment
class 正在扩展 Fragment
,哪个? (检查文件顶部的 import
s)
我们有 android.app.Fragment and androidx.fragment.app.Fragment - 选择您的方法所需的适当方法
built-in android.app.Fragment
目前已弃用,您应该使用 androidx
版本,但有可能某些库仍然需要原始版本(遗憾)
在 onViewCreated 中,你先做你的事情然后调用 super.onViewCreated.
this 指的是 Base Fragment 实例 并且您的代码在 super[=29= 之前] 调用,它假定您的 Fragment type 是 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
}
如果你的 QRCodeFragment
扩展 androidx.fragment.app.Fragment
那么你应该使用 IntentIntegrator.forSupportFragment