当移动设备不在覆盖区域时,我想存储一些数据。服务恢复后,它将在服务器上上传数据。但是没有插入数据。我在网上检查了许多tutorail。但一直都一样。应用程序意外关闭。

LogCat错误

        10-12 12:08:54.754: E/Service Example(1374): Service Started..
        10-12 12:08:54.814: E/AndroidRuntime(1374): FATAL EXCEPTION: main
        10-12 12:08:54.814: E/AndroidRuntime(1374): java.lang.RuntimeException: Unable    create service com.remote.synchronizer.haris.OfflineDataService:  java.lang.NullPointerException
        10-12 12:08:54.814: E/AndroidRuntime(1374):     at android.app.ActivityThread.handleCreateService(ActivityThread.java:1955)
        10-12 12:08:54.814: E/AndroidRuntime(1374):     at android.app.ActivityThread.access$2500(ActivityThread.java:117)
        10-12 12:08:54.814: E/AndroidRuntime(1374):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:985)
        10-12 12:08:54.814: E/AndroidRuntime(1374):     at android.os.Handler.dispatchMessage(Handler.java:99)
        10-12 12:08:54.814: E/AndroidRuntime(1374):     at android.os.Looper.loop(Looper.java:123)
        10-12 12:08:54.814: E/AndroidRuntime(1374):     at android.app.ActivityThread.main(ActivityThread.java:3683)
        10-12 12:08:54.814: E/AndroidRuntime(1374):     at java.lang.reflect.Method.invokeNative(Native Method)
        10-12 12:08:54.814: E/AndroidRuntime(1374):     at java.lang.reflect.Method.invoke(Method.java:507)
        10-12 12:08:54.814: E/AndroidRuntime(1374):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
        10-12 12:08:54.814: E/AndroidRuntime(1374):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
        10-12 12:08:54.814: E/AndroidRuntime(1374):     at dalvik.system.NativeStart.main(Native Method)
        10-12 12:08:54.814: E/AndroidRuntime(1374): Caused by: java.lang.NullPointerException
        10-12 12:08:54.814: E/AndroidRuntime(1374):     at com.remote.synchronizer.haris.SQLiteAdapter.Write(SQLiteAdapter.java:59)
        10-12 12:08:54.814: E/AndroidRuntime(1374):     at com.remote.synchronizer.haris.OfflineDataService.onCreate(OfflineDataService.java:37)
        10-12 12:08:54.814: E/AndroidRuntime(1374):     at android.app.ActivityThread.handleCreateService(ActivityThread.java:1945)
        10-12 12:08:54.814: E/AndroidRuntime(1374):     ... 10 more


SQLiteAdapter

    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 = "Order";
    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 = "date";
    private static final String KEY_ORDER = "order";


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

    private SQLiteDatabase sqLiteDatabase;
    private SQLiteAdapter sqLiteHelper;

    /*private Context context;

    public SQLiteAdapter(Context c){
        context = c;
    }
    */
    public void Read() throws android.database.SQLException {
        sqLiteDatabase = sqLiteHelper.getReadableDatabase();
    }

    public void Write() throws android.database.SQLException {
        sqLiteDatabase = sqLiteHelper.getWritableDatabase();
    }

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

    public long insert(String name, String shop, String city, String date, String order){

        ContentValues contentValues = new ContentValues();
        contentValues.put(KEY_NAME, name);
        contentValues.put(KEY_SHOP, shop);
        contentValues.put(KEY_SHOP, city);
        contentValues.put(KEY_DATE, date);
        contentValues.put(KEY_ORDER, order);
        return sqLiteDatabase.insert(TABLE_NAME, null, contentValues);
    }

    public int deleteAll(){
        return 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;
        Read();
        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);
    }

}


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.util.Log;
    import android.widget.Toast;

    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 Example", "Service Started.. ");

         mySQLiteAdapter = new SQLiteAdapter(this);
         mySQLiteAdapter.Write();

}

    @Override
    protected void onHandleIntent(Intent intent) {

        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)
                       {
                           DataSender();
                       }
                       else
                       {
                           //Toast toast=Toast.makeText(getApplicationContext(), "Testing", toast.LENGTH_LONG).show();
                       }
            }

        }, 1000, 5000);


    }

    /*@Override
    public void onDestroy() {

        super.onDestroy();
        Log.e("Service Example", "Service Destroyed.. ");
        mySQLiteAdapter.deleteAll();


    }*/

    private void DataSender()
    {
        timer.cancel();

        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)
                {
                    //notification
                }

                else{}
                    //error.setText("Sorry!! Try again later.\n Response Code: "+ response);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            //  error.setText(e.toString());
            }
    }

}


我的代码有什么问题?

日志猫中的新错误

10-12 14:57:00.424: E/Service Example(1494): Service Started..
10-12 14:57:00.664: E/Database(1494): Failure 1 (near "Order": syntax error) on 0x5b7638 when preparing 'CREATE TABLE Order (_id integer primary key autoincrement, name VARCHAR,shop VARCHAR,city VARCHAR, date VARCHAR, order VARCHAR );'.
10-12 14:57:00.764: E/AndroidRuntime(1494): FATAL EXCEPTION: main
10-12 14:57:00.764: E/AndroidRuntime(1494): java.lang.RuntimeException: Unable to create service com.remote.synchronizer.haris.OfflineDataService: android.database.sqlite.SQLiteException: near "Order": syntax error: CREATE TABLE Order (_id integer primary key autoincrement, name VARCHAR,shop VARCHAR,city VARCHAR, date VARCHAR, order VARCHAR );
10-12 14:57:00.764: E/AndroidRuntime(1494):     at android.app.ActivityThread.handleCreateService(ActivityThread.java:1955)
10-12 14:57:00.764: E/AndroidRuntime(1494):     at android.app.ActivityThread.access$2500(ActivityThread.java:117)
10-12 14:57:00.764: E/AndroidRuntime(1494):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:985)
10-12 14:57:00.764: E/AndroidRuntime(1494):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-12 14:57:00.764: E/AndroidRuntime(1494):     at android.os.Looper.loop(Looper.java:123)
10-12 14:57:00.764: E/AndroidRuntime(1494):     at android.app.ActivityThread.main(ActivityThread.java:3683)
10-12 14:57:00.764: E/AndroidRuntime(1494):     at java.lang.reflect.Method.invokeNative(Native Method)
10-12 14:57:00.764: E/AndroidRuntime(1494):     at java.lang.reflect.Method.invoke(Method.java:507)
10-12 14:57:00.764: E/AndroidRuntime(1494):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-12 14:57:00.764: E/AndroidRuntime(1494):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-12 14:57:00.764: E/AndroidRuntime(1494):     at dalvik.system.NativeStart.main(Native Method)
10-12 14:57:00.764: E/AndroidRuntime(1494): Caused by: android.database.sqlite.SQLiteException: near "Order": syntax error: CREATE TABLE Order (_id integer primary key autoincrement, name VARCHAR,shop VARCHAR,city VARCHAR, date VARCHAR, order VARCHAR );
10-12 14:57:00.764: E/AndroidRuntime(1494):     at android.database.sqlite.SQLiteDatabase.native_execSQL(Native Method)
10-12 14:57:00.764: E/AndroidRuntime(1494):     at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1763)
10-12 14:57:00.764: E/AndroidRuntime(1494):     at com.remote.synchronizer.haris.SQLiteAdapter.onCreate(SQLiteAdapter.java:42)
10-12 14:57:00.764: E/AndroidRuntime(1494):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:126)
10-12 14:57:00.764: E/AndroidRuntime(1494):     at com.remote.synchronizer.haris.SQLiteAdapter.Write(SQLiteAdapter.java:59)
10-12 14:57:00.764: E/AndroidRuntime(1494):     at com.remote.synchronizer.haris.OfflineDataService.onCreate(OfflineDataService.java:37)
10-12 14:57:00.764: E/AndroidRuntime(1494):     at android.app.ActivityThread.handleCreateService(ActivityThread.java:1945)
10-12 14:57:00.764: E/AndroidRuntime(1494):     ... 10 more

最佳答案

您定义sqLiteHelper,但不要初始化它。这意味着当下面的行(在您的Write()方法中)运行时,它将导致logcat中显示NullPointerException

sqLiteDatabase = sqLiteHelper.getWritableDatabase();


更改为...

sqLiteDatabase = this.getWritableDatabase();


..或确保在使用前将sqLiteHelper初始化。

注意:就目前情况而言,除非您初始化Read()或我们sqLiteHelper,否则您的this方法将以相同的方式失败。

10-07 22:18