我在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
。
尝试初始化eg:DatabaseHelper DatabaseHelper=new DatabaseHelper();