当我为SQLite数据库中的存储数据编写可写数据库时,应用程序关闭

时间:2019-07-07 10:24:31

标签: java sqlite android-sqlite

我正在尝试将数据存储在Sqlite数据库中。我创建了一个扩展Sqlite openhelper类的帮助器类。 我写了所有代码来创建数据库表和列。 在我的 MainActivity 中单击保存按钮时,我调用savedata方法。 当我为可写数据库编写代码时发生了问题。 应用未响应并关闭。

public class ClientDetailsActivity extends AppCompatActivity {
     EditText client_name, client_address, client_mobile_number, client_email_id, policy_name, policy_number;
     Button save, edit, delete;
     ListView listView;
     SQLiteDatabase sqLiteDatabase;
     Context context;

     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_client_details);

         client_name = findViewById(R.id.client_name);
         client_address = findViewById(R.id.client_address);
         client_mobile_number = findViewById(R.id.client_mobile_number);
         client_email_id = findViewById(R.id.client_email_id);
         policy_name = findViewById(R.id.et_policy_name);
         policy_number = findViewById(R.id.et_policy_number);
         save = findViewById(R.id.btn_save);
         edit = findViewById(R.id.btn_edit);
         delete = findViewById(R.id.btn_delete);
         listView = findViewById(R.id.list);




     }

     public void Savedata(View view) {
        Helper helper=new Helper(context);
        helper.getWritableDatabase();
         ContentValues values=new ContentValues();
         values.put(Helper.COL_CLIENT_NAME,client_name.getText().toString());
         values.put(Helper.COL_CLIENT_ADDRESS,client_address.getText().toString());
         values.put(Helper.COL_CLIENT_MOBILE_NUMBER,client_mobile_number.getText().toString());
         values.put(Helper.COL_CLIENT_EMAIL_ID,client_mobile_number.getText().toString());
         values.put(Helper.COL_POLICY_NAME,policy_name.getText().toString());
         values.put(Helper.COL_POLICY_NUMBER,policy_number.getText().toString());

        String clientname=client_name.getText().toString();
        if(clientname.equals("")) {
            long id = sqLiteDatabase.insert(Helper.CLIENT_TABLE,null,values);

            Toast.makeText(context,"ID"+id,Toast.LENGTH_LONG).show();

        }
     }
 }

错误

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.sqlite.SQLiteDatabase android.content.Context.openOrCreateDatabase(java.lang.String, int, android.database.sqlite.SQLiteDatabase$CursorFactory, android.database.DatabaseErrorHandler)' on a null object reference
        at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:223)
        at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
        at com.example.smartagent.ClientDetailsActivity$override.Savedata(ClientDetailsActivity.java:46)
        at com.example.smartagent.ClientDetailsActivity$override.access$dispatch(ClientDetailsActivity.java)
        at com.example.smartagent.ClientDetailsActivity.Savedata(ClientDetailsActivity.java)
        at java.lang.reflect.Method.invoke(Native Method) 
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385) 
        at android.view.View.performClick(View.java:5198) 
        at android.view.View$PerformClick.run(View.java:21147) 
        at android.os.Handler.handleCallback(Handler.java:739) 
        at android.os.Handler.dispatchMessage(Handler.java:95) 
        at android.os.Looper.loop(Looper.java:148) 
        at android.app.ActivityThread.main(ActivityThread.java:5417) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
I/Process: Sending signal. PID: 10482 SIG: 9
Application terminated.

1 个答案:

答案 0 :(得分:0)

您的问题是所传递的上下文为空。您要么需要将变量上下文放在onClickListener的范围内,要么如下所示,从通过getContext()方法单击的视图中获取上下文。

您还需要将变量sqLiteDatabase设为有效的SQLiteDatabase对象,因此第二行将分配通过上一步中实例化的通过Helper对象的helper实例检索到的SQLiteDatabase。

尝试更改

Helper helper=new Helper(context);
helper.getWritableDatabase();

保存数据方法中

Helper helper=new Helper(view.getContext());
SQLiteDatabase sqLiteDatabase = helper.getWritableDatabase();