将上下文从fragment传递到SQLiteOpenHelper类时出现NullPointerException

时间:2017-08-28 10:12:56

标签: android nullpointerexception

此代码在API级别21中完美运行,但它在API级别19中不起作用。它产生NullPointerException。

我已经在Google上检查了很多与此相关的问题,但它们没有帮助。

我从子片段调用SQliteOpenHelper,所以这是我的代码有错误:

// This is my Chatbox class which a childfragment and
// I am calling this method to pass values in sqlite class

 private void saveConversation(String profile_name,String profile_id,String message,String who){
    sqLite = new SQLite(getContext());
    SQLiteDatabase sqLiteDatabase = sqLite.getWritableDatabase();
    sqLite.onCreate(sqLiteDatabase);
    sqLite.insert(profile_name, profile_id, message, who);
}


//Sqlite class
public class SQLite extends SQLiteOpenHelper {
     SQLiteDatabase sqLiteDatabase  =  this.getWritableDatabase();
     ContentValues contentValues;
    Context context;

    public static final String DatabaseName = "Communication";
    public static final String TableName1  = "ChatConversation";
    public static final int DatabaseVersion = 2;

    public SQLite(Context context) {
        super(context, DatabaseName, null, DatabaseVersion);
        this.context = context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
            Toast.makeText(context,"on create table is called !!",Toast.LENGTH_SHORT).show();
            db.execSQL("CREATE TABLE IF NOT EXISTS "+TableName1+"(_id INTEGER PRIMARY KEY AUTOINCREMENT,profilename VARCHAR(50),profileid VARCHAR(50),message VARCHAR(255),who VARCHAR(20))");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    public void insert(String profilename,String profileid ,String message, String who){
        Toast.makeText(context,"insert database was called",Toast.LENGTH_LONG).show();
        contentValues = new ContentValues();
        contentValues.put("profilename",profilename);
        contentValues.put("profileid",profileid);
        contentValues.put("message",message);
        contentValues.put("who",who);
        long id = sqLiteDatabase.insert(SQLite.TableName1,null,contentValues);
        if(id>0){
           Toast.makeText(context,"successfully inserted", LENGTH_LONG).show();
        }else{
            Toast.makeText(context,"Something went wrong", LENGTH_LONG).show();
        }
    }

}

这是我的错误:

com.example.shuresnepali.communicationpost, PID: 21374
   java.lang.NullPointerException
   at android.widget.Toast.<init>(Toast.java:117)
   at android.widget.Toast.makeText(Toast.java:275)
   at com.example.shuresnepali.communicationpost.SQLite.onCreate(SQLite.java:28)
   at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
   at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
   at com.example.shuresnepali.communicationpost.SQLite.<init>(SQLite.java:13)
   at com.example.shuresnepali.communicationpost.Chat_box.saveConversation(Chat_box.java:283)
   at com.example.shuresnepali.communicationpost.Chat_box.onClick(Chat_box.java:149)
   at android.view.View.performClick(View.java:4652)
   at android.view.View$PerformClick.run(View.java:19318)
   at android.os.Handler.handleCallback(Handler.java:733)
   at android.os.Handler.dispatchMessage(Handler.java:95)
   at android.os.Looper.loop(Looper.java:146)
   at android.app.ActivityThread.main(ActivityThread.java:5641)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:515)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1288)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1104)
   at dalvik.system.NativeStart.main(Native Method)

4 个答案:

答案 0 :(得分:1)

尝试在片段中使用getActivity()来获取Context

private void saveConversation(String profile_name,String profile_id,String message,String who){
        sqLite = new SQLite(getParentFragment().getActivity());
        SQLiteDatabase sqLiteDatabase = sqLite.getWritableDatabase();
        sqLite.onCreate(sqLiteDatabase);
        sqLite.insert(profile_name, profile_id, message, who);
    }

答案 1 :(得分:1)

使用getActivity()获取片段中的Context;

 private void saveConversation(String profile_name,String profile_id,String message,String who){
        sqLite = new SQLite(getActivity());
        SQLiteDatabase sqLiteDatabase = sqLite.getWritableDatabase();
        sqLite.onCreate(sqLiteDatabase);
        sqLite.insert(profile_name, profile_id, message, who);
    }

答案 2 :(得分:1)

public class SQLite extends SQLiteOpenHelper {
      SQLiteDatabase sqLiteDatabase  =  this.getWritableDatabase();

你过早地打电话给getWritableDatabase()。在Java中,成员初始值设定项在构造函数体之前运行(除了显式超级构造函数调用之外),并且只是在构造函数体中初始化contextgetWritableDatabase()依次回拨onCreate(),您正在使用尚未初始化的context字段。进一步阅读:Java order of Initialization and Instantiation

删除此字段,或将其初始化推迟到构造函数体。

在相关说明中,您不应自己致电onCreate()

为什么它在API 21上“工作”而不是19可能是因为在您的API 21设备/模拟器上,您已经拥有一个具有相同名称的数据库文件,使用早期版本的应用程序创建,没有此错误。如果数据库文件不存在,getWritableDatabase()仅回调onCreate()When is SQLiteOpenHelper onCreate() / onUpgrade() run?

答案 3 :(得分:0)

感谢大家的帮助我通过...在插入数据期间传递上下文并从初始化中删除了getwriteabledatabase来解决这个问题

 private void saveConversation(String profile_name,String profile_id,String message,String who){
        sqLite = new SQLite(getParentFragment().getContext());
        SQLiteDatabase sqLiteDatabase = sqLite.getWritableDatabase();
        sqLite.onCreate(sqLiteDatabase);
        sqLite.insert(profile_name, profile_id, message, who,sqLiteDatabase);
    }
//and after this
 public void insert(String profilename,String profileid ,String message, String who,SQLiteDatabase sqLiteDatabase){
        Toast.makeText(context,"insert database was called",Toast.LENGTH_LONG).show();
        contentValues = new ContentValues();
        contentValues.put("profilename",profilename);
        contentValues.put("profileid",profileid);
        contentValues.put("message",message);
        contentValues.put("who",who);
        long id = sqLiteDatabase.insert(SQLite.TableName1,null,contentValues);
        if(id>0){
           Toast.makeText(context,"successfully inserted", LENGTH_LONG).show();
        }else{
            Toast.makeText(context,"Something went wrong", LENGTH_LONG).show();
        }
    }