gpt4 book ai didi

android startActivityForResult 给出错误

转载 作者:行者123 更新时间:2023-11-29 00:44:37 26 4
gpt4 key购买 nike

我是 android 的新手,有人可以帮助我吗,以下是我尝试运行以扫描条形码的代码,但是每当我单击按钮时它都会出错,它说强制关闭我应该做什么请帮助我。

this.btnCheck = (Button) findViewById(R.id.btnsearch);
this.btnCheck.setOnClickListener(
new OnClickListener() {
public void onClick(View v)
{
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 0);
}
}
);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
String contents = data.getStringExtra("SCAN_RESULT");
Toast.makeText(this, "the scaned code is = "+ contents, Toast.LENGTH_SHORT).show();
// Handle successful scan
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
}
}
}

编辑日志:

这是日志猫现在开始:

Intent { act=com.google.zxing.client.android.SCAN (has extras) } from pid 359 thread exiting with uncaught exception (group=0x40015560) 

08-18 01:52:00.995: ERROR/AndroidRuntime(359): FATAL EXCEPTION: main

08-18 01:52:00.995: ERROR/AndroidRuntime(359): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.google.zxing.client.android.SCAN (has extras) }

08-18 01:52:00.995: ERROR/AndroidRuntime(359): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1408)

最佳答案

此异常可能意味着您尝试使用的设备没有安装 BarcodeScanner。您可以在应用中的某处使用此代码进行检查,并提示用户从市场安装它(如果他们还没有)。

      //Check for Barcode scanner, if not found put up an alert that allows user to install it.
PackageManager pm = getPackageManager();
try {
ApplicationInfo appInfo = pm.getApplicationInfo("com.google.zxing.client.android", 0);
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
new AlertDialog.Builder(this)
.setTitle("WARNING:")
.setMessage("You don't have Barcode Scanner installed. Please install it.")
.setCancelable(false)
.setNeutralButton("Install it now", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Uri uri = Uri.parse("market://search?q=pname:com.google.zxing.client.android");
startActivity(new Intent(Intent.ACTION_VIEW, uri));
}
})
.show();
}

编辑:更改此行:

intent.putExtra("SCAN_MODE", "QR_CODE_MODE");

为此:

intent.putExtra("SCAN_MODE", "PRODUCT_MODE");

我强烈建议你 go to this page通读它,然后点击代码示例下方的两个链接,查看它显示的所有代码。即使您不理解它,它也会让您了解 BarcodeScanner 应用程序通过 Intents 的功能。

关于android startActivityForResult 给出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7101377/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com