在BroadcastReceiver中打开数据库困难

发布于 2024-12-06 12:11:23 字数 5506 浏览 0 评论 0原文

我在课堂上打开数据库时遇到困难。我试图让我的应用程序在手机启动时启动,前提是数据库中的布尔值为 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

I am having difficulty opening my database in my class. I am trying to get my application to start on the phone bootup, that is only if the boolean value in the database is true, if false it will not boot the application on startup. I have the errors to the bottom

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)

And again at

       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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不知所踪 2024-12-13 12:11:23

可能需要更多代码来解决问题,但我想问题与接收器的注册方式有关。您是在 XML 中声明还是通过 registerReceiver 声明?这个 onReceive 是否曾经被达到过?

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

Will probably need more code to solve the problem, but I imagine the problem has something to do with teh way that the receiver is registered. Do you declare in XML or via registerReceiver? Is this onReceive ever being reached?

Read about the BroadcastReceiver lifecycle on this reference page: http://developer.android.com/reference/android/content/BroadcastReceiver.html

深海蓝天 2024-12-13 12:11:23

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

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();
    }
}

The database was the problem. I created a method and added context to the opening of the database. Here is my solution.

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();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文