-1

我知道如何做 SQLite 数据库,特别是在它自己的专用 java 类中。但是每当我希望它位于某个东西的内部时,在这种情况下,它是一个 Intent 服务,它总是给我错误,我无法调试它。

package my.skul;


    import android.app.IntentService;
    import android.app.Notification;
    import android.app.NotificationManager;
    import android.app.PendingIntent;
    import android.content.Context;
    import android.content.Intent;
    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteOpenHelper;

    public class CheckClassroom extends IntentService {

                /**
                 * A constructor is required, and must call the super IntentService(String)
                 * constructor with a name for the worker thread.
                 */
                public CheckClassroom() {
                    super("CheckClassroom");
                }

                /**
                 * The IntentService calls this method from the default worker thread with
                 * the intent that started the service. When this method returns,
                 * IntentService stops the service, as appropriate.
                 */
                @Override
                protected void onHandleIntent(Intent intent) {





                }
                private DbHelper ourHelper;
                private final Context ourContext;
                private SQLiteDatabase ourDatabase;

                private static class DbHelper extends SQLiteOpenHelper {

                    public DbHelper(Context context) {
                        super(context, "myschool", null, 1);
                        // TODO Auto-generated constructor stub
                    }

                    @Override
                    public void onCreate(SQLiteDatabase arg0) {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
                        // TODO Auto-generated method stub

                    }

                    public CheckClassroom(Context c) {
                        ourContext = c;
                    }

                    public CheckClassroom open() {
                        ourHelper = new DbHelper(ourContext);
                        ourDatabase = ourHelper.getWritableDatabase();

                        return this;

                    }

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

            }
                getColumns()
                {
                    //retrieve sqlitedatabase data

                }


    }

我一直在阅读很多关于构造函数的内容,但我并不能完全理解它,在这里解释我所有的错误真的会让我更好地理解这一点。谢谢..

4

1 回答 1

2

当我将鼠标悬停在 public CheckClassroom() { super("CheckClassroom"); } 空白的 final 字段 ourContext 可能没有被初始化

因为您将DbHelper类声明为静态,所以它会在实例化后立即创建一个实例CheckClassroom。那个时候ourContext还没有初始化。

要解决此问题,请删除DbHelper类的静态并将一个实例改为静态字段。然后在构造函数中你可以初始化它(在你初始化ourContext之后)。

private static DbHelper myDbHelper;

public CheckClassroom() {
    super("CheckClassroom");
    ourContext = this;
    myDbHelper = new DbHelper();
}

而不是使用ourContext,你可以CheckClassroom.thisDbHelper类中使用。

还...

public CheckClassroom(Context c) { ourContext = c; }函数与类内部无关DbHelper....

此外,打开和关闭功能似乎不正确,相反,我认为您应该覆盖 onCreate 和 onDestroy

这是您的代码,已修复:

package my.skul;

import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class CheckClassroom extends IntentService {

            /**
             * A constructor is required, and must call the super IntentService(String)
             * constructor with a name for the worker thread.
             */
            public CheckClassroom() {
                super("CheckClassroom");
            }

            /**
             * The IntentService calls this method from the default worker thread with
             * the intent that started the service. When this method returns,
             * IntentService stops the service, as appropriate.
             */
            @Override
            protected void onHandleIntent(Intent intent) {





            }
            private DbHelper ourHelper;
            private SQLiteDatabase ourDatabase;

            @Override
            public void onCreate() {
                ourHelper = new DbHelper();
                ourDatabase = ourHelper.getWritableDatabase();
            }

            @Override
            public void onDestroy() {
                ourHelper.close();
            }

            private class DbHelper extends SQLiteOpenHelper {

                public DbHelper() {
                    super(CheckClassroom.this, "myschool", null, 1);
                    // TODO Auto-generated constructor stub
                }

                @Override
                public void onCreate(SQLiteDatabase arg0) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
                    // TODO Auto-generated method stub

                }
            }
            /* not sure what that is... 
            getColumns()
            {
                //retrieve sqlitedatabase data

            }
            */
}
于 2012-10-29T01:00:18.247 回答