在后台运行Volley请求上调用getWritableDatabase()时,SQLite崩溃

时间:2015-04-14 18:06:17

标签: android sqlite android-volley

当Caller Fragment正在运行时,请求能够成功执行getWritableDatabase()。 但是当片段关闭或应用程序关闭时,在后台执行的截击请求无法执行getWritableDatabase()

public class ContactsTableManager {
private Context context;
private DBHelper ourHelper;
private SQLiteDatabase ourDatabase;
private static final String DATABASE_NAME = "Contacts";
private static final String DATABASE_TABLE = "ContactsTable";
private static final int DATABASE_VERSION = 1;
public static final String TAG = "contactsmanager.java";
public static final String KEY_NAME = "NAME";
public static final String KEY_NUMBER = "NUMBER";
public static final String KEY_ID = "ID";



public ContactsTableManager(Context c) {
    context = c;
}

public ContactsTableManager open() {


    ourHelper = new DBHelper(context);
    ourDatabase = ourHelper.getWritableDatabase();
    return this;
}

public void close() {
    ourHelper.close();

}
public boolean addEntry(String Name, String Number) {

    boolean success=false;
    ContentValues cv = new ContentValues();
    cv.put(KEY_NAME,Name);
    cv.put(KEY_NUMBER,Number);
    if(!checkData(cv)){
        open();
        success = ourDatabase.insert(DATABASE_TABLE, null, cv) > 0;
        close();
    }

    return success;
}
private static class DBHelper extends SQLiteOpenHelper {

    public DBHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
                        KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                        KEY_NAME + " TEXT NOT NULL, " + KEY_NUMBER + " TEXT NOT NULL); "
        );

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
        onCreate(db);
    }
}

}

排球请求代码,在片段上执行, 我甚至尝试在onCreateView()中传递getActivity()。 没用。

    private void sendNumberRequest(final String name, final String phoneNumber) {


    String tag_string_req = "req_contact";



    StringRequest strReq = new StringRequest(Request.Method.POST,
            AppConfig.URL_Contacts, new Response.Listener<String>() {

        @Override
        public void onResponse(String response) {
            response=response.substring(response.indexOf("{"));


            try {
                JSONObject jObj = new JSONObject(response);
                Log.e(TAG, "JSONObject: " + jObj.toString());
                boolean error = jObj.getBoolean("error");

                // Check for error node in json
                if (!error) {

                    JSONObject user = jObj.getJSONObject("user");

                    ContactsTableManager contactsTableManager1=new ContactsTableManager(getActivity());





                    // Inserting row in users table

                    contactsTableManager1.addEntry(name,phoneNumber);

                    makeScreen();

                }
            } catch (JSONException e) {
                // JSON error
                Log.e(TAG, "Login Response: catch");
                e.printStackTrace();
            }

        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e(TAG, "Login Error: " + error.getMessage());


        }
    }) {

        @Override
        protected Map<String, String> getParams() {
            // Posting parameters to login url
            Map<String, String> params = new HashMap<String, String>();
            params.put("tag", "contact_req");
            params.put("name", name);
            params.put("number", phoneNumber);

            return params;
        }

    };

    // Adding request to request queue
    AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
}

1 个答案:

答案 0 :(得分:0)

当您的Activity关闭时,用于获取数据库的上下文已不复存在。如果您想中止操作,那么您可以使用queue.cancelAll(tag_string_req);

取消可能onDestroy()中的Volley请求

如果要将数据保留在应用程序范围之外,请使用“服务”。