java - 当我编写 getwriteable 数据库以在 Sqlite 数据库中存储数据时,应用程序关闭

标签 java sqlite android-sqlite

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

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.

最佳答案

您的问题是传递的上下文为空。您需要变量上下文位于 onClickListener 的范围内,或者如下所示从通过 getContext() 方法单击的 View 中获取上下文。

您还需要变量 sqLiteDatabase 成为有效的 SQLiteDatabase 对象,因此第二行分配通过上一行中实例化的 Helper 对象的帮助程序实例检索的 SQLiteDatabase。

尝试改变

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

Savedata方法中

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

关于java - 当我编写 getwriteable 数据库以在 Sqlite 数据库中存储数据时,应用程序关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56921295/

相关文章:

c++ - 是否推荐使用 PRAGMA synchronous=OFF 来提高性能

ios - 在 NSString 中写入 %

sqlite - 访问 Lollipop 上的启动器数据库

android - 这是在 android 的本地内存中存储自定义数组列表的更好方法

java - Android 应用程序在运行时崩溃 : 'java.lang.String android.content.Context.getPackageName()'

java - 无法运行类文件

java - 忽略非扩展类 JPA 的 @MappedSuperclass

java - 是否可以确定 View 列是否包含常量数据或仅包含 UI 数据?

android - 发送带有数据库的应用程序

Python sqlite3错误