gpt4 book ai didi

android - 在 Android app_project 中读写 SQLite 数据库

转载 作者:行者123 更新时间:2023-11-29 21:14:10 24 4
gpt4 key购买 nike

我在 LogCat 上有一个 java.lang.NullPointer 错误,但我不知道为什么。似乎错误在于第二个 OnClick 过程。谁能帮帮我??

package com.example.cinemaodeon;


import com.example.cinemaodeon.Main;



public class Main extends Activity {

private DatabaseHelper DatabaseHelper;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);


setContentView(R.layout.main_layout);

final Toast toast = Toast.makeText(this,
"Il nome utente non è presente!!! registra un nuovo nome utente!",
Toast.LENGTH_LONG);

final Toast toast2 = Toast.makeText(this,
"Il nome utente è già presente!!!Puoi accedere con questo nome utente!",
Toast.LENGTH_LONG);


Button btnGO = (Button) findViewById(R.id.main_btnentra);
btnGO.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {

SQLiteDatabase db = DatabaseHelper.getReadableDatabase();
String[] columns = { "_id_user" };
Cursor cursor = db.query("users", columns, null, null, null, null, null);
String[] result = new String[]{};
int count = cursor.getCount();
while (cursor.moveToNext()) {
result[count]=cursor.getString(0);
count=count+1;
}

EditText editusername =(EditText) findViewById(R.id.main_editusername);
final String username = editusername.getText().toString();

for (int i = 0; i < count; i++) {
if (username==result[count]){

Intent openProgrammazione = new Intent(Main.this,Programmazione.class);
startActivity(openProgrammazione);

}
}
toast.show();
}
});


Button btnReg = (Button) findViewById(R.id.main_btnregistra);
btnReg.setOnClickListener(new OnClickListener(){

似乎错误在下面的 OnClick 过程中,可能是 databaseHelper 的定义有问题

        @Override    
public void onClick(View arg0) {

SQLiteDatabase db = DatabaseHelper.getReadableDatabase();
String[] columns = { "_id_user" };
Cursor cursor = db.query("users", columns, null, null, null, null, null);
String[] result = new String[]{};
int count = cursor.getCount();
while (cursor.moveToNext()) {
result[count]=cursor.getString(0);
count=count+1;
}


EditText editusername =(EditText) findViewById(R.id.main_editusername_registra);
final String username = editusername.getText().toString();


for (int i = 0; i < count; i++) {
if (username==result[count]){

toast2.show();
}
}
SQLiteDatabase dbW = DatabaseHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("_id_user", username);
@SuppressWarnings("unused")
long id = dbW.insert("users", null, values);

Intent openProgrammazione = new Intent(Main.this,Programmazione.class);
startActivity(openProgrammazione);
}

});


}

最佳答案

您还没有初始化 DatabaseHelper DatabaseHelper

尝试初始化它

例如:DatabaseHelper DatabaseHelper=new DatabaseHelper();

关于android - 在 Android app_project 中读写 SQLite 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21878484/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com