Android 截击崩溃
Android Volley Crashing
我正在使用 Android Volley 登录用户,但是当调用 addToRequestQueue 时,会显示以下崩溃报告。
在Loginactivity中onclick进行登录请求如下:
// Login button Click Event
btnLogin.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String username = inputUserName.getText().toString();
String pin = inputPIN.getText().toString();
// Check for empty data in the form
if (username.trim().length() > 0 && pin.trim().length() > 0) {
// login user
SharedPreferences prefs = getGCMPreferences(context);
String storeRegId = prefs.getString(PROPERTY_REG_ID, "");
String devid = Secure.getString(getBaseContext().getContentResolver(),Secure.ANDROID_ID);
String vars = "/?tag=login&username=" + username + "&pin=" + pin + "®id=" + storeRegId + "&devid=" + devid;
WebRequest wr = new WebRequest(context);
wr.Request(AppConfig.URL_LOGIN, vars, true, "Please Wait", "Logging in...", "req_login");
} else {
// Prompt user to enter credentials
Toast.makeText(getApplicationContext(), "Please enter the credentials!", Toast.LENGTH_LONG).show();
}
}
});
使用 volley 的 WebRequest 如下所示:
package app;
import helper.SQLiteHandler;
import helper.SessionManager;
import helper.dbTables;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import main.MainActivity;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.WindowManager;
import android.widget.Toast;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.Request.Method;
import com.android.volley.toolbox.StringRequest;
import com.google.android.gms.gcm.GoogleCloudMessaging;
public class WebRequest extends Activity {
private static final String TAG = "LoginActivity";
AtomicInteger msgId = new AtomicInteger();
GoogleCloudMessaging gcm;
SharedPreferences prefs;
public SQLiteHandler db;
SessionManager session;
protected Context context;
public WebRequest(Context context){
this.context = context.getApplicationContext();
}
public void Request(final String url, final String vars, final Boolean show, final String title, final String msg, final String requestName){
final SessionManager session = new SessionManager(context);
final ProgressDialog theProgressDialog = new ProgressDialog(context);
db = new SQLiteHandler(context);
if(show == true){
theProgressDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
theProgressDialog.setTitle(title);
theProgressDialog.setMessage(msg);
theProgressDialog.setIndeterminate(true);
theProgressDialog.setCancelable(false);
theProgressDialog.show();
}
StringRequest strreq = new StringRequest(Method.GET, url + vars,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, "WEB Response: " + response.toString());
try {
JSONObject jObj = new JSONObject(response);
boolean error = jObj.getBoolean("error");
// Check for error node in json
if (!error) {
switch(requestName){
case "req_login":
//process user login
// Fetching user details from sqlite
HashMap<String, String> user = db.fetchResults(dbTables.TABLE_LOGIN, null);
String storedUid = user.get("uid");
JSONObject login = jObj.getJSONObject("login");
if(storedUid != login.getString("uid")){
//new user for device
String[] emptyTables = {dbTables.TABLE_LOGIN};
for(int i=0; i<emptyTables.length; i++){
db.emptyTable(emptyTables[i]);
Log.d(TAG, "empty table : " + emptyTables[i]);
}
}
//store user in database
db.addUser(login.getString("uid"),
login.getString("companyid"),
login.getString("resourceid"),
login.getString("groupid"),
login.getString("title"),
login.getString("firstname"),
login.getString("middleinitial"),
login.getString("lastname"),
login.getString("jobtitle"),
login.getString("managerid"),
login.getString("email"),
login.getString("photo_url"),
login.getString("signature_url"),
login.getString("language"),
login.getString("skin"),
login.getString("defaultprojectid"),
login.getString("cnNotifications"),
login.getString("crNotifications"),
login.getString("coNotifications"),
login.getString("addedby"),
login.getString("dateadded"),
login.getString("editedby"),
login.getString("dateedited"));
// Create login session
session.setLogin(true);
// Launch main activity
Intent intent = new Intent(context, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
//finish();
break;
}
if(show == true){
theProgressDialog.dismiss();
}
}else{
String errorMsg = jObj.getString("error_msg");
Toast.makeText(context, errorMsg, Toast.LENGTH_LONG).show();
if(show == true){
theProgressDialog.dismiss();
}
}
}catch(JSONException e){
// JSON error
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Web Error: " + error.getMessage());
Toast.makeText(context, error.getMessage(), Toast.LENGTH_LONG).show();
if(show == true){
theProgressDialog.dismiss();
}
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(strreq, requestName);
}
}
我还包括了我没有亲自更改的 AppController
package app;
import android.app.Application;
import android.text.TextUtils;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
public class AppController extends Application {
public static final String TAG = AppController.class.getSimpleName();
private RequestQueue mRequestQueue;
private static AppController mInstance;
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
}
public static synchronized AppController getInstance() {
return mInstance;
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
mRequestQueue = Volley.newRequestQueue(getApplicationContext());
}
return mRequestQueue;
}
public <T> void addToRequestQueue(Request<T> req, String tag) {
req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
getRequestQueue().add(req);
}
public <T> void addToRequestQueue(Request<T> req) {
req.setTag(TAG);
getRequestQueue().add(req);
}
public void cancelPendingRequests(Object tag) {
if (mRequestQueue != null) {
mRequestQueue.cancelAll(tag);
}
}
}
- 这不是创建 Activity 对象的方法。很多事情都出错了
像这样。
- Activity 是上下文因此他不需要持有上下文
作为会员。这是常见的内存泄漏。
- activity 没有经历正确的生命周期,这就是为什么
一切都爆炸了。
我建议你从基础开始,因为当你这样做时,一切都会更清楚。
使用 volley 创建请求时不需要 AppController ......只需将请求添加到 volley android volley 请求......https://www.simplifiedcoding.net/android-volley-post-request-tutorial/..... ...
你发送请求的方式将决定应用程序是否崩溃......其一,如果请求发送正确,Sqlite会导致应用程序崩溃......
我正在使用 Android Volley 登录用户,但是当调用 addToRequestQueue 时,会显示以下崩溃报告。
在Loginactivity中onclick进行登录请求如下:
// Login button Click Event
btnLogin.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String username = inputUserName.getText().toString();
String pin = inputPIN.getText().toString();
// Check for empty data in the form
if (username.trim().length() > 0 && pin.trim().length() > 0) {
// login user
SharedPreferences prefs = getGCMPreferences(context);
String storeRegId = prefs.getString(PROPERTY_REG_ID, "");
String devid = Secure.getString(getBaseContext().getContentResolver(),Secure.ANDROID_ID);
String vars = "/?tag=login&username=" + username + "&pin=" + pin + "®id=" + storeRegId + "&devid=" + devid;
WebRequest wr = new WebRequest(context);
wr.Request(AppConfig.URL_LOGIN, vars, true, "Please Wait", "Logging in...", "req_login");
} else {
// Prompt user to enter credentials
Toast.makeText(getApplicationContext(), "Please enter the credentials!", Toast.LENGTH_LONG).show();
}
}
});
使用 volley 的 WebRequest 如下所示:
package app;
import helper.SQLiteHandler;
import helper.SessionManager;
import helper.dbTables;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import main.MainActivity;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.WindowManager;
import android.widget.Toast;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.Request.Method;
import com.android.volley.toolbox.StringRequest;
import com.google.android.gms.gcm.GoogleCloudMessaging;
public class WebRequest extends Activity {
private static final String TAG = "LoginActivity";
AtomicInteger msgId = new AtomicInteger();
GoogleCloudMessaging gcm;
SharedPreferences prefs;
public SQLiteHandler db;
SessionManager session;
protected Context context;
public WebRequest(Context context){
this.context = context.getApplicationContext();
}
public void Request(final String url, final String vars, final Boolean show, final String title, final String msg, final String requestName){
final SessionManager session = new SessionManager(context);
final ProgressDialog theProgressDialog = new ProgressDialog(context);
db = new SQLiteHandler(context);
if(show == true){
theProgressDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
theProgressDialog.setTitle(title);
theProgressDialog.setMessage(msg);
theProgressDialog.setIndeterminate(true);
theProgressDialog.setCancelable(false);
theProgressDialog.show();
}
StringRequest strreq = new StringRequest(Method.GET, url + vars,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, "WEB Response: " + response.toString());
try {
JSONObject jObj = new JSONObject(response);
boolean error = jObj.getBoolean("error");
// Check for error node in json
if (!error) {
switch(requestName){
case "req_login":
//process user login
// Fetching user details from sqlite
HashMap<String, String> user = db.fetchResults(dbTables.TABLE_LOGIN, null);
String storedUid = user.get("uid");
JSONObject login = jObj.getJSONObject("login");
if(storedUid != login.getString("uid")){
//new user for device
String[] emptyTables = {dbTables.TABLE_LOGIN};
for(int i=0; i<emptyTables.length; i++){
db.emptyTable(emptyTables[i]);
Log.d(TAG, "empty table : " + emptyTables[i]);
}
}
//store user in database
db.addUser(login.getString("uid"),
login.getString("companyid"),
login.getString("resourceid"),
login.getString("groupid"),
login.getString("title"),
login.getString("firstname"),
login.getString("middleinitial"),
login.getString("lastname"),
login.getString("jobtitle"),
login.getString("managerid"),
login.getString("email"),
login.getString("photo_url"),
login.getString("signature_url"),
login.getString("language"),
login.getString("skin"),
login.getString("defaultprojectid"),
login.getString("cnNotifications"),
login.getString("crNotifications"),
login.getString("coNotifications"),
login.getString("addedby"),
login.getString("dateadded"),
login.getString("editedby"),
login.getString("dateedited"));
// Create login session
session.setLogin(true);
// Launch main activity
Intent intent = new Intent(context, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
//finish();
break;
}
if(show == true){
theProgressDialog.dismiss();
}
}else{
String errorMsg = jObj.getString("error_msg");
Toast.makeText(context, errorMsg, Toast.LENGTH_LONG).show();
if(show == true){
theProgressDialog.dismiss();
}
}
}catch(JSONException e){
// JSON error
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Web Error: " + error.getMessage());
Toast.makeText(context, error.getMessage(), Toast.LENGTH_LONG).show();
if(show == true){
theProgressDialog.dismiss();
}
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(strreq, requestName);
}
}
我还包括了我没有亲自更改的 AppController
package app;
import android.app.Application;
import android.text.TextUtils;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
public class AppController extends Application {
public static final String TAG = AppController.class.getSimpleName();
private RequestQueue mRequestQueue;
private static AppController mInstance;
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
}
public static synchronized AppController getInstance() {
return mInstance;
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
mRequestQueue = Volley.newRequestQueue(getApplicationContext());
}
return mRequestQueue;
}
public <T> void addToRequestQueue(Request<T> req, String tag) {
req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
getRequestQueue().add(req);
}
public <T> void addToRequestQueue(Request<T> req) {
req.setTag(TAG);
getRequestQueue().add(req);
}
public void cancelPendingRequests(Object tag) {
if (mRequestQueue != null) {
mRequestQueue.cancelAll(tag);
}
}
}
- 这不是创建 Activity 对象的方法。很多事情都出错了
像这样。
- Activity 是上下文因此他不需要持有上下文 作为会员。这是常见的内存泄漏。
- activity 没有经历正确的生命周期,这就是为什么 一切都爆炸了。
我建议你从基础开始,因为当你这样做时,一切都会更清楚。
使用 volley 创建请求时不需要 AppController ......只需将请求添加到 volley android volley 请求......https://www.simplifiedcoding.net/android-volley-post-request-tutorial/..... ...
你发送请求的方式将决定应用程序是否崩溃......其一,如果请求发送正确,Sqlite会导致应用程序崩溃......