SQLiteDatabase db = this.getWritableDatabase()在收到广播时抛出错误

时间:2017-04-05 19:07:49

标签: android sqlite broadcastreceiver android-sqlite android-broadcastreceiver

我正在尝试在收到广播时从数据库中检索一些表条目(在这种情况下是连接变化)。

我已经将错误追踪到了 在getAllData()方法中的SQLiteDatabase db = this.getWritableDatabase();

以下是代码:

sendMessageFromDatabase:

    import android.content.Context;
    import android.database.Cursor;
    import android.os.Handler;
    import android.util.Log;

    import com.loopj.android.http.AsyncHttpClient;
    import com.loopj.android.http.JsonHttpResponseHandler;
    import com.loopj.android.http.RequestParams;
    import com.pusher.client.Pusher;
    import com.pusher.client.PusherOptions;
    import com.pusher.client.channel.Channel;
    import com.pusher.client.channel.SubscriptionEventListener;

    import org.json.JSONObject;

    import java.util.ArrayList;

    import cz.msebera.android.httpclient.Header;

    import static android.content.ContentValues.TAG;


    public class sendMessageFromDatabase {

        private static final String TAG = "SendingMFD DB";

        private final Handler handler;

        final String MESSAGES_ENDPOINT = "http://fragmentstanvir.azurewebsites.net";

        DataBaseHelper myDB;
        Context context;

        public sendMessageFromDatabase(Handler handler) {

            this.handler = handler;
            RequestParams rp = new RequestParams();
            SendMessageFromDBMethod(rp);
        }




        public void SendMessageFromDBMethod(RequestParams param) {

            Log.d(TAG, "Method started");

            AsyncHttpClient client = new AsyncHttpClient();

            //pusher
            PusherOptions options = new PusherOptions();
            options.setCluster("ap2");
            Pusher pusher = new Pusher("87ded5c4cb1e22c46dd4", options);

            Channel channel = pusher.subscribe("my-channel");

            channel.bind("my-event", new SubscriptionEventListener() {
                @Override
                public void onEvent(String channelName, String eventName, final String data) {
                    System.out.println(data);
                }
            });

            pusher.connect();

            RequestParams postParams = new RequestParams();

            // Database
            Log.d(TAG, "DATABASE OPERATIONS STARTED");

            myDB = new DataBaseHelper(context);

            Cursor res = myDB.getAllData();

            if (res.getCount() == 0) {
                Log.d(TAG, "No data found");
                return;

            }

            final ArrayList<String> messageList = new ArrayList();

            while (res.moveToNext()) {
                messageList.add(res.getString(0));
            }

            Log.d(TAG, "BUFFER LENGTH :: " + messageList.size());

            for (int i = 0; i < messageList.size(); i++) {

                client.post(MESSAGES_ENDPOINT + "/messages" + messageList.get(i).toString(), postParams, new JsonHttpResponseHandler() {

                    @Override
                    public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                Log.d(TAG, "fuck the log");
                            }
                        });
                    }

                    @Override
                    public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                        //Toast.makeText(, "Something went wrong :(", Toast.LENGTH_LONG).show();
                    }
                });

            }

            return;

        }

    }

DataBaseHelper.java ::

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DataBaseHelper extends SQLiteOpenHelper {

    public static final String DATABASE_NAME = "messages.db";
    public static final String TABLE_NAME = "kalol";
    public static final String COL_1 = "params";
    SQLiteDatabase db;
    private Context mContext;

    public DataBaseHelper(Context context) {
        super(context, DATABASE_NAME, null, 1);
        mContext = context;
        db = this.getWritableDatabase();
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table " + TABLE_NAME + " (PARAMS TEXT UNIQUE) ");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int i, int i1) {
        db.execSQL("DROP TABLE IF EXISTS" + TABLE_NAME);
        onCreate(db);
    }

    public boolean insertData(String params){
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(COL_1,params);
        long result = db.insert(TABLE_NAME,null, contentValues);
        if (result == -1)
            return false;
        else
            return true;
    }

    public Cursor getAllData(){
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor res = db.rawQuery("SELECT * FROM " + TABLE_NAME, null);
        return res;
    }

}

Logcat:

04-06 00:17:00.999 22916-22916/com.tanvirsingh.fragmentsdemo E/AndroidRuntime: FATAL EXCEPTION: main
                                                                               Process: com.tanvirsingh.fragmentsdemo, PID: 22916
                                                                               Theme: themes:{default=overlay:system, iconPack:com.baranovgroup.nstyle, fontPkg:com.baranovgroup.nstyle, com.android.systemui=overlay:com.baranovgroup.nstyle, com.android.systemui.navbar=overlay:system}
                                                                               java.lang.RuntimeException: Unable to start receiver com.tanvirsingh.fragmentsdemo.NetworkChangeReceiver: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.sqlite.SQLiteDatabase android.content.Context.openOrCreateDatabase(java.lang.String, int, android.database.sqlite.SQLiteDatabase$CursorFactory, android.database.DatabaseErrorHandler)' on a null object reference
                                                                                   at android.app.ActivityThread.handleReceiver(ActivityThread.java:2778)
                                                                                   at android.app.ActivityThread.-wrap14(ActivityThread.java)
                                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1440)
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                   at android.os.Looper.loop(Looper.java:148)
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:5471)
                                                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                                   at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:102)
                                                                                Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.sqlite.SQLiteDatabase android.content.Context.openOrCreateDatabase(java.lang.String, int, android.database.sqlite.SQLiteDatabase$CursorFactory, android.database.DatabaseErrorHandler)' on a null object reference
                                                                                   at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:223)
                                                                                   at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
                                                                                   at com.tanvirsingh.fragmentsdemo.DataBaseHelper.<init>(DataBaseHelper.java:20)
                                                                                   at com.tanvirsingh.fragmentsdemo.sendMessageFromDatabase.SendMessageFromDBMethod(sendMessageFromDatabase.java:73)
                                                                                   at com.tanvirsingh.fragmentsdemo.sendMessageFromDatabase.<init>(sendMessageFromDatabase.java:40)
                                                                                   at com.tanvirsingh.fragmentsdemo.NetworkChangeReceiver.onReceive(NetworkChangeReceiver.java:43)
                                                                                   at android.app.ActivityThread.handleReceiver(ActivityThread.java:2771)
                                                                                   at android.app.ActivityThread.-wrap14(ActivityThread.java) 
                                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1440) 
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                   at android.os.Looper.loop(Looper.java:148) 
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:5471) 
                                                                                   at java.lang.reflect.Method.invoke(Native Method) 
                                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                                                                                   at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:102) 

0 个答案:

没有答案