2

アセットに保存したデータベースを使用してリストビューを作成しようとしています。アプリを実行したときにデータベースが作成されているかどうかを確認するところまで来ました(これはそうです)。私の現在の問題は、列を取得してリストビューに表示する方法を見つけようとしています。理解できないコードが表示された場合に備えて、私の列は名前、役職、部門です。誰かが私を助けることができるなら、私は本当に 感謝しています、ありがとうアドバンスで。

編集:コードを実行すると、null ポインター例外が発生します

    public class Favorites extends ListActivity {
        public static String tableName = "fac";
        private ListView lv;
        SQLiteDatabase newDB;
        private ArrayList<String> results = new ArrayList<String>();

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.favorites);

            openAndQueryDatabase();
            displayResultList();
        }

        private void displayResultList() {
            TextView tView = new TextView(this);
            tView.setText("This data is retrieved from the database and only 4 "
                    + "of the results are displayed");
            getListView().addHeaderView(tView);

            setListAdapter(new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_1, results));
            getListView().setTextFilterEnabled(true);

        }

        private void openAndQueryDatabase() {
            try {
                DataBaseHelper dbHelper = new DataBaseHelper(
                        this.getApplicationContext());
                newDB = dbHelper.getWritableDatabase();
                Cursor c = newDB.rawQuery("SELECT name, position FROM " + tableName
                        + "where ", null);

                if (c != null) {
                    if (c.moveToFirst()) {
                        do {
                            String name = c.getString(c.getColumnIndex("name"));
                            String position = c.getString(c
                                    .getColumnIndex("position"));
                            results.add("name: " + name + ",Position " + position);
                        } while (c.moveToNext());
                    }
                }
            } catch (SQLiteException se) {
            } finally {
                if (newDB != null)
{
                    newDB.execSQL("DELETE FROM " + tableName);
                newDB.close();
            }}

        }
    }

LogCat:

     04-24 04:43:19.064: E/AndroidRuntime(1644): FATAL EXCEPTION: main
        04-24 04:43:19.064: E/AndroidRuntime(1644): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cerealBarApps/com.cerealBarApps.Faculty_tab}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cerealBarApps/com.cerealBarApps.Favorites}: java.lang.NullPointerException
        04-24 04:43:19.064: E/AndroidRuntime(1644):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1816)
        04-24 04:43:19.064: E/AndroidRuntime(1644):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1837)
        04-24 04:43:19.064: E/AndroidRuntime(1644):     at android.app.ActivityThread.access$1500(ActivityThread.java:132)
        04-24 04:43:19.064: E/AndroidRuntime(1644):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1033)
        04-24 04:43:19.064: E/AndroidRuntime(1644):     at android.os.Handler.dispatchMessage(Handler.java:99)
        04-24 04:43:19.064: E/AndroidRuntime(1644):     at android.os.Looper.loop(Looper.java:143)
        04-24 04:43:19.064: E/AndroidRuntime(1644):     at android.app.ActivityThread.main(ActivityThread.java:4196)
        04-24 04:43:19.064: E/AndroidRuntime(1644):     at java.lang.reflect.Method.invokeNative(Native Method)
        04-24 04:43:19.064: E/AndroidRuntime(1644):     at java.lang.reflect.Method.invoke(Method.java:507)
        04-24 04:43:19.064: E/AndroidRuntime(1644):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
        04-24 04:43:19.064: E/AndroidRuntime(1644):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
        04-24 04:43:19.064: E/AndroidRuntime(1644):     at dalvik.system.NativeStart.main(Native Method)
        04-24 04:43:19.064: E/AndroidRuntime(1644): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cerealBarApps/com.cerealBarApps.Favorites}: java.lang.NullPointerException
        04-24 04:43:19.064: E/AndroidRuntime(1644):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1816)
        04-24 04:43:19.064: E/AndroidRuntime(1644):     at android.app.ActivityThread.startActivityNow(ActivityThread.java:1651)
        04-24 04:43:19.064: E/AndroidRuntime(1644):     at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
        04-24 04:43:19.064: E/AndroidRuntime(1644):     at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
        04-24 04:43:19.064: E/AndroidRuntime(1644):     at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:656)
        04-24 04:43:19.064: E/AndroidRuntime(1644):     at android.widget.TabHost.setCurrentTab(TabHost.java:326)
        04-24 04:43:19.064: E/AndroidRuntime(1644):     at android.widget.TabHost.addTab(TabHost.java:216)
        04-24 04:43:19.064: E/AndroidRuntime(1644):     at com.cerealBarApps.Faculty_tab.onCreate(Faculty_tab.java:55)
        04-24 04:43:19.064: E/AndroidRuntime(1644):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
        04-24 04:43:19.064: E/AndroidRuntime(1644):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1780)
        04-24 04:43:19.064: E/AndroidRuntime(1644):     ... 11 more
        04-24 04:43:19.064: E/AndroidRuntime(1644): Caused by: java.lang.NullPointerException
04-24 04:43:19.064: E/AndroidRuntime(1644):     at com.cerealBarApps.Favorites.openAndQueryDatabase(Favorites.java:63)
04-24 04:43:19.064: E/AndroidRuntime(1644):     at com.cerealBarApps.Favorites.onCreate(Favorites.java:25)
04-24 04:43:19.064: E/AndroidRuntime(1644):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
04-24 04:43:19.064: E/AndroidRuntime(1644):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1780)

ごめん

4

1 に答える 1

0

これを試してください.. 以下のステートメントでは、'where' キーワードの前に ' ' を入れてください。

Cursor c = newDB.rawQuery("SELECT name, position FROM " + tableName + "where ", null);

新しい

Cursor c = newDB.rawQuery("SELECT name, position FROM " + tableName + " where ", null);
于 2012-04-24T09:10:46.540 に答える