私はAndroidプログラミングに関して初心者であり、SQLiteを理解するのに特に問題があります。アプリを実行すると正常に読み込まれますが、「トレース ファイルを開くときにエラーが発生しました: そのようなファイルまたはディレクトリはありません (2)」というエラーが表示されます。次に、ボタンをクリックすると、データベースに素晴らしい文字列が挿入され、textView に返されます。これらのエラーが発生し、アプリがクラッシュします。
03-15 17:35:01.523: E/Trace(794): error opening trace file: No such file or directory (2)
03-15 17:35:02.223: D/gralloc_goldfish(794): Emulator without GPU emulation detected.
03-15 17:37:48.623: E/SQLiteLog(794): (1) no such column: _id
03-15 17:37:48.623: D/AndroidRuntime(794): Shutting down VM
03-15 17:37:48.623: W/dalvikvm(794): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
03-15 17:37:48.673: E/AndroidRuntime(794): FATAL EXCEPTION: main
03-15 17:37:48.673: E/AndroidRuntime(794): java.lang.IllegalStateException: Could not execute method of the activity
03-15 17:37:48.673: E/AndroidRuntime(794): at android.view.View$1.onClick(View.java:3591)
03-15 17:37:48.673: E/AndroidRuntime(794): at android.view.View.performClick(View.java:4084)
03-15 17:37:48.673: E/AndroidRuntime(794): at android.view.View$PerformClick.run(View.java:16966)
03-15 17:37:48.673: E/AndroidRuntime(794): at android.os.Handler.handleCallback(Handler.java:615)
03-15 17:37:48.673: E/AndroidRuntime(794): at android.os.Handler.dispatchMessage(Handler.java:92)
03-15 17:37:48.673: E/AndroidRuntime(794): at android.os.Looper.loop(Looper.java:137)
03-15 17:37:48.673: E/AndroidRuntime(794): at android.app.ActivityThread.main(ActivityThread.java:4745)
03-15 17:37:48.673: E/AndroidRuntime(794): at java.lang.reflect.Method.invokeNative(Native Method)
03-15 17:37:48.673: E/AndroidRuntime(794): at java.lang.reflect.Method.invoke(Method.java:511)
03-15 17:37:48.673: E/AndroidRuntime(794): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
03-15 17:37:48.673: E/AndroidRuntime(794): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-15 17:37:48.673: E/AndroidRuntime(794): at dalvik.system.NativeStart.main(Native Method)
03-15 17:37:48.673: E/AndroidRuntime(794): Caused by: java.lang.reflect.InvocationTargetException
03-15 17:37:48.673: E/AndroidRuntime(794): at java.lang.reflect.Method.invokeNative(Native Method)
03-15 17:37:48.673: E/AndroidRuntime(794): at java.lang.reflect.Method.invoke(Method.java:511)
03-15 17:37:48.673: E/AndroidRuntime(794): at android.view.View$1.onClick(View.java:3586)
03-15 17:37:48.673: E/AndroidRuntime(794): ... 11 more
03-15 17:37:48.673: E/AndroidRuntime(794): Caused by: android.database.sqlite.SQLiteException: no such column: _id (code 1): , while compiling: SELECT _id, content FROM exampletable WHERE _id = 4
03-15 17:37:48.673: E/AndroidRuntime(794): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
03-15 17:37:48.673: E/AndroidRuntime(794): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
03-15 17:37:48.673: E/AndroidRuntime(794): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
03-15 17:37:48.673: E/AndroidRuntime(794): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
03-15 17:37:48.673: E/AndroidRuntime(794): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
03-15 17:37:48.673: E/AndroidRuntime(794): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
03-15 17:37:48.673: E/AndroidRuntime(794): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
03-15 17:37:48.673: E/AndroidRuntime(794): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
03-15 17:37:48.673: E/AndroidRuntime(794): at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1161)
03-15 17:37:48.673: E/AndroidRuntime(794): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1032)
03-15 17:37:48.673: E/AndroidRuntime(794): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1238)
03-15 17:37:48.673: E/AndroidRuntime(794): at com.example.sqlite.MainActivity.insertButton(MainActivity.java:38)
03-15 17:37:48.673: E/AndroidRuntime(794): ... 14 more
私の MainActivity.java:
package com.example.sqlite;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity {
DBHelper dbhelper = new DBHelper(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@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 void insertButton(View view){
SQLiteDatabase db = dbhelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("content", "Awesome");
long newRowId = db.insert("exampletable", null, values);
String[] projection = {"_id", "content"};
Cursor c = db.query("exampletable", projection, "_id = " + newRowId, null, null, null, null, null);
c.moveToFirst();
String content = c.getString(c.getColumnIndex("content"));
TextView textView = (TextView) findViewById(R.id.textView1);
textView.setText(content);
}
}
私の DBHelper.java:
package com.example.sqlite;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBHelper extends SQLiteOpenHelper {
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "ExampleDB";
private String ONCREATE_SQL = "CREATE TABLE exampletable(_id INTEGER PRIMARY KEY AUTOINCREMENT, content TEXT);";
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(ONCREATE_SQL);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
私は何を間違っていますか?ボタンをクリックしたときのエラーは初期エラーによるものですか、それとも別のものですか? どうもありがとう。