为什么在“ zxing”应用程序“ Kotlin”上启动“ OnPause”后我的应用程序退出

时间:2018-12-05 14:20:12

标签: android kotlin zxing

我的应用程序上的“ Onpause”方法有问题。 当我处于ScanView活动中并按“后退按钮”时,我的应用程序崩溃。

这是我在MainActivity上的点击侦听器。

        btn!!.setOnClickListener {
        val intent = Intent(this@MainActivity, ScanActivity::class.java)
        //startActivity(intent)
        startActivityForResult(intent,PICK_CONTACT_REQUEST)
    }

这是我的课程ScanActivity

class ScanActivity : Activity(), ZXingScannerView.ResultHandler {


private var mScannerView: ZXingScannerView? = null

public override fun onCreate(state: Bundle?) {
    super.onCreate(state)
    mScannerView = ZXingScannerView(this)    // Programmatically initialize the scanner view
    setContentView(mScannerView)                // Set the scanner view as the content view
}

public override fun onResume() {
    super.onResume()
    mScannerView!!.setResultHandler(this) // Register ourselves as a handler for scan results.
    mScannerView!!.startCamera()          // Start camera on resume
}

public override fun onPause() {
    super.onPause()
    mScannerView!!.stopCamera()
}

override fun handleResult(p0: com.google.zxing.Result?) {

    val data = Intent()
    data.putExtra("code", p0!!.text)
    setResult(Activity.RESULT_OK, data)
    finish()
    mScannerView!!.resumeCameraPreview(this)
}

在我的MainActivity中,Onactivity结果

    public override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {
    if (requestCode == PICK_CONTACT_REQUEST) {
        if (resultCode == Activity.RESULT_OK) {
            val cb = data.getStringExtra("code")
            println(cb)
            tvresult.text = cb.toString()
        }}
}

最后是错误代码。

    java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=0, data=null} to activity {fr.tais.riocb/fr.tais.riocb.MainActivity}: java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter data
    at android.app.ActivityThread.deliverResults(ActivityThread.java:4268)
    at android.app.ActivityThread.handleSendResult(ActivityThread.java:4312)
    at android.app.ActivityThread.-wrap19(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1644)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6494)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
 Caused by: java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter data
    at fr.tais.riocb.MainActivity.onActivityResult(Unknown Source:2)
    at android.app.Activity.dispatchActivityResult(Activity.java:7276)
    at android.app.ActivityThread.deliverResults(ActivityThread.java:4264)
    at android.app.ActivityThread.handleSendResult(ActivityThread.java:4312) 
    at android.app.ActivityThread.-wrap19(Unknown Source:0) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1644) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:164) 
    at android.app.ActivityThread.main(ActivityThread.java:6494) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 

您对我的问题有想法吗?

谢谢

3 个答案:

答案 0 :(得分:1)

问题是您的onActivityResult声称data参数必须为非null。它实际上是空的。我会尝试更改您的签名以使其可为空。如果由于覆盖导致事实不起作用,请确保您的其他主动性始终设置结果意图,即使结果没有答案。

您的onPause没什么问题,完全无关。

答案 1 :(得分:0)

这可能是因为您没有输入handleResult()方法,所以未设置活动结果。只需从setResult()onPause()方法中调用onBackPressed(),然后返回一些结果(例如Activity.RESULT_CANCELED),即使没有返回值。

答案 2 :(得分:0)

谢谢

我必须加上“?”到意图参数。

    public override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    if (requestCode == PICK_CONTACT_REQUEST) {
        if (resultCode == Activity.RESULT_OK) {
            val cb = data?.getStringExtra("code")
            println(cb)
            tvresult.text = cb.toString()
        }}
}