我是android的初学者...从qr扫描仪我扫描数据...代码正在运行 很好...如果我扫描像abc一样的字符串......我的应用程序正在崩溃...因为性格较少...如何避免崩溃...如果我扫描下面代码中的一些无效字符串...任何人都可以帮助我...
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 0) {
if (resultCode == Activity.RESULT_OK) {
String contents = data.getStringExtra("SCAN_RESULT");
String Id = contents.substring(0, 10),
password = contents.substring(10, 14);
// formatTxt.setText("FORMAT: " + contents);
// contentTxt.setText("CONTENT: " + format);
Toast toast = Toast.makeText(getActivity(), "" + contents, Toast.LENGTH_LONG);
toast.show();
setdetails(id.toString(), password.toString());
} else if (resultCode == Activity.RESULT_CANCELED) {
// Handle cancel
Toast toast = Toast.makeText(getActivity(), " Cancelled", Toast.LENGTH_SHORT);
toast.show();
}
} else
super.onActivityResult(requestCode, resultCode, data);
}
java.lang.RuntimeException:传递结果失败 ResultInfo {who = android:fragment:0,request = 0,result = -1,data = Intent {act = com.google.zxing.client.android.SCAN flg = 0x80000(有额外内容)}}到activity {com.application}: java.lang.StringIndexOutOfBoundsException:length = 7; regionStart = 0; regionLength = 10
at android.app.ActivityThread.deliverResults(ActivityThread.java:3720)
Caused by: java.lang.StringIndexOutOfBoundsException: length=7; regionStart=0; regionLength=10
答案 0 :(得分:1)
你需要确保字符串长度不小于14(在你的情况下) 它也不是空的 使用此代码:
//...some code ...
String contents = data.getStringExtra("SCAN_RESULT");
if(contents!=null && contents.length() >= 14){
String Id = contents.substring(0, 10),
password = contents.substring(10, 14);
// formatTxt.setText("FORMAT: " + contents);
// contentTxt.setText("CONTENT: " + format);
Toast toast = Toast.makeText(getActivity(), "" + contents, Toast.LENGTH_LONG);
toast.show();
setdetails(id.toString(), password.toString());
}else{
Toast toast = Toast.makeText(getActivity(), "Invalid Data", Toast.LENGTH_LONG);
toast.show();
}
答案 1 :(得分:0)
您能告诉错误日志中显示的错误,您正在使用哪种扫描程序。
至于zxing条码扫描器本身给出错误对话框"错误,无法解释QR码。"显示。