gpt4 book ai didi

java - 应用程序在调试时停止

转载 作者:行者123 更新时间:2023-11-30 01:58:48 24 4
gpt4 key购买 nike

每次我调试我的项目时,我的设备都会显示我的应用程序已停止的对话框。这是我的代码。我不知道我的代码有什么问题。

public class Nasabah extends Activity {
DatabaseHelper db = new DatabaseHelper(this);
ArrayList<HashMap<String,String>> contactList;
private static final String TAG_NAME = "name";

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nasabah);

final ListView listNasabah = (ListView) findViewById(R.id.listNasabah);

Log.d("Insert: ", "Inserting...");
db.addContact(new Contact("Jose Mourinho", "087888888888", "1088994444"));
db.addContact(new Contact("Didier Drogba", "0878555555555", "1088996666"));
db.addContact(new Contact("Eden Hazard", "0878511111111", "1088995555"));

Log.d("Reading: ", "Reading all contacts...");
List<Contact> contact = db.getAllContacts();

contactList = new ArrayList<HashMap<String, String>>();
for (Contact cn : contact){
String log = "Id :"+cn.getId()+" ,Name: "+cn.getName()+" ,Telp: "+cn.getTelp()+" ,Rek: "+cn.getRek();
Log.d("Name: ", log);

String name = "" + cn.getName();
HashMap <String, String> contacts = new HashMap<String, String>();

contacts.put(TAG_NAME, name);
contactList.add(contacts);
}

SimpleAdapter adapter = new SimpleAdapter(this, contactList, android.R.layout.simple_list_item_1, new String[]{TAG_NAME}, new int[]{android.R.id.text1});
listNasabah.setAdapter(adapter);

listNasabah.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
final String item = (String) parent.getItemAtPosition(position);
}
});
}}

这是我的databasehelper.java:

public class DatabaseHelper extends SQLiteOpenHelper{
private static final String DB_NAME = "BMIcontact.db";
private static final int DB_VER = 1;
private static final String TABLE_NAME = "contact";
private static final String COL_ID = "id_nasabah";
private static final String COL_NAME = "nama_nasabah";
private static final String COL_TELP = "telp";
private static final String COL_REK = "rekening";
public DatabaseHelper (Context context){
super(context, DB_NAME, null, DB_VER);
//super(context, Environment.getExternalStorageDirectory() + File.separator + "/DataBase/" + File.separator + DB_NAME, null, DB_VER);
}

@Override
public void onCreate(SQLiteDatabase db){
String CREATE_CONTACT_TABLE = "CREATE TABLE" + TABLE_NAME + "(" + COL_ID + "INTEGER PRIMARY KEY," + COL_NAME
+ "TEXT," + COL_TELP + "TELP," + COL_REK + "TEXT" + ")";
db.execSQL(CREATE_CONTACT_TABLE);
}
@Override
public void onUpgrade (SQLiteDatabase db, int oldVersion, int NewVersion){
db.execSQL("DROP TABLE IF EXIST" + TABLE_NAME);
onCreate(db);
}

void addContact (Contact contact){
SQLiteDatabase db = this.getWritableDatabase();

ContentValues values = new ContentValues();
values.put(COL_NAME, contact.getName());
values.put(COL_TELP, contact.getTelp());
values.put(COL_REK, contact.getRek());

db.insert(TABLE_NAME, null, values);
db.close();
}
Contact getContact (int id){
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_NAME, new String[] { COL_ID, COL_NAME, COL_TELP, COL_REK}, COL_ID + "=?",
new String[] {String.valueOf(id) }, null, null, null, null);
if(cursor != null){
cursor.moveToFirst();
}
Contact contact = new Contact(Integer.parseInt(cursor.getString(0)), cursor.getString(1), cursor.getString(2), cursor.getString(3));
return contact;
}

public List<Contact> getAllContacts() {
List<Contact> contactList = new ArrayList<Contact>();
String selectQuery = "SELECT * FROM " + TABLE_NAME;

SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);

if(cursor.moveToFirst()){
do{
Contact contact = new Contact();
contact.setId(Integer.parseInt(cursor.getString(0)));
contact.setName(cursor.getString(1));
contact.setTelp(cursor.getString(2));
contact.setRek(cursor.getString(3));

contactList.add(contact);
}while(cursor.moveToNext());
}
return contactList;
}

public int getContactsCount(){
String countQuery = "SELECT * FROM " + TABLE_NAME;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
cursor.close();

return cursor.getCount();
}}

最佳答案

永远不要忘记 sql 关键字和表/列名之间的空格:

"CREATE TABLE " + TABLE_NAME + "(" + COL_ID + " INTEGER PRIMARY KEY," + COL_NAME
+ " TEXT," + COL_TELP + " TEXT," + COL_REK + " TEXT" + ")";


db.execSQL("DROP TABLE IF EXIST " + TABLE_NAME);

或者您认为这可以执行吗?

CREATE TABLEcontact

关于java - 应用程序在调试时停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31806552/

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