0

我在android中有服务,从那个服务我开始了一个活动。我从服务中调用了活动中的方法。在那个方法中,我打开数据库并尝试插入值。不使用数据库,它工作正常。但是我的应用程序当我使用数据库时强制关闭

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

服务.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);
        }
    };    
}

活动.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
4

1 回答 1

0

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

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)

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

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)
于 2012-07-09T10:16:10.660 回答