为什么我的应用程序在 "zxing" 应用程序 "Kotlin" 上启动 "OnPause" 后退出
Why my app quit after i launch "OnPause" on a "zxing" app "Kotlin"
我的应用程序上的 "Onpause" 方法有问题。
当我在 ScanView activity 中按下 "back button" 时,我的应用程序崩溃了。
这是我的 MainActivity 上的点击监听器。
btn!!.setOnClickListener {
val intent = Intent(this@MainActivity, ScanActivity::class.java)
//startActivity(intent)
startActivityForResult(intent,PICK_CONTACT_REQUEST)
}
这是我的 class 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 中打开 activity 结果
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)
你知道我的问题吗?
谢谢
问题是您的 onActivityResult 声明数据参数必须为非空。它实际上是空的。我会尝试更改您的签名以使其可为空。如果由于覆盖而不起作用,请确保您的其他活动始终设置结果意图,即使结果是没有答案。
你的onPause没问题,完全不相关。
可能是因为您没有进入 handleResult()
方法,所以您的 activity 结果未设置。只需从 onPause()
或 onBackPressed()
方法调用 setResult()
,然后 return 一些结果(例如 Activity.RESULT_CANCELED
),即使 return 没有任何结果。
谢谢大家
我只是加了一个“?”意图参数。
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()
}}
}
我的应用程序上的 "Onpause" 方法有问题。 当我在 ScanView activity 中按下 "back button" 时,我的应用程序崩溃了。
这是我的 MainActivity 上的点击监听器。
btn!!.setOnClickListener {
val intent = Intent(this@MainActivity, ScanActivity::class.java)
//startActivity(intent)
startActivityForResult(intent,PICK_CONTACT_REQUEST)
}
这是我的 class 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 中打开 activity 结果
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)
你知道我的问题吗?
谢谢
问题是您的 onActivityResult 声明数据参数必须为非空。它实际上是空的。我会尝试更改您的签名以使其可为空。如果由于覆盖而不起作用,请确保您的其他活动始终设置结果意图,即使结果是没有答案。
你的onPause没问题,完全不相关。
可能是因为您没有进入 handleResult()
方法,所以您的 activity 结果未设置。只需从 onPause()
或 onBackPressed()
方法调用 setResult()
,然后 return 一些结果(例如 Activity.RESULT_CANCELED
),即使 return 没有任何结果。
谢谢大家
我只是加了一个“?”意图参数。
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()
}}
}