当我尝试调试它时,它说:不幸的是你的应用程序停止了 请帮助我,我是开发新手
DataBase Helper Class是
'//div[contains(@class, "field-documento-id_validade")]//input[@type="checkbox"]'
主要活动类是
public class DatabaseHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION=1;
private static final String DATABASE_NAME= "permitManager",
TABLE_CONTACTS ="contacts",
KEY_ID = "id",
KEY_CAR="car",
KEY_STATUS="status",
KEY_IMAGEURI="imageUri";
public DatabaseHandler(Context context){
super(context, DATABASE_NAME,null,DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db){
db.execSQL("CREATE TABLE " + TABLE_CONTACTS +""+ KEY_ID + " INTEGER PRIMARY," + KEY_CAR + "TEXT," + KEY_STATUS + "TEXT," + KEY_IMAGEURI + "TEXT,");
}
@Override
public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion){
db.execSQL("DROP TABLE IF EXISTS"+ TABLE_CONTACTS);
onCreate(db);
}
public void createContact(Contact contact){
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_CAR,contact.getCar());
values.put(KEY_STATUS,contact.getStatus());
values.put(KEY_IMAGEURI, contact.getimageURI().toString());
db.insert(TABLE_CONTACTS,null,values);
db.close();
}
public Contact getContact(int id){
SQLiteDatabase db =getReadableDatabase();
Cursor cursor =db.query(TABLE_CONTACTS, new String[]{KEY_ID,KEY_CAR,KEY_STATUS,KEY_IMAGEURI},KEY_ID + "=?" , new String[]{String.valueOf(id)},null,null,null);
if(cursor!=null)
cursor.moveToFirst();
Contact contact = new Contact(Integer.parseInt(cursor.getString(0)),cursor.getString(1),cursor.getString(2), Uri.parse(cursor.getString(3)));
db.close();
cursor.close();
return contact;
}
public void deleteContact(Contact contact){
SQLiteDatabase db = getWritableDatabase() ;
db.delete(TABLE_CONTACTS,KEY_ID+"=?",new String[]{String.valueOf(contact.getid())});
db.close();
}
public int getContactCount(){
SQLiteDatabase db= getReadableDatabase();
Cursor cursor =db.rawQuery("SELECT * FROM" + TABLE_CONTACTS,null);
int count = cursor.getCount();
db.close();
cursor.close();
return count;
}
public int updateContact(Contact contact){
SQLiteDatabase db= getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_CAR,contact.getCar());
values.put(KEY_STATUS,contact.getStatus());
values.put(KEY_IMAGEURI, contact.getimageURI().toString());
db.insert(TABLE_CONTACTS,null,values);
return db.update(TABLE_CONTACTS,values,KEY_ID +"=?",new String[]{String.valueOf(contact.getid())});
}
public List<Contact> getAllContacts(){
List<Contact> contacts = new ArrayList<>();
SQLiteDatabase db = getWritableDatabase();
Cursor cursor =db.rawQuery("SELECT * FROM"+ TABLE_CONTACTS,null);
if (cursor.moveToFirst()){
do {
Contact contact = new Contact(Integer.parseInt(cursor.getString(0)),cursor.getString(1),cursor.getString(2),Uri.parse(cursor.getString(3)));
contacts.add(contact);
}
while (cursor.moveToNext());
}
return contacts;
}
}
logcat是
public class MainActivity extends ActionBarActivity {
EditText carTxt, statusTxt;
ImageView contactImageImgView;
List<Contact> Contacts = new ArrayList<Contact>();
ListView contactListView;
Uri imageUri=null;
DatabaseHandler dbHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
carTxt = (EditText) findViewById(R.id.txtCar);
statusTxt = (EditText) findViewById(R.id.txtStatus);
contactListView = (ListView) findViewById(R.id.listView);
contactImageImgView = (ImageView) findViewById(R.id.imgViewContactImage);
dbHandler = new DatabaseHandler(getApplicationContext());
TabHost tabHost = (TabHost) findViewById(R.id.tabHost);
tabHost.setup();
TabHost.TabSpec tabSpec = tabHost.newTabSpec("Creator");
tabSpec.setContent(R.id.Creator);
tabSpec.setIndicator("Creator");
tabHost.addTab(tabSpec);
tabSpec = tabHost.newTabSpec("List");
tabSpec.setContent(R.id.List);
tabSpec.setIndicator("List");
tabHost.addTab(tabSpec);
final Button addBtn = (Button) findViewById(R.id.btnAdd);
addBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Contact contact = new Contact(dbHandler.getContactCount(),String.valueOf(carTxt.getText()),String.valueOf(statusTxt.getText()),imageUri);
dbHandler.createContact(contact);
Contacts.add(contact);
populateList();
Toast.makeText(getApplicationContext(), carTxt.getText().toString() + " has been add to list", Toast.LENGTH_SHORT).show();
}
});
carTxt.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
addBtn.setEnabled(!carTxt.getText().toString().trim().isEmpty());
}
@Override
public void afterTextChanged(Editable s) {
}
});
contactImageImgView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Contact Image"), 1);
}
});
List<Contact> addableContacts = dbHandler.getAllContacts();
int contactcount = dbHandler.getContactCount();
for(int i =0;i< contactcount;i++) {
Contacts.add(addableContacts.get(1));
}
if(!addableContacts.isEmpty())
populateList();
}
public void onActivityResult(int reqcode, int rescode, Intent data){
if(rescode == RESULT_OK){
if(reqcode == 1){
imageUri = data.getData();
contactImageImgView.setImageURI(data.getData());
}
}
}
private void populateList() {
ArrayAdapter<Contact> adapter = new ContactListAdapter();
contactListView.setAdapter(adapter);
}
private class ContactListAdapter extends ArrayAdapter<Contact> {
public ContactListAdapter() {
super(MainActivity.this, R.layout.listview_item, Contacts);
}
@Override
public View getView(int position, View view, ViewGroup parent) {
if (view == null)
view = getLayoutInflater().inflate(R.layout.listview_item, parent, false);
Contact currentContact = Contacts.get(position);
TextView car = (TextView) view.findViewById(R.id.contactname);
car.setText(currentContact.getCar());
TextView status = (TextView) view.findViewById(R.id.status);
status.setText(currentContact.getStatus());
ImageView img = (ImageView) view.findViewById(R.id.img);
img.setImageURI(currentContact.getimageURI());
return view;
}
}
}
答案 0 :(得分:2)
这是你的错误(最后缺少空格,括号+额外的逗号):
db.execSQL("CREATE TABLE " + TABLE_CONTACTS +""+ KEY_ID + " INTEGER PRIMARY," + KEY_CAR + "TEXT," + KEY_STATUS + "TEXT," + KEY_IMAGEURI + "TEXT,");
和(两次!!)
Cursor cursor =db.rawQuery("SELECT * FROM" + TABLE_CONTACTS,null);
和
db.execSQL("DROP TABLE IF EXISTS"+ TABLE_CONTACTS);
这是修复:
db.execSQL("CREATE TABLE " + TABLE_CONTACTS +" ("+ KEY_ID + " INTEGER PRIMARY, " + KEY_CAR + " TEXT, " + KEY_STATUS + " TEXT, " + KEY_IMAGEURI + " TEXT)");
和(两次!!)
Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_CONTACTS, null);
和
db.execSQL("DROP TABLE IF EXISTS "+ TABLE_CONTACTS);