Android IntentService在数据插入SQLite后停止,并在第二次插入时给出NULLPOINTER异常

Android IntentService在数据插入SQLite后停止,并在第二次插入时给出NULLPOINTER异常,android,sqlite,service,intentservice,Android,Sqlite,Service,Intentservice,我正在开发一个应用程序,在没有移动信号覆盖区域的情况下,将数据保存到数据库中。当服务返回时,它将从数据库中获取所有数据并上传到服务器上 现在我有两个问题 当第一次将数据插入数据库时,IntentService停止。(当网络服务返回时,它也会在服务器上上载数据,用于检查网络连接,我使用定时器。)我不想停止服务 当我第二次启动服务时,它再次创建并给出Nullpointerexception。因为数据库中并没有数据,所以在数据检索方面也会出现一些其他错误 我的代码有什么问题?我只希望当用户启动服务时,

我正在开发一个应用程序,在没有移动信号覆盖区域的情况下,将数据保存到数据库中。当服务返回时,它将从数据库中获取所有数据并上传到服务器上

现在我有两个问题

  • 当第一次将数据插入数据库时,IntentService停止。(当网络服务返回时,它也会在服务器上上载数据,用于检查网络连接,我使用定时器。)我不想停止服务
  • 当我第二次启动服务时,它再次创建并给出Nullpointerexception。因为数据库中并没有数据,所以在数据检索方面也会出现一些其他错误
  • 我的代码有什么问题?我只希望当用户启动服务时,它将一直运行到数据库中的最后一个数据,然后停止。我在intentservice中使用了线程,因为之前我的应用程序崩溃了

    LogCat错误

    10-14 20:44:02.135: E/Service(10042): Service Created.. 
    10-14 20:44:02.505: E/Service Started(10042): Successful
    10-14 20:44:03.585: E/Service Network(10042): Network is offline
    10-14 20:44:08.656: E/Service Network(10042): Network is offline
    10-14 20:44:13.616: E/Service Network(10042): Network is offline
    10-14 20:44:18.646: E/Service Network(10042): Network is offline
    10-14 20:44:23.595: E/Service Network(10042): Network is offline
    10-14 20:44:29.526: E/Service Network(10042): Network is online
    10-14 20:44:31.256: E/Data Sent(10042): Response 200
    10-14 20:44:33.599: E/Service Network(10042): Network is online
    10-14 20:44:34.446: E/Data Sent(10042): Response 200
    10-14 20:44:38.646: E/Service Network(10042): Network is online
    10-14 20:44:40.616: E/Data Sent(10042): Response 200
    10-14 20:44:43.625: E/Service Network(10042): Network is offline
    10-14 20:44:48.595: E/Service Network(10042): Network is offline
    10-14 20:44:53.586: E/Service Network(10042): Network is offline
    10-14 20:45:20.486: E/Service(10042): Service Created.. 
    10-14 20:45:20.587: E/Service Started(10042): Successful
    10-14 20:45:20.627: E/Insertion(10042): java.lang.NullPointerException
    10-14 20:45:21.666: E/Service Network(10042): Network is offline
    10-14 20:45:23.616: E/Service Network(10042): Network is offline
    10-14 20:45:36.645: E/Service Network(10042): Network is offline
    10-14 20:45:38.585: E/Service Network(10042): Network is offline
    10-14 20:45:41.786: E/Service Network(10042): Network is online
    10-14 20:45:42.026: E/AndroidRuntime(10042): FATAL EXCEPTION: Timer-1
    10-14 20:45:42.026: E/AndroidRuntime(10042): java.lang.NullPointerException
    10-14 20:45:42.026: E/AndroidRuntime(10042):    at com.remote.synchronizer.haris.SQLiteAdapter.getAllContacts(SQLiteAdapter.java:93)
    10-14 20:45:42.026: E/AndroidRuntime(10042):    at com.remote.synchronizer.haris.OfflineDataService$1$1.run(OfflineDataService.java:80)
    10-14 20:45:42.026: E/AndroidRuntime(10042):    at java.util.Timer$TimerImpl.run(Timer.java:284)
    10-14 20:45:43.646: E/Service Network(10042): Network is online
    10-14 20:45:44.266: E/Data Sent(10042): Response 200
    
    SQLiteAdapter.java

    package com.remote.synchronizer.haris;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.apache.http.NameValuePair;
    import org.apache.http.message.BasicNameValuePair;
    
    import android.content.ContentValues;
    import android.content.Context;
    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteOpenHelper;
    import android.util.Log;
    
    public class SQLiteAdapter extends SQLiteOpenHelper {
    
        public static final String DATABASE_NAME = "Product";
        public static final String TABLE_NAME = "Orders";
        public static final int DATABASE_VERSION = 1;
        public static final String KEY_ID = " _id";  //
        public static final String KEY_NAME = "name";
        public static final String KEY_SHOP = "shop";
        private static final String KEY_CITY = "city";
        private static final String KEY_DATE = "datee";
        private static final String KEY_ORDER = "orderr";
    
        private SQLiteDatabase sqLiteDatabase;
        private SQLiteAdapter sqLiteHelper=this;
    
        private static final String CREATE_TABLE =
                "CREATE TABLE " + TABLE_NAME + " ("
                        + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
                        + KEY_NAME + " VARCHAR,"
                        + KEY_SHOP + " VARCHAR," + KEY_CITY + " VARCHAR, " + KEY_DATE + " VARCHAR, " + KEY_ORDER + " VARCHAR" +");";
    
    
        public SQLiteAdapter(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);  
        }
    
        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(CREATE_TABLE);
            sqLiteDatabase=db;
        }
    
        /*public void close(){
            sqLiteHelper.close();
        }
         */
        public void insert(String name, String shop, String city, String datee, String orderr){
    
            try
            {
                ContentValues contentValues = new ContentValues();
                contentValues.put(KEY_NAME, name);
                contentValues.put(KEY_SHOP, shop);
                contentValues.put(KEY_CITY, city);
                contentValues.put(KEY_DATE, datee);
                contentValues.put(KEY_ORDER, orderr);
                sqLiteDatabase.insertOrThrow(TABLE_NAME, KEY_ID, contentValues);
                //sqLiteDatabase.close();
            }
            catch(Exception e)
            {
                e.printStackTrace();
                Log.e("Insertion", e.toString());
            }
        }
    
        public void deleteAll(){
            sqLiteDatabase.delete(TABLE_NAME, null, null);
        }
    
        public void delete_byID(int id){
            sqLiteDatabase.delete(TABLE_NAME, KEY_ID+"="+id, null);
        }
    
        /*public Cursor queueAll(){
            String[] columns = new String[]{KEY_ID, KEY_NAME, KEY_SHOP, KEY_CITY,  KEY_DATE, KEY_ORDER};
            Cursor cursor = sqLiteDatabase.query(TABLE_NAME, columns, 
                    null, null, null, null, null);
    
            return cursor;
        }*/
    
        public List<NameValuePair> getAllContacts() {
            List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
    
            String selectQuery = "SELECT  * FROM " + TABLE_NAME;
            sqLiteHelper.getReadableDatabase();
            Cursor cursor = sqLiteDatabase.rawQuery(selectQuery, null);
    
            if(cursor.moveToFirst()){
                while(!cursor.isAfterLast())
                {
                    postParameters.add(new BasicNameValuePair("User", cursor.getString(cursor.getColumnIndex(KEY_NAME))));
                    postParameters.add(new BasicNameValuePair("ShopName", cursor.getString(cursor.getColumnIndex(KEY_SHOP))));
                    postParameters.add(new BasicNameValuePair("city", cursor.getString(cursor.getColumnIndex(KEY_CITY))));
                    postParameters.add(new BasicNameValuePair("OrderDate", cursor.getString(cursor.getColumnIndex(KEY_DATE))));
                    postParameters.add(new BasicNameValuePair("OrderDetail", cursor.getString(cursor.getColumnIndex(KEY_ORDER))));
                    cursor.moveToNext();
                }
    
            }
            //cursor.close();
            return postParameters;
        };
    
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            Log.w(SQLiteAdapter.class.getName(),
                    "Upgrading database from version " + oldVersion + " to "
                            + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS " + CREATE_TABLE);
            onCreate(db);
        }
    
    }
    
    包com.remote.synchronizer.haris;
    导入java.util.ArrayList;
    导入java.util.List;
    导入org.apache.http.NameValuePair;
    导入org.apache.http.message.BasicNameValuePair;
    导入android.content.ContentValues;
    导入android.content.Context;
    导入android.database.Cursor;
    导入android.database.sqlite.SQLiteDatabase;
    导入android.database.sqlite.SQLiteOpenHelper;
    导入android.util.Log;
    公共类SQLiteAdapter扩展了SQLiteOpenHelper{
    公共静态最终字符串数据库\u NAME=“Product”;
    公共静态最终字符串表\u NAME=“Orders”;
    公共静态最终int数据库_VERSION=1;
    公共静态最终字符串键\u ID=“\u ID”//
    公共静态最终字符串键\u NAME=“NAME”;
    公共静态最终字符串键\u SHOP=“SHOP”;
    私有静态最终字符串KEY_CITY=“CITY”;
    私有静态最终字符串键\u DATE=“datee”;
    私有静态最终字符串键\u ORDER=“ORDER”;
    专用SQLiteDatabase SQLiteDatabase;
    私有SQLiteAdapter sqLiteHelper=this;
    私有静态最终字符串创建_表=
    “创建表格”+表格名称+”(“
    +KEY_ID+“整数主键自动递增,”
    +键名称+“VARCHAR,”
    +钥匙店+“VARCHAR”+“钥匙城市+”VARCHAR“+“钥匙日期+”VARCHAR“+”钥匙订单+”VARCHAR“+”;
    公共SQLiteAdapter(上下文){
    super(上下文、数据库名称、null、数据库版本);
    }
    @凌驾
    public void onCreate(SQLiteDatabase db){
    db.execSQL(创建_表);
    sqLiteDatabase=db;
    }
    /*公众假期结束(){
    sqLiteHelper.close();
    }
    */
    公共无效插入(字符串名称、字符串商店、字符串城市、字符串日期、字符串订购者){
    尝试
    {
    ContentValues ContentValues=新ContentValues();
    contentValues.put(KEY\u NAME,NAME);
    contentValues.put(KEY_SHOP,SHOP);
    contentValues.put(KEY_CITY,CITY);
    contentValues.put(KEY_DATE,datee);
    contentValues.put(KEY\u ORDER,ORDER);
    insertOrThrow(表名称、键ID、内容值);
    //sqLiteDatabase.close();
    }
    捕获(例外e)
    {
    e、 printStackTrace();
    Log.e(“插入”,例如toString());
    }
    }
    public void deleteAll(){
    delete(表名称,null,null);
    }
    公共void delete_byID(int id){
    sqLiteDatabase.delete(表名,键ID+“=”+ID,null);
    }
    /*公共游标queueAll(){
    String[]columns=新字符串[]{KEY\u ID、KEY\u NAME、KEY\u SHOP、KEY\u CITY、KEY\u DATE、KEY\u ORDER};
    
    Cursor Cursor=sqLiteDatabase.query(表名、列、, 空,空,空,空,空,空); 返回光标; }*/ 公共列表getAllContacts(){ List postParameters=new ArrayList(); String selectQuery=“SELECT*FROM”+表格名称; sqLiteHelper.getReadableDatabase();
    Cursor Cursor=sqLiteDatabase.rawQuery(selectQuery,null); if(cursor.moveToFirst()){ 而(!cursor.isAfterLast()) { add(新的BasicNameValuePair(“用户”,cursor.getString(cursor.getColumnIndex(KEY_NAME))); 添加(新的BasicNameValuePair(“ShopName”,cursor.getString(cursor.getColumnIndex(KEY_SHOP))); add(新的BasicNameValuePair(“城市”,cursor.getString(cursor.getColumnIndex(KEY_city))); add(新的BasicNameValuePair(“OrderDate”,cursor.getString(cursor.getColumnIndex(KEY_DATE))); add(新的BasicNameValuePair(“OrderDetail”,cursor.getString(cursor.getColumnIndex(KEY_ORDER))); cursor.moveToNext(); } } //cursor.close(); 返回后参数; }; @凌驾 public void onUpgrade(SQLiteDatabase db,int-oldVersion,int-newVersion){ Log.w(SQLiteAdapter.class.getName(), 将数据库从版本“+oldVersion+”升级到” +新版本+”,将销毁所有旧数据); db.execSQL(“如果存在删除表”+创建表); onCreate(db); } }
    OfflineDataService.java

    package com.remote.synchronizer.haris;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Timer;
    import java.util.TimerTask;
    import org.apache.http.NameValuePair;
    
    import android.app.IntentService;
    import android.content.Intent;
    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.HandlerThread;
    import android.util.Log;
    
    public class OfflineDataService extends IntentService {
    
        boolean wifi,edge;
        private Timer timer= new Timer();
        SQLiteDatabase db;
        String un,shop,city,date,order;
        private SQLiteAdapter mySQLiteAdapter;
        Cursor cursor;
    
        public OfflineDataService() {
            super("OfflineDataService");
        }
    
        @Override   
        public void onCreate() {
    
            super.onCreate();
    
            Log.e("Service", "Service Created.. ");
    
            mySQLiteAdapter = new SQLiteAdapter(this);
            mySQLiteAdapter.getWritableDatabase();
    
        }   
    
        @Override
        protected void onHandleIntent(final Intent intent) {
    
            final Handler threadHandler;  
            threadHandler = new Handler();
    
            new Thread(new Runnable(){
    
                @Override
                public void run() {
    
                    Bundle bundle=intent.getExtras();
                    un=bundle.getString("un");
                    shop=bundle.getString("shop");
                    city=bundle.getString("city");
                    date=bundle.getString("date");
                    order=bundle.getString("order");
    
                    Log.e("Service Started", "Successful");
    
                    //Inserting New Record
                    mySQLiteAdapter.insert(un,shop,city,date,order);
    
                    timer.scheduleAtFixedRate(new TimerTask(){
    
                        @Override
                        public void run() {
    
                            //Checking network connectivity
                            wifi=NetworkInfo.Wifi(OfflineDataService.this);
                            edge=NetworkInfo.EDGE(OfflineDataService.this);
    
                            if(wifi==true||edge==true)
                            {
                                Log.e("Service Network", "Network is online");
                                List<NameValuePair> contacts=new ArrayList<NameValuePair>();
    
                                contacts=mySQLiteAdapter.getAllContacts();
    
                                String url="http://10.0.2.2:3325/Product/Create?"; 
    
                                int response = 0;
    
                                try 
                                {
                                    response = CustomHttpClient.executeHttpPost(url, contacts);
    
                                    if(response==200)
                                    {
                                        Log.e("Data Sent", "Response 200");
                                    }
    
                                    else{
    
                                        Log.e("Service Data", "Faield to upload data" );
                                    }
    
                                }
                                catch (Exception e)
                                {
                                    Log.e("Data Sending", e.toString());
                                    e.printStackTrace();
                                }
                            }
                            else
                            {
                                Log.e("Service Network", "Network is offline");
                            }
                        }
    
                    }, 1000, 5000);
    
                }
    
            }).start();
        }
    
        /*@Override
        public void onDestroy() {
    
            super.onDestroy();
          //  mySQLiteAdapter.deleteAll();
            Log.e("Service Destroy", "Successful");
    
    
        }*/
    }
    
    包com.remote.synchronizer.haris;
    导入java.util.ArrayList;
    导入java.util.List;
    导入java.util.Timer;
    导入java.util.TimerTask;
    导入org.apache.http.NameValuePair;
    导入android.app.IntentService;
    导入android.content.Intent;
    导入android.database.Cursor;
    导入android.database.sqlite.SQLiteDatabase;
    导入android.os.Bundle;
    导入android.os.Handler;
    导入android.os.HandlerThread;
    导入android.util.Log;
    公共类OfflineDataService扩展了IntentService{
    布尔wifi,边缘;
    专用计时器=新计时器();
    sqlitedb数据库;
    字符串un、商店、城市、日期、订单;
    私有SQLiteAdapter mySQLiteAdapter;
    光标;
    公众的
    
    sqLiteHelper.getReadableDatabase();
    Cursor cursor = sqLiteDatabase.rawQuery(selectQuery, null);
    
    if(sqLiteDatabase == null || !sqLiteDatabase.isOpen()) {
        sqLiteDatabase = sqLiteHelper.getReadableDatabase();
    }
    Cursor cursor = sqLiteDatabase.rawQuery(selectQuery, null);
    
    public SQLiteAdapter(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);  
        sqLiteDatabase = this.getWritableDatabase();
    }