在BroadcastReceiver中打开数据库的难度

时间:2011-09-22 16:05:43

标签: android sqlite broadcastreceiver

我在课堂上打开数据库时遇到了困难。我试图让我的应用程序启动电话启动,只有当数据库中的布尔值为true时,如果为false,它将无法在启动时启动应用程序。我有错误到底

public class My_BroadcastReceiver extends BroadcastReceiver {   

SQLiteDatabase sqlDB;
private static String DBNAME2 = "database2.db";
private static String Table2 = "options";

Boolean isOk = true;
Context context;

@Override  
public void onReceive(Context context, Intent intent) {


    try {

        sqlDB = SQLiteDatabase.openOrCreateDatabase(DBNAME2, null);

        Cursor r = sqlDB.rawQuery("SELECT Apponstart FROM " + Table2, null);
        System.out.println("COUNT of Location: " + r.getCount());
        int loc = r.getColumnIndex("Apponstart");

        int j = 0;
        if (r.moveToFirst()) {
            do {
                System.out.println("Apponstart is " + r.getString(loc));

                isOk = Boolean.parseBoolean(r.getString(loc));

                j++;
            } while (r.moveToNext());
            if (j == 0) {
                System.out.println("No data found");
            }

        }
        r.close();
        // sqlDB.close();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        sqlDB.close();
    }

    if(isOk){
        Intent i = new Intent(context, JoshTwoActivity.class);                   
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);                
        context.startActivity(i); 
    }
}



 09-23 08:10:05.615: WARN/System.err(208): android.database.sqlite.SQLiteException: unable to open database file
        09-23 08:10:05.625: WARN/System.err(208):     at android.database.sqlite.SQLiteDatabase.dbopen(Native Method)
        09-23 08:10:05.625: WARN/System.err(208):     at                android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1812)
        09-23 08:10:05.625: WARN/System.err(208):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:817)
        09-23 08:10:05.625: WARN/System.err(208):     at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:851)
        09-23 08:10:05.625: WARN/System.err(208):     at three.three.My_BroadcastReceiver.onReceive(My_BroadcastReceiver.java:31)
         09-23 08:10:05.625: WARN/System.err(208):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2810)
         09-23 08:10:05.625: WARN/System.err(208):     at android.app.ActivityThread.access$3200(ActivityThread.java:125)
           09-23 08:10:05.625: WARN/System.err(208):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2083)
          09-23 08:10:05.625: WARN/System.err(208):     at android.os.Handler.dispatchMessage(Handler.java:99)
            09-23 08:10:05.625: WARN/System.err(208):     at android.os.Looper.loop(Looper.java:123)
          09-23 08:10:05.625: WARN/System.err(208):     at android.app.ActivityThread.main(ActivityThread.java:4627)
          09-23 08:10:05.625: WARN/System.err(208):     at java.lang.reflect.Method.invokeNative(Native Method)
          09-23 08:10:05.625: WARN/System.err(208):     at java.lang.reflect.Method.invoke(Method.java:521)
         09-23 08:10:05.625: WARN/System.err(208):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
         09-23 08:10:05.625: WARN/System.err(208):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
         09-23 08:10:05.625: WARN/System.err(208):     at dalvik.system.NativeStart.main(Native Method)

再次

       09-23 08:10:06.046: ERROR/AndroidRuntime(208): FATAL EXCEPTION: main
       09-23 08:10:06.046: ERROR/AndroidRuntime(208): java.lang.RuntimeException:  Unable to start receiver three.three.My_BroadcastReceiver: java.lang.NullPointerException
       09-23 08:10:06.046: ERROR/AndroidRuntime(208):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2821)
       09-23 08:10:06.046: ERROR/AndroidRuntime(208):     at android.app.ActivityThread.access$3200(ActivityThread.java:125)
       09-23 08:10:06.046: ERROR/AndroidRuntime(208):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2083)
       09-23 08:10:06.046: ERROR/AndroidRuntime(208):     at android.os.Handler.dispatchMessage(Handler.java:99)
       09-23 08:10:06.046: ERROR/AndroidRuntime(208):     at android.os.Looper.loop(Looper.java:123)
       09-23 08:10:06.046: ERROR/AndroidRuntime(208):     at android.app.ActivityThread.main(ActivityThread.java:4627)
       09-23 08:10:06.046: ERROR/AndroidRuntime(208):     at java.lang.reflect.Method.invokeNative(Native Method)
       09-23 08:10:06.046: ERROR/AndroidRuntime(208):     at java.lang.reflect.Method.invoke(Method.java:521)
       09-23 08:10:06.046: ERROR/AndroidRuntime(208):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
       09-23 08:10:06.046: ERROR/AndroidRuntime(208):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
       09-23 08:10:06.046: ERROR/AndroidRuntime(208):     at dalvik.system.NativeStart.main(Native Method)
       09-23 08:10:06.046: ERROR/AndroidRuntime(208): Caused by: java.lang.NullPointerException
       09-23 08:10:06.046: ERROR/AndroidRuntime(208):     at three.three.My_BroadcastReceiver.onReceive(My_BroadcastReceiver.java:57)
       09-23 08:10:06.046: ERROR/AndroidRuntime(208):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2810)
       09-23 08:10:06.046: ERROR/AndroidRuntime(208):     ... 10 more

2 个答案:

答案 0 :(得分:0)

可能需要更多代码来解决问题,但我想这个问题与接收器注册的方式有关。你是用XML还是通过registerReceiver声明的?这是onReceive吗?

在此参考页面上阅读BroadcastReceiver生命周期:http://developer.android.com/reference/android/content/BroadcastReceiver.html

答案 1 :(得分:0)

数据库是问题所在。我创建了一个方法并在数据库的开头添加了上下文。这是我的解决方案。

public void guru(Context ctx){

    System.out.println("Flax");
    try {
        Log.e("troll", "alol");
        sqlDB = ctx.openOrCreateDatabase(DBNAME2, 0,null); 
        //sqlDB = SQLiteDatabase.openOrCreateDatabase(DBNAME2, null);           
        Cursor r = sqlDB.rawQuery("SELECT Apponstart FROM " + Table2, null);
        System.out.println("COUNT of Location: " + r.getCount());
        int loc = r.getColumnIndex("Apponstart");

        int j = 0;
        if (r.moveToFirst()) {
            do {
                System.out.println("Apponstart is " + r.getString(loc));
                Log.d("happening", "here");
                isOk = Boolean.parseBoolean(r.getString(loc));

                j++;
            } while (r.moveToNext());
            if (j == 0) {
                System.out.println("No data found");
            }

        }
        r.close();
        // sqlDB.close();
    } catch (Exception e) {
        e.printStackTrace();
        Log.e("exception", e.toString());
    } finally {
        sqlDB.close();
    }
}