Java Android emulator因Db而崩溃(找不到错误-新手)
很抱歉提出这个问题,但我真的不知道为什么我的DB会抛出异常。这是密码,请看一下。整个想法是把一个刽子手游戏作为一个学校项目,我想创建一个用户可以看到所有当前单词的地方,如果他/她想要或删除需求中现有的单词,可以添加更多。我认为我在populateListView方法中犯了一个错误,因为我认为我以前没有使用过光标,但看过一些在线资料,所以我尝试进行相应的调整。如果有一个更简单的方法来做->我完全赞成。提前谢谢 这是DBAdapter文件:Java Android emulator因Db而崩溃(找不到错误-新手),java,android,database,sqlite,Java,Android,Database,Sqlite,很抱歉提出这个问题,但我真的不知道为什么我的DB会抛出异常。这是密码,请看一下。整个想法是把一个刽子手游戏作为一个学校项目,我想创建一个用户可以看到所有当前单词的地方,如果他/她想要或删除需求中现有的单词,可以添加更多。我认为我在populateListView方法中犯了一个错误,因为我认为我以前没有使用过光标,但看过一些在线资料,所以我尝试进行相应的调整。如果有一个更简单的方法来做->我完全赞成。提前谢谢 这是DBAdapter文件: public class DBAdapter { pri
public class DBAdapter {
private static final String TAG = "DBAdapter"; //used for logging database version changes
// Field Names:
public static final String KEY_ROWID = "_id";
public static final String KEY_WORD = "word";
public static final String[] ALL_KEYS = new String[] {KEY_ROWID, KEY_WORD};
// Column Numbers for each Field Name:
public static final int COL_ROWID = 0;
public static final int COL_WORD = 1;
// DataBase info:
public static final String DATABASE_NAME = "dbToDo";
public static final String DATABASE_TABLE = "mainToDo";
public static final int DATABASE_VERSION = 2; // The version number must be incremented each time a change to DB structure occurs.
//SQL statement to create database
private static final String DATABASE_CREATE_SQL =
"CREATE TABLE " + DATABASE_TABLE
+ " (" + KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ KEY_WORD + " TEXT NOT NULL"
+ ");";
private final Context context;
private DatabaseHelper myDBHelper;
private SQLiteDatabase db;
public DBAdapter(Context ctx) {
this.context = ctx;
myDBHelper = new DatabaseHelper(context);
}
// Open the database connection.
public DBAdapter open() {
db = myDBHelper.getWritableDatabase();
return this;
}
// Close the database connection.
public void close() {
myDBHelper.close();
}
// Add a new set of values to be inserted into the database.
public long insertRow(String word) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_WORD, word);
// Insert the data into the database.
return db.insert(DATABASE_TABLE, null, initialValues);
}
// Delete a row from the database, by rowId (primary key)
public boolean deleteRow(long rowId) {
String where = KEY_ROWID + "=" + rowId;
return db.delete(DATABASE_TABLE, where, null) != 0;
}
public void deleteAll() {
Cursor c = getAllRows();
long rowId = c.getColumnIndexOrThrow(KEY_ROWID);
if (c.moveToFirst()) {
do {
deleteRow(c.getLong((int) rowId));
} while (c.moveToNext());
}
c.close();
}
// Return all data in the database.
public Cursor getAllRows() {
String where = null;
Cursor c = db.query(true, DATABASE_TABLE, ALL_KEYS, where, null, null, null, null, null);
if (c != null) {
c.moveToFirst();
}
return c;
}
// Get a specific row (by rowId)
public Cursor getRow(long rowId) {
String where = KEY_ROWID + "=" + rowId;
Cursor c = db.query(true, DATABASE_TABLE, ALL_KEYS,
where, null, null, null, null, null);
if (c != null) {
c.moveToFirst();
}
return c;
}
// Change an existing row to be equal to new data.
public boolean updateRow(long rowId, String word) {
String where = KEY_ROWID + "=" + rowId;
ContentValues newValues = new ContentValues();
newValues.put(KEY_WORD, word);
// Insert it into the database.
return db.update(DATABASE_TABLE, newValues, where, null) != 0;
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase _db) {
_db.execSQL(DATABASE_CREATE_SQL);
}
@Override
public void onUpgrade(SQLiteDatabase _db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading application's database from version " + oldVersion
+ " to " + newVersion + ", which will destroy all old data!");
// Destroy old database:
_db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
// Recreate new database:
onCreate(_db);
}
}
这里是另一个崩溃的地方:
public class EditWords extends Activity {
ListView listViewWords;
EditText editTextNewWord;
Button buttonAddWord;
Button buttonDeleteWord;
DBAdapter myDb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_words);
listViewWords = (ListView) findViewById(R.id.listViewWords);
editTextNewWord = (EditText) findViewById(R.id.editTextNewWord);
buttonAddWord = (Button) findViewById(R.id.buttonAddWord);
buttonDeleteWord = (Button) findViewById(R.id.buttonDeleteWord);
populateListView();
openDB();
}
private void openDB(){
myDb = new DBAdapter(this);
myDb.open();
}
private void populateListView() {
Cursor cursor = myDb.getAllRows();
String[] fromFieldNames = new String[] {DBAdapter.KEY_ROWID,DBAdapter.KEY_WORD};
int[] toViewIDs = new int[]{R.id.editTextNewWord};
SimpleCursorAdapter myCursorAdapter;
myCursorAdapter = new SimpleCursorAdapter(getBaseContext(),R.id.listViewWords,cursor,fromFieldNames,toViewIDs,0);
listViewWords.setAdapter(myCursorAdapter);
}
public void onClick_AddWord(View v) {
if(!TextUtils.isEmpty(editTextNewWord.getText().toString())) {
myDb.insertRow(editTextNewWord.getText().toString());
}
populateListView();
}
}
堆栈跟踪:
03-30 12:13:39.603 8800-9926/com.example.daniel.hangman I/OpenGLRenderer:初始化EGL,版本1.4
03-30 12:13:39.603 8800-9926/com.example.daniel.hangman W/OpenGLRenderer:未能选择保留EGL交换行为的配置,正在重试,但没有。。。
03-30 12:13:42.570 8800-8800/com.example.daniel.hangman D/AndroidRuntime:关闭虚拟机
03-30 12:13:42.571 8800-8800/com.example.daniel.hangman E/AndroidRuntime:致命异常:main
进程:com.example.daniel.hangman,PID:8800
android.content.res.Resources$NotFoundException:资源ID 0x7f070009类型0x12无效
位于android.content.res.Resources.loadXmlResourceParserResources.java:2779
位于android.content.res.Resources.getLayoutResources.java:1165
在android.view.LayoutInflater.inflateLayoutInflater.java:421
位于android.widget.ResourceCursorAdapter.newViewResourceCursorAdapter.java:135
位于android.widget.CursorAdapter.getViewCursorAdapter.java:285
在android.widget.AbsListView.ActainViewAbsListView.java:2346
在android.widget.ListView.makeAndAddViewListView.java:1875
在android.widget.ListView.fillDownListView.java:702
在android.widget.ListView.fillFromTopListView.java:763
在android.widget.ListView.layoutChildrenListView.java:1684
在android.widget.AbsListView.onLayoutAblistView.java:2148
在android.view.view.layoutView.java:16630
在android.view.ViewGroup.layoutViewGroup.java:5437
位于android.widget.LinearLayout.setChildFrameLinearLayout.java:1743
位于android.widget.LinearLayout.layoutVerticalLinearLayout.java:1586
在android.widget.LinearLayout.onLayoutLinearLayout.java:1495
在android.view.view.layoutView.java:16630
在android.view.ViewGroup.layoutViewGroup.java:5437
在android.widget.FrameLayout.layoutChildrenFrameLayout.java:336
位于android.widget.FrameLayout.onLayoutFrameLayout.java:273
在android.view.view.layoutView.java:16630
在android.view.ViewGroup.layoutViewGroup.java:5437
位于com.android.internal.widget.ActionBarOverlayLayout.onLayoutActionBarOverlayLayout.java:493
在android.view.view.layoutView.java:16630
在android.view.ViewGroup.layoutViewGroup.java:5437
在android.widget.FrameLayout.layoutChildrenFrameLayout.java:336
位于android.widget.FrameLayout.onLayoutFrameLayout.java:273
在com.android.internal.policy.PhoneWindow$DecorView.onLayoutPhoneWindow.java:2678
在android.view.view.layoutView.java:16630
在android.view.ViewGroup.layoutViewGroup.java:5437
在android.view.ViewRootImpl.performLayoutViewRootImpl.java:2171
在android.view.ViewRootImpl.performTraversalsViewRootImpl.java:1931
在android.view.ViewRootImpl.doTraversalViewRootImpl.java:1107
在android.view.ViewRootImpl$TraversalRunnable.runViewRootImpl.java:6013
在android.view.Choreographer$CallbackRecord.runChoreographer.java:858
在android.view.Choreographer.docallbackshoreographer.java:670
在android.view.Choreographer.doFrameChoreographer.java:606
在android.view.Choreographer$FrameDisplayEventReceiver.runChoreographer.java:844
位于android.os.Handler.handleCallbackHandler.java:739
在android.os.Handler.dispatchMessageHandler.java:95
在android.os.Looper.Looper.java:148
在android.app.ActivityThread.mainActivityThread.java:5417
在java.lang.reflect.Method.Invokenactive方法中
位于com.android.internal.os.ZygoteInit$MethodAndArgsCaller.runZygoteInit.java:726
在com.android.internal.os.ZygoteInit.mainZygoteInit.java:616上,您可以调用:
populateListView();
在以下情况之前使用“myDb”:
openDB();
它初始化了“myDb”将您的stacktrace张贴到邮件中。事情是这样的:您在openDB之前调用了populateListView,所以中populateListView中的myDB为null…可能是交换的myDB的重复,但现在它抛出了另一个错误。我将更新stacktracefixed该部分,但它仍然抛出和错误不同的一个。这次,更新了stack Trace。对于此问题,您应该检查此线程: