Reputation: 25
I am beginner to android...from qr scanner i am scanning data...code is working fine...suposse if i scan some string like abc...my app is crashing ...because of less character...how avoid crashing...if i scan some invalid string in below code...can anyone help me...
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: Failure delivering result ResultInfo{who=android:fragment:0, request=0, result=-1, data=Intent { act=com.google.zxing.client.android.SCAN flg=0x80000 (has extras) }} to 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
Upvotes: 0
Views: 288
Reputation: 95
Can you just tell what is the error showing in error log, which scanner you are using.
As of zxing barcode scanner itself gives error dialog "Error, Cannot interpret QR code." is displayed.
Upvotes: 0
Reputation: 6082
you need to make sure string length is not less than 14 (in your case) and it's not null too use this code:
//...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();
}
Upvotes: 1