我是Android的新手,似乎我喜欢崩溃我制作的任何应用程序:)
我正在创建一个简单的联系人应用程序,用户可以自己输入联系人数据,然后他可以在主要活动的列表视图中看到它们
每当我打开应用程序时它就会崩溃,当我看到我的logcat时,我看到数据库没有创建,任何人都可以帮助我并告诉我我做错了什么?
我的MainActivity:
public class MainActivity extends Activity {
Button ADD;
ListView LS;
DBcreator dbview;
SQLiteDatabase db;
ArrayList<HashMap<String, String>> List = new ArrayList<HashMap<String, String>>();// define
// an
// arraylist
// with
// hashmap
// for
// ref.//
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ADD = (Button) findViewById(R.id.AddButton);
LS = (ListView) findViewById(R.id.ContactsList);
SimpleAdapter sa = new SimpleAdapter(this, List,
R.layout.activity_main, new String[] { DBcreator.KEY_ROWID,
DBcreator.KEY_FNAME, DBcreator.KEY_LNAME,
DBcreator.KEY_MOBILE, DBcreator.KEY_ADDRESS },
new int[] { R.id.ContactsList });
existingcontacts();
LS.setAdapter(sa);
LS.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
}
});
ADD.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent j = new Intent(MainActivity.this, CreateContact.class);
startActivity(j);
}
});
}
private void existingcontacts() {
// TODO Auto-generated method stub
DBcreator db = new DBcreator(MainActivity.this);
SQLiteDatabase database = db.getReadableDatabase();
Cursor find = database.rawQuery("select * from contactsdisplay", null);
while (find.moveToNext()) {
HashMap<String, String> data = new HashMap<String, String>();
data.put("_ID", find.getString(0));
data.put("FirstName", find.getString(1));
data.put("LastName", find.getString(2));
data.put("Mobile", find.getString(3));
data.put("Address", find.getString(4));
List.add(data);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
数据库类:
public class DBcreator extends SQLiteOpenHelper {
/*
* this class is responsible for all the database manipulation and
* requirements it is called inside other classes for implementation
*/
private SQLiteDatabase db;
public static final String DATABASE_NAME = "contacts";// NAME OF THE
// DATABASE//
public static final String DATABASE_TABLE = "contactsdisplay";// NAME OF
// THE
// TABLE
// INSIDE
// OF
// IT//
// THE DATA INSIDE THE TABLE//
public static final String KEY_FNAME = "first_name";
public static final String KEY_LNAME = "last_name";
public static final String KEY_MOBILE = "mobile";
public static final String KEY_ADDRESS = "address";
public static final String KEY_ROWID = "_id";
// VERSION OF THE DATABASE//
public static final int DATABASE_VERSION = 1;
// create database//
public static final String DATABASE_CREATE = " CREATE TABLE "
+ DATABASE_TABLE + "(" + KEY_ROWID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_FNAME
+ " TEXT NOT NULL, " + KEY_LNAME + " TEXT NOT NULL, " + KEY_MOBILE
+ " TEXT NOT NULL, " + KEY_ADDRESS + " TEXT NOT NULL);";
public DBcreator(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(DATABASE_CREATE);// this orders the code to create the
// database//
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// drop old version and use the new one//
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
public long add(String fname, String lname, String mob, String address) {
// getting the database and editing in it//
db = this.getWritableDatabase();
// insert what the user had entered in the database//
ContentValues cv = new ContentValues();
cv.put(KEY_FNAME, fname);
cv.put(KEY_LNAME, lname);
cv.put(KEY_MOBILE, mob);
cv.put(KEY_ADDRESS, address);
// db.insert(DATABASE_TABLE, null, cv);
return db.insert(DATABASE_TABLE, null, cv);
}
public void savechanges(String fname, String lname, String mob,
String address) {
// change the existing data of the contact by lookup then replace//
db = this.getWritableDatabase();
String[] array = new String[] { KEY_ROWID, KEY_FNAME, KEY_LNAME,
KEY_MOBILE, KEY_ADDRESS };
Cursor c = db.query(DATABASE_TABLE, array, fname, null, null, null,
null);
// we need to loop inside the database to find the existing values and
// replace them//
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
int firstname = c.getColumnIndex(KEY_FNAME);
int lastname = c.getColumnIndex(KEY_FNAME);
int mobile = c.getColumnIndex(KEY_FNAME);
int address2 = c.getColumnIndex(KEY_FNAME);
if (c.getString(firstname).equals(fname)) {
ContentValues changes = new ContentValues();
changes.put(KEY_FNAME, fname);
changes.put(KEY_LNAME, lname);
changes.put(KEY_MOBILE, mob);
changes.put(KEY_ADDRESS, address);
db.update(DATABASE_TABLE, changes, KEY_FNAME + "=" + fname,
null);
break;
}
}
}
public void deletecontact(String fnamedelete) {
// TODO Auto-generated method stub
db = this.getWritableDatabase();
db.delete(DATABASE_TABLE, KEY_FNAME + "=" + fnamedelete, null);
}
public String[] queryall() {
String[] FirstNames = new String[] { KEY_FNAME };
db = this.getWritableDatabase();
Cursor cfname = db.query(DATABASE_TABLE, FirstNames, null, null, null,
null, null);// to see all contacts//
int GetTheName = cfname.getColumnIndex(KEY_FNAME);
List<String> TheNames = new ArrayList<String>();
{
cfname.moveToFirst();
while (cfname.moveToNext()) {
TheNames.add(cfname.getString(GetTheName));
}
return TheNames.toArray(new String[TheNames.size()]);
}
}
}
Logcat:
01-04 15:59:38.294: E/Trace(7479): error opening trace file: No such file or directory (2)
01-04 15:59:38.518: W/dalvikvm(7479): threadid=1: thread exiting with uncaught exception (group=0x41af8908)
01-04 15:59:38.541: E/AndroidRuntime(7479): FATAL EXCEPTION: main
01-04 15:59:38.541: E/AndroidRuntime(7479): java.lang.IllegalStateException: android.widget.ListView is not a view that can be bounds by this SimpleAdapter
01-04 15:59:38.541: E/AndroidRuntime(7479): at android.widget.SimpleAdapter.bindView(SimpleAdapter.java:195)
01-04 15:59:38.541: E/AndroidRuntime(7479): at android.widget.SimpleAdapter.createViewFromResource(SimpleAdapter.java:126)
01-04 15:59:38.541: E/AndroidRuntime(7479): at android.widget.SimpleAdapter.getView(SimpleAdapter.java:114)
01-04 15:59:38.541: E/AndroidRuntime(7479): at android.widget.AbsListView.obtainView(AbsListView.java:2340)
01-04 15:59:38.541: E/AndroidRuntime(7479): at android.widget.ListView.measureHeightOfChildren(ListView.java:1250)
01-04 15:59:38.541: E/AndroidRuntime(7479): at android.widget.ListView.onMeasure(ListView.java:1162)
01-04 15:59:38.541: E/AndroidRuntime(7479): at android.view.View.measure(View.java:15341)
01-04 15:59:38.541: E/AndroidRuntime(7479): at android.widget.RelativeLayout.measureChild(RelativeLayout.java:602)
01-04 15:59:38.541: E/AndroidRuntime(7479): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:415)
01-04 15:59:38.541: E/AndroidRuntime(7479): at android.view.View.measure(View.java:15341)
01-04 15:59:38.541: E/AndroidRuntime(7479): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4931)
01-04 15:59:38.541: E/AndroidRuntime(7479): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-04 15:59:38.541: E/AndroidRuntime(7479): at android.view.View.measure(View.java:15341)
01-04 15:59:38.541: E/AndroidRuntime(7479): at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
01-04 15:59:38.541: E/AndroidRuntime(7479): at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
01-04 15:59:38.541: E/AndroidRuntime(7479): at android.view.View.measure(View.java:15341)
01-04 15:59:38.541: E/AndroidRuntime(7479): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4931)
01-04 15:59:38.541: E/AndroidRuntime(7479): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-04 15:59:38.541: E/AndroidRuntime(7479): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2172)
01-04 15:59:38.541: E/AndroidRuntime(7479): at android.view.View.measure(View.java:15341)
01-04 15:59:38.541: E/AndroidRuntime(7479): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2131)
01-04 15:59:38.541: E/AndroidRuntime(7479): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1242)
01-04 15:59:38.541: E/AndroidRuntime(7479): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1435)
01-04 15:59:38.541: E/AndroidRuntime(7479): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1127)
01-04 15:59:38.541: E/AndroidRuntime(7479): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4606)
01-04 15:59:38.541: E/AndroidRuntime(7479): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:747)
01-04 15:59:38.541: E/AndroidRuntime(7479): at android.view.Choreographer.doCallbacks(Choreographer.java:567)
01-04 15:59:38.541: E/AndroidRuntime(7479): at android.view.Choreographer.doFrame(Choreographer.java:536)
01-04 15:59:38.541: E/AndroidRuntime(7479): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:733)
01-04 15:59:38.541: E/AndroidRuntime(7479): at android.os.Handler.handleCallback(Handler.java:615)
01-04 15:59:38.541: E/AndroidRuntime(7479): at android.os.Handler.dispatchMessage(Handler.java:92)
01-04 15:59:38.541: E/AndroidRuntime(7479): at android.os.Looper.loop(Looper.java:153)
01-04 15:59:38.541: E/AndroidRuntime(7479): at android.app.ActivityThread.main(ActivityThread.java:5006)
01-04 15:59:38.541: E/AndroidRuntime(7479): at java.lang.reflect.Method.invokeNative(Native Method)
01-04 15:59:38.541: E/AndroidRuntime(7479): at java.lang.reflect.Method.invoke(Method.java:511)
01-04 15:59:38.541: E/AndroidRuntime(7479): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
01-04 15:59:38.541: E/AndroidRuntime(7479): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
01-04 15:59:38.541: E/AndroidRuntime(7479): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
在onCreate中,你正在创建一个名为contactsdisplay.db的表(你可能不会用.db结束表名,这很令人困惑)。
从这一行:Caused by: android.database.sqlite.SQLiteException: no such table: data (code 1): , while compiling: select * from data
原因是这一行:
Cursor find = database.rawQuery("select * from data", null);
您正在访问从未创建的“数据”表。
编辑:此行:
SimpleAdapter sa = new SimpleAdapter(this, List, R.layout.activity_main ...)
第三个参数必须是实际ListView 项的资源。
答案 1 :(得分:0)
嘿,你的桌名错了。 contactsdisplay.db
这就是为什么找不到这张桌子的原因。
更正:将表格设为contactsdisplay
注意:使用.db
课程时,您不必提及SQLiteOpenHelper
分机。
答案 2 :(得分:0)
我制作了另一个xml来显示数据然后我使用了一个简单的光标,如下所示
DBcreator db = new DBcreator(MainActivity.this);
SQLiteDatabase database = db.getReadableDatabase();
Cursor find = database.rawQuery("select * from contactsdisplay", null);//go read from the database contactsdisplay//
//below is the adapter to put the intended content in the list view//
SimpleCursorAdapter cursor=new SimpleCursorAdapter(MainActivity.this,
R.layout.listviewdistribution,
find,
new String[] {DBcreator.KEY_FNAME, DBcreator.KEY_LNAME,DBcreator.KEY_MOBILE,DBcreator.KEY_ADDRESS},
new int[]{R.id.FirstNameTextView,R.id.LastNameTextView,R.id.MobileTextView,R.id.AddressTextView});
//below is looping with the cursor inside the table to get all data//
while (find.moveToNext()) {
HashMap<String, String> data = new HashMap<String, String>();
data.put("_ID", find.getString(0));
data.put("FirstName", find.getString(1));
data.put("LastName", find.getString(2));
data.put("Mobile", find.getString(3));
data.put("Address", find.getString(4));
List.add(data);
}
LS.setAdapter(cursor);
}