Android getWritableDatabase()错误

Android getWritableDatabase()错误,android,sqlite,Android,Sqlite,我想在手机不在覆盖区域时存储一些数据。当服务返回时,它将在服务器上上传数据。但数据并没有插入。我在网上查了很多全家教。但一直都是一样的。应用程序异常关闭 LogCat错误 SQLiteAdapter 您定义了sqLiteHelper,但从未初始化它。这意味着当下面的这一行(在Write()方法中)运行时,它会导致logcat中显示的NullPointerException sqLiteDatabase = sqLiteHelper.getWritableDatabase(); 把它改成 sqL

我想在手机不在覆盖区域时存储一些数据。当服务返回时,它将在服务器上上传数据。但数据并没有插入。我在网上查了很多全家教。但一直都是一样的。应用程序异常关闭

LogCat错误

SQLiteAdapter


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

sqLiteDatabase = sqLiteHelper.getWritableDatabase();
把它改成

sqLiteDatabase = this.getWritableDatabase();
..或确保
sqLiteHelper
在使用前已初始化

注意:目前,您的
Read()
方法将以同样的方式失败,除非您初始化
sqLiteHelper
或使用us
this

尝试在Write()和Read()方法中使用此方法

sqLiteDatabase = this.getWritableDatabase(); 
而不是这个

sqLiteDatabase = sqLiteHelper.getWritableDatabase(); 
或者像这样初始化sqLiteHelper

sqLiteHelper = this;

创建一个seprate函数并尝试此操作,您只需要定义一个私有静态SQLiteDatabase db来删除错误

DBhelper.java

 package com.example.mitul.jsontosqlite.sampledata;


 import android.content.Context;
 import android.content.res.Resources;
 import android.database.sqlite.SQLiteDatabase;
 import android.database.sqlite.SQLiteOpenHelper;
 import android.util.Log;
 import android.content.ContentValues;
 import android.widget.Toast;

 import com.example.mitul.jsontosqlite.MainActivity;
 import com.example.mitul.jsontosqlite.R;

 import org.json.JSONArray;
 import org.json.JSONException;
 import org.json.JSONObject;

 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;


public class DBhelper extends SQLiteOpenHelper {
    private static final String TAG = DBhelper.class.getSimpleName();
    SQLiteOpenHelper sqLiteOpenHelper;
    private Resources mResources;
    private static final String DATABASE_NAME = "menu.db";
    private static final int DATABASE_VERSION = 1;
    Context context;
    private static SQLiteDatabase db;

    public DBhelper(Context context){
            super(context,DATABASE_NAME,null,DATABASE_VERSION);
            mResources = context.getResources();
            db = this.getWritableDatabase();
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
            final String SQL_CREATE_BUGS_TABLE = "CREATE TABLE " + Dbcontract.MenuEntry.TABLE_NAME + " (" +
                    Dbcontract.MenuEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
                    Dbcontract.MenuEntry.COLUMN_NAME + " TEXT UNIQUE NOT NULL, " +
                    Dbcontract.MenuEntry.COLUMN_DESCRIPTION + " TEXT NOT NULL, " +
                    Dbcontract.MenuEntry.COLUMN_PRICE + " TEXT NOT NULL, " +
                    Dbcontract.MenuEntry.COLUMN_CATEGORY + " TEXT NOT NULL, " +
                    Dbcontract.MenuEntry.COLUMN_PHOTO + " INTEGER NOT NULL " + " );";


            db.execSQL(SQL_CREATE_BUGS_TABLE);
            Log.d(TAG, "Database Created Successfully" );
            try {

                    readDataToDb(db);
            } catch (IOException e) {
                    e.printStackTrace();
            } catch (JSONException e) {
                    e.printStackTrace();
            }
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }

    private void readDataToDb(SQLiteDatabase db) throws IOException, JSONException {


            final String MNU_NAME = "name";
            final String MNU_DESCRIPTION = "description";
            final String MNU_PRICE = "price";
            final String MNU_CATEGORY = "category";
            final String MNU_PHOTO = "photo";

            try {
                    String jsonDataString = readJsonDataFromFile();
                    JSONArray menuItemsJsonArray = new JSONArray(jsonDataString);

                    for (int i = 0; i < menuItemsJsonArray.length(); ++i) {

                            String name;
                            String description;
                            String price;
                            String category;
                            String photo;


                            JSONObject menuItemObject = menuItemsJsonArray.getJSONObject(i);


                            name = menuItemObject.getString(MNU_NAME);
                            description = menuItemObject.getString(MNU_DESCRIPTION);
                            price = menuItemObject.getString(MNU_PRICE);
                            category = menuItemObject.getString(MNU_CATEGORY);
                            photo = menuItemObject.getString(MNU_PHOTO);


                            ContentValues menuValues = new ContentValues();

                            menuValues.put(Dbcontract.MenuEntry.COLUMN_NAME, name);
                            menuValues.put(Dbcontract.MenuEntry.COLUMN_DESCRIPTION, description);
                            menuValues.put(Dbcontract.MenuEntry.COLUMN_PRICE, price);
                            menuValues.put(Dbcontract.MenuEntry.COLUMN_CATEGORY, category);
                            menuValues.put(Dbcontract.MenuEntry.COLUMN_PHOTO, photo);

                            db.insert(Dbcontract.MenuEntry.TABLE_NAME, null, menuValues);


                            Log.d(TAG, "Inserted Successfully " + menuValues );

                    }


            } catch (JSONException e) {
                    Log.e(TAG, e.getMessage(), e);
                    e.printStackTrace();
            }

    }

    private String readJsonDataFromFile() throws IOException {

            InputStream inputStream = null;
            StringBuilder builder = new StringBuilder();

            try {
                    String jsonDataString = null;
                    inputStream = mResources.openRawResource(R.raw.menu_item);
                    BufferedReader bufferedReader = new BufferedReader(
                            new InputStreamReader(inputStream, "UTF-8"));
                    while ((jsonDataString = bufferedReader.readLine()) != null) {
                            builder.append(jsonDataString);
                    }
            } finally {
                    if (inputStream != null) {
                            inputStream.close();
                    }
            }

            return new String(builder);
    }
 }
package com.example.mitul.jsontosqlite.sampledata;

 import android.provider.BaseColumns;



 public class Dbcontract {

   public static final class MenuEntry implements BaseColumns{

    public static final String TABLE_NAME ="menu";
    public static final String COLUMN_NAME ="name";
    public static final String COLUMN_DESCRIPTION ="description";
    public static final String COLUMN_PRICE ="price";
    public static final String COLUMN_CATEGORY ="category";
    public static final String COLUMN_PHOTO ="photo";



           }

   }

您必须从DataHelper创建一个新对象,如下所示:

DatabaseHelper helper = new DataHelper(this);//write this in your MainActivity class

我初始化了sqLiteHelper,但getWritableDatabase错误仍然存在。但是现在有一些错误语法@SQUONK任何人都有..幸运吗?请为askers的好处添加一些解释。如何
确保sqLiteHelper在使用前已初始化。
sqLiteHelper = this;
 package com.example.mitul.jsontosqlite.sampledata;


 import android.content.Context;
 import android.content.res.Resources;
 import android.database.sqlite.SQLiteDatabase;
 import android.database.sqlite.SQLiteOpenHelper;
 import android.util.Log;
 import android.content.ContentValues;
 import android.widget.Toast;

 import com.example.mitul.jsontosqlite.MainActivity;
 import com.example.mitul.jsontosqlite.R;

 import org.json.JSONArray;
 import org.json.JSONException;
 import org.json.JSONObject;

 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;


public class DBhelper extends SQLiteOpenHelper {
    private static final String TAG = DBhelper.class.getSimpleName();
    SQLiteOpenHelper sqLiteOpenHelper;
    private Resources mResources;
    private static final String DATABASE_NAME = "menu.db";
    private static final int DATABASE_VERSION = 1;
    Context context;
    private static SQLiteDatabase db;

    public DBhelper(Context context){
            super(context,DATABASE_NAME,null,DATABASE_VERSION);
            mResources = context.getResources();
            db = this.getWritableDatabase();
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
            final String SQL_CREATE_BUGS_TABLE = "CREATE TABLE " + Dbcontract.MenuEntry.TABLE_NAME + " (" +
                    Dbcontract.MenuEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
                    Dbcontract.MenuEntry.COLUMN_NAME + " TEXT UNIQUE NOT NULL, " +
                    Dbcontract.MenuEntry.COLUMN_DESCRIPTION + " TEXT NOT NULL, " +
                    Dbcontract.MenuEntry.COLUMN_PRICE + " TEXT NOT NULL, " +
                    Dbcontract.MenuEntry.COLUMN_CATEGORY + " TEXT NOT NULL, " +
                    Dbcontract.MenuEntry.COLUMN_PHOTO + " INTEGER NOT NULL " + " );";


            db.execSQL(SQL_CREATE_BUGS_TABLE);
            Log.d(TAG, "Database Created Successfully" );
            try {

                    readDataToDb(db);
            } catch (IOException e) {
                    e.printStackTrace();
            } catch (JSONException e) {
                    e.printStackTrace();
            }
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }

    private void readDataToDb(SQLiteDatabase db) throws IOException, JSONException {


            final String MNU_NAME = "name";
            final String MNU_DESCRIPTION = "description";
            final String MNU_PRICE = "price";
            final String MNU_CATEGORY = "category";
            final String MNU_PHOTO = "photo";

            try {
                    String jsonDataString = readJsonDataFromFile();
                    JSONArray menuItemsJsonArray = new JSONArray(jsonDataString);

                    for (int i = 0; i < menuItemsJsonArray.length(); ++i) {

                            String name;
                            String description;
                            String price;
                            String category;
                            String photo;


                            JSONObject menuItemObject = menuItemsJsonArray.getJSONObject(i);


                            name = menuItemObject.getString(MNU_NAME);
                            description = menuItemObject.getString(MNU_DESCRIPTION);
                            price = menuItemObject.getString(MNU_PRICE);
                            category = menuItemObject.getString(MNU_CATEGORY);
                            photo = menuItemObject.getString(MNU_PHOTO);


                            ContentValues menuValues = new ContentValues();

                            menuValues.put(Dbcontract.MenuEntry.COLUMN_NAME, name);
                            menuValues.put(Dbcontract.MenuEntry.COLUMN_DESCRIPTION, description);
                            menuValues.put(Dbcontract.MenuEntry.COLUMN_PRICE, price);
                            menuValues.put(Dbcontract.MenuEntry.COLUMN_CATEGORY, category);
                            menuValues.put(Dbcontract.MenuEntry.COLUMN_PHOTO, photo);

                            db.insert(Dbcontract.MenuEntry.TABLE_NAME, null, menuValues);


                            Log.d(TAG, "Inserted Successfully " + menuValues );

                    }


            } catch (JSONException e) {
                    Log.e(TAG, e.getMessage(), e);
                    e.printStackTrace();
            }

    }

    private String readJsonDataFromFile() throws IOException {

            InputStream inputStream = null;
            StringBuilder builder = new StringBuilder();

            try {
                    String jsonDataString = null;
                    inputStream = mResources.openRawResource(R.raw.menu_item);
                    BufferedReader bufferedReader = new BufferedReader(
                            new InputStreamReader(inputStream, "UTF-8"));
                    while ((jsonDataString = bufferedReader.readLine()) != null) {
                            builder.append(jsonDataString);
                    }
            } finally {
                    if (inputStream != null) {
                            inputStream.close();
                    }
            }

            return new String(builder);
    }
 }
package com.example.mitul.jsontosqlite.sampledata;

 import android.provider.BaseColumns;



 public class Dbcontract {

   public static final class MenuEntry implements BaseColumns{

    public static final String TABLE_NAME ="menu";
    public static final String COLUMN_NAME ="name";
    public static final String COLUMN_DESCRIPTION ="description";
    public static final String COLUMN_PRICE ="price";
    public static final String COLUMN_CATEGORY ="category";
    public static final String COLUMN_PHOTO ="photo";



           }

   }
DatabaseHelper helper = new DataHelper(this);//write this in your MainActivity class