无法在android中打开本地数据库

时间:2012-07-09 10:05:40

标签: android database cordova

我在android中有服务,从那个服务我开始活动。我从服务中调用了活动内部的方法。在那个方法中,我打开数据库并尝试插入值。没有使用数据库,它的工作正常。但是当我使用数据库

时,我的应用程序被关闭了

插件(java文件) - >服务 - >活动(这里我尝试在数据库中插入数据)

service.java

public class MyService extends Service 
{
    @Override
    public IBinder onBind(Intent intent) 
    {
        return null;
    }

    @Override
    public void onCreate() 
    {
        Log.d(TAG, "onCreate");
    }

    @Override
    public void onDestroy() 
    {
        Log.d(TAG, "onDestroy");
    }

    public void onStart(Intent intent, int startid) 
    {
        Timer mTimer = new Timer(user);
        mTimer.scheduleAtFixedRate(new mainTask(), 5000,60000);//1 hour=3600 s

    }

    private class mainTask extends TimerTask 
    { 
        public void run() 
        {
            toastHandler.sendEmptyMessage(0);
        }
    }  


    private final Handler toastHandler = new Handler() 
    {
        public void handleMessage(Message msg) 
        {
             StorageHelper s=new StorageHelper();
             String a= s.UpdateValues(userid);
        }
    };    
}

Activity.java

public class StorageHelper extends Activity
{
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState); 
    }

    public  String UpdateValues(int userid)
    {
        try {
            DBAdapter1 database=new DBAdapter1(this);
            database.open();
            long id=database.insert(71,4,"yes");
            database.close();
        } catch (SQLException e) {

        }
        return "success";
    }
}

注意 当我在oncreate中打开数据库工作时,但在updatavalues()数据库里面无法打开插入。

try {
    DBAdapter1 database=new DBAdapter1(this);
    database.open();
    long id=database.insert(71,4,"yes");
    database.close();
} catch (SQLException e) {

}

我有两种力量关闭

错误编号1

07-09 15:16:27.859: E/AndroidRuntime(1211): FATAL EXCEPTION: main
07-09 15:16:27.859: E/AndroidRuntime(1211): java.lang.NullPointerException
07-09 15:16:27.859: E/AndroidRuntime(1211):     at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)
07-09 15:16:27.859: E/AndroidRuntime(1211):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:98)
07-09 15:16:27.859: E/AndroidRuntime(1211):     at com.app.mobilyzer.DBAdapter1.open(DBAdapter1.java:68)
07-09 15:16:27.859: E/AndroidRuntime(1211):     at com.app.mobilyzer.StorageHelper.UpdateValues(StorageHelper.java:33)
07-09 15:16:27.859: E/AndroidRuntime(1211):     at com.app.mobilyzer.MyService$1.handleMessage(MyService.java:121)
07-09 15:16:27.859: E/AndroidRuntime(1211):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-09 15:16:27.859: E/AndroidRuntime(1211):     at android.os.Looper.loop(Looper.java:123)
07-09 15:16:27.859: E/AndroidRuntime(1211):     at android.app.ActivityThread.main(ActivityThread.java:4627)
07-09 15:16:27.859: E/AndroidRuntime(1211):     at java.lang.reflect.Method.invokeNative(Native Method)
07-09 15:16:27.859: E/AndroidRuntime(1211):     at java.lang.reflect.Method.invoke(Method.java:521)
07-09 15:16:27.859: E/AndroidRuntime(1211):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-09 15:16:27.859: E/AndroidRuntime(1211):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-09 15:16:27.859: E/AndroidRuntime(1211):     at dalvik.system.NativeStart.main(Native Method)

错误编号2

07-09 15:16:36.889: E/AndroidRuntime(1237): FATAL EXCEPTION: main
07-09 15:16:36.889: E/AndroidRuntime(1237): java.lang.RuntimeException: Unable to start service com.app.mobilyzer.MyService@44fc6280 with null: java.lang.NullPointerException
07-09 15:16:36.889: E/AndroidRuntime(1237):     at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3063)
07-09 15:16:36.889: E/AndroidRuntime(1237):     at android.app.ActivityThread.access$3600(ActivityThread.java:125)
07-09 15:16:36.889: E/AndroidRuntime(1237):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2096)
07-09 15:16:36.889: E/AndroidRuntime(1237):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-09 15:16:36.889: E/AndroidRuntime(1237):     at android.os.Looper.loop(Looper.java:123)
07-09 15:16:36.889: E/AndroidRuntime(1237):     at android.app.ActivityThread.main(ActivityThread.java:4627)
07-09 15:16:36.889: E/AndroidRuntime(1237):     at java.lang.reflect.Method.invokeNative(Native Method)
07-09 15:16:36.889: E/AndroidRuntime(1237):     at java.lang.reflect.Method.invoke(Method.java:521)
07-09 15:16:36.889: E/AndroidRuntime(1237):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-09 15:16:36.889: E/AndroidRuntime(1237):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-09 15:16:36.889: E/AndroidRuntime(1237):     at dalvik.system.NativeStart.main(Native Method)
07-09 15:16:36.889: E/AndroidRuntime(1237): Caused by: java.lang.NullPointerException
07-09 15:16:36.889: E/AndroidRuntime(1237):     at com.app.mobilyzer.MyService.onStart(MyService.java:81)
07-09 15:16:36.889: E/AndroidRuntime(1237):     at android.app.Service.onStartCommand(Service.java:420)
07-09 15:16:36.889: E/AndroidRuntime(1237):     at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3053)
07-09 15:16:36.889: E/AndroidRuntime(1237):     ... 10 more

1 个答案:

答案 0 :(得分:0)

这两个异常都是 NullPointerException 类型,在database.open()方法中是第一个。 DBAdapter1.java:68。看看你在DBAdapter1类的第68行做了什么。如果您怀疑该值可以为null / empty

,则始终进行空检查
07-09 15:16:27.859: E/AndroidRuntime(1211): java.lang.NullPointerException
07-09 15:16:27.859: E/AndroidRuntime(1211):     at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)
07-09 15:16:27.859: E/AndroidRuntime(1211):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:98)
07-09 15:16:27.859: E/AndroidRuntime(1211):     at com.app.mobilyzer.DBAdapter1.open(DBAdapter1.java:68)

第二个例外是在Onstart方法的第81行的MyService.java服务中。

07-09 15:16:36.889: E/AndroidRuntime(1237): Caused by: java.lang.NullPointerException
07-09 15:16:36.889: E/AndroidRuntime(1237):     at com.app.mobilyzer.MyService.onStart(MyService.java:81)