我是初學者android ...從qr掃描器我正在掃描數據...代碼正在工作 罰款suposse如果我掃描一些像abc字符串。 ..my應用程序崩潰......因爲更少的性格......如何避免崩潰...如果我掃描下面的代碼一些無效的字符串...誰能幫我...我的應用程序崩潰時,通過使用Qr掃描器掃描無效字符串
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 {co m.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
發表你的異常和堆棧跟蹤請 –
你能看到編輯的代碼嗎?我發佈了異常 – Kingraj
添加try catch並顯示消息給用戶關於無效數據 –