テーブルから頭をぶつけています。私はこれを理解できないようです。私がやりたいことは、アクティビティが作成されたときに、SQLite データベースからの項目が取り込まれたリストビューを持つことだけです。
多分私は何か小さなものを見逃しているのかもしれません.この時点では何もわかりません. ファイル エクスプローラーに存在しないため、データベースが作成されていないことはわかっています。
DatabaseHandler.java は次のとおりです。
package com.dd.gfit;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHandler extends SQLiteOpenHelper {
private SQLiteDatabase db;
// database strings
public static final String DATABASE_NAME = "gfit.db";
public static final int DATABASE_VERSION = 1;
// table strings
public static final String TABLE_ROUTINES = "routines";
// key strings
public static final String KEY_ROUTINES_ID = "id";
public static final String KEY_ROUTINES_NAME = "name";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// create routines table
db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_ROUTINES + " ("
+ KEY_ROUTINES_ID + " INTEGER PRIMARY KEY AUTO_INCREMENT,"
+ KEY_ROUTINES_NAME + " VARCHAR"
+ ")");
// insert test data into routines table
db.execSQL("INSERT INTO " + TABLE_ROUTINES + " (" + KEY_ROUTINES_NAME + ") VALUES ('Test Routine 1')");
db.execSQL("INSERT INTO " + TABLE_ROUTINES + " (" + KEY_ROUTINES_NAME + ") VALUES ('Test Routine 2')");
db.execSQL("INSERT INTO " + TABLE_ROUTINES + " (" + KEY_ROUTINES_NAME + ") VALUES ('Test Routine 3')");
db.execSQL("INSERT INTO " + TABLE_ROUTINES + " (" + KEY_ROUTINES_NAME + ") VALUES ('Test Routine 4')");
db.execSQL("INSERT INTO " + TABLE_ROUTINES + " (" + KEY_ROUTINES_NAME + ") VALUES ('Test Routine 5')");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// do database upgrades here
}
public Cursor fetchRoutines() {
Cursor cursor = db.query(TABLE_ROUTINES, new String[] {KEY_ROUTINES_ID, KEY_ROUTINES_NAME}, null, null, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
}
return cursor;
}
}
RoutinesActivity.java は次のとおりです。
package com.dd.gfit;
import android.os.Bundle;
import android.app.Activity;
import android.database.Cursor;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class RoutinesActivity extends Activity {
private DatabaseHandler db = new DatabaseHandler(this);
private Cursor cursor = db.fetchRoutines();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_routines);
// Show the Up button in the action bar.
getActionBar().setDisplayHomeAsUpEnabled(true);
// open the database and show the routines
db.getWritableDatabase();
displayRoutines();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_routines, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
private void displayRoutines() {
ListAdapter adapter = new SimpleCursorAdapter(
this,
R.layout.listitem_routine, cursor,
new String[] { db.KEY_ROUTINES_NAME },
new int[] { R.id.listitem_routine_name });
ListView routineList = (ListView)findViewById(R.id.list_routines);
routineList.setAdapter(adapter);
}
}
ここに activity_routines.xml があります:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".RoutinesActivity" >
<ListView
android:id="@+id/list_routines"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="10dp" >
</ListView>
</RelativeLayout>
listitem_routine.xml は次のとおりです。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center_vertical" >
<TextView
android:id="@+id/listitem_routine_name"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/lorem_routine_item"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="@+id/button_routine_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_action_edit"
android:contentDescription="@string/ic_action_edit" />
<ImageView
android:id="@+id/button_routine_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_action_delete"
android:contentDescription="@string/ic_action_delete" />
</LinearLayout>
ここで何が問題なのですか?また、これが機能するようになると、listitem_routine.xml でわかるように、各リストビュー アイテム内にボタンが表示されます。KEY_ROUTINES_ID を何らかの形でボタンに渡して、各項目をその場で編集できるようにしたいと考えています。誰でも知っている、または単なるスニペットを使用できるチュートリアルはありますか?
編集:
ログは次のとおりです。
12-24 02:52:24.429: D/dalvikvm(783): GC_FOR_ALLOC freed 49K, 7% free 2564K/2732K, paused 68ms, total 69ms
12-24 02:52:24.449: I/dalvikvm-heap(783): Grow heap (frag case) to 3.616MB for 1048592-byte allocation
12-24 02:52:24.559: D/dalvikvm(783): GC_FOR_ALLOC freed 2K, 5% free 3585K/3760K, paused 110ms, total 110ms
12-24 02:52:24.659: D/dalvikvm(783): GC_CONCURRENT freed <1K, 5% free 3585K/3760K, paused 5ms+3ms, total 102ms
12-24 02:52:24.839: D/dalvikvm(783): GC_FOR_ALLOC freed <1K, 5% free 3585K/3760K, paused 51ms, total 51ms
12-24 02:52:24.869: I/dalvikvm-heap(783): Grow heap (frag case) to 5.863MB for 2359312-byte allocation
12-24 02:52:24.989: D/dalvikvm(783): GC_CONCURRENT freed 0K, 3% free 5889K/6068K, paused 24ms+33ms, total 115ms
12-24 02:52:25.319: D/gralloc_goldfish(783): Emulator without GPU emulation detected.
12-24 02:52:31.950: E/Trace(827): error opening trace file: No such file or directory (2)
12-24 02:52:32.660: D/dalvikvm(827): GC_FOR_ALLOC freed 52K, 7% free 2564K/2736K, paused 25ms, total 28ms
12-24 02:52:32.670: I/dalvikvm-heap(827): Grow heap (frag case) to 3.616MB for 1048592-byte allocation
12-24 02:52:32.710: D/dalvikvm(827): GC_FOR_ALLOC freed 2K, 5% free 3585K/3764K, paused 40ms, total 40ms
12-24 02:52:32.760: D/dalvikvm(827): GC_CONCURRENT freed <1K, 5% free 3585K/3764K, paused 5ms+3ms, total 50ms
12-24 02:52:32.840: D/dalvikvm(827): GC_FOR_ALLOC freed <1K, 5% free 3585K/3764K, paused 19ms, total 19ms
12-24 02:52:32.860: I/dalvikvm-heap(827): Grow heap (frag case) to 5.863MB for 2359312-byte allocation
12-24 02:52:33.000: D/dalvikvm(827): GC_CONCURRENT freed 0K, 4% free 5889K/6072K, paused 77ms+13ms, total 140ms
12-24 02:52:33.250: D/gralloc_goldfish(827): Emulator without GPU emulation detected.
12-24 02:52:36.120: D/AndroidRuntime(827): Shutting down VM
12-24 02:52:36.140: W/dalvikvm(827): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
12-24 02:52:36.190: E/AndroidRuntime(827): FATAL EXCEPTION: main
12-24 02:52:36.190: E/AndroidRuntime(827): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.dd.gfit/com.dd.gfit.RoutinesActivity}: java.lang.NullPointerException
12-24 02:52:36.190: E/AndroidRuntime(827): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
12-24 02:52:36.190: E/AndroidRuntime(827): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-24 02:52:36.190: E/AndroidRuntime(827): at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-24 02:52:36.190: E/AndroidRuntime(827): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
12-24 02:52:36.190: E/AndroidRuntime(827): at android.os.Handler.dispatchMessage(Handler.java:99)
12-24 02:52:36.190: E/AndroidRuntime(827): at android.os.Looper.loop(Looper.java:137)
12-24 02:52:36.190: E/AndroidRuntime(827): at android.app.ActivityThread.main(ActivityThread.java:5039)
12-24 02:52:36.190: E/AndroidRuntime(827): at java.lang.reflect.Method.invokeNative(Native Method)
12-24 02:52:36.190: E/AndroidRuntime(827): at java.lang.reflect.Method.invoke(Method.java:511)
12-24 02:52:36.190: E/AndroidRuntime(827): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-24 02:52:36.190: E/AndroidRuntime(827): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-24 02:52:36.190: E/AndroidRuntime(827): at dalvik.system.NativeStart.main(Native Method)
12-24 02:52:36.190: E/AndroidRuntime(827): Caused by: java.lang.NullPointerException
12-24 02:52:36.190: E/AndroidRuntime(827): at com.dd.gfit.DatabaseHandler.fetchRoutines(DatabaseHandler.java:51)
12-24 02:52:36.190: E/AndroidRuntime(827): at com.dd.gfit.RoutinesActivity.<init>(RoutinesActivity.java:17)
12-24 02:52:36.190: E/AndroidRuntime(827): at java.lang.Class.newInstanceImpl(Native Method)
12-24 02:52:36.190: E/AndroidRuntime(827): at java.lang.Class.newInstance(Class.java:1319)
12-24 02:52:36.190: E/AndroidRuntime(827): at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
12-24 02:52:36.190: E/AndroidRuntime(827): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
12-24 02:52:36.190: E/AndroidRuntime(827): ... 11 more