是android编程的新手,我发现将qr scan的结果代码(使用Zxing
)发送到我已经设置好的PHP文件时遇到了一些问题。扫描结果后,结果显示在警告对话框中,当我点击“继续”按钮时,应用程序崩溃。
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
contents = intent.getStringExtra("SCAN_RESULT");
String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
// Handle successful scan
//Toast toast = Toast.makeText(this, "Content:" + contents + " Format:" + format , Toast.LENGTH_LONG);
//toast.setGravity(Gravity.TOP, 25, 400);
//toast.show();
Context context=this;
AlertDialog.Builder builder1 = new AlertDialog.Builder(context);
builder1.setMessage("Id Number: "+contents+"\n"+"Format: "+format);
builder1.setCancelable(true);
builder1.setNegativeButton(
"Continue",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
checkLogin(contents);
}
//end
}
);
builder1.show();
}
}
else if (resultCode == RESULT_CANCELED) {
// Handle cancel
Toast toast = Toast.makeText(this, "Scan was Cancelled!", Toast.LENGTH_LONG);
toast.setGravity(Gravity.TOP, 25, 400);
toast.show();
new Thread(new Runnable() {
public void run() {
}
}).start();
}
else{
Toast toast = Toast.makeText(getApplicationContext(),
"No scan data received!", Toast.LENGTH_SHORT);
toast.show();
}
}
////////////////////////////////
public void checkLogin(final String contents) {
//start
String tag_string_req = "req_login";
pDialog.setMessage("Logging in ...");
showDialog();
StringRequest strReq = new StringRequest(Request.Method.POST,
REGISTER_URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
hideDialog();
try {
JSONObject jObj = new JSONObject(response);
String userId = jObj.getString(TAG);
if (userId != null) {
String successMsg = jObj.getString("success_msg");
Toast.makeText(getApplicationContext(),
successMsg, Toast.LENGTH_LONG).show();
} else {
String errorMsg = jObj.getString("error_msg");
Toast.makeText(getApplicationContext(),
errorMsg, Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_LONG).show();
hideDialog();
}
}) {
@Override
protected Map<String, String> getParams() {
// Post params to login url
Map<String, String> params = new HashMap<>();
params.put(TAG, contents);
return params;
}
};
// Adding request to queue
AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
}
private void showDialog() {
if (!pDialog.isShowing())
pDialog.show();
}
private void hideDialog() {
if (pDialog.isShowing())
pDialog.dismiss();
}