ListActivity不会在ListView中显示任何条目

时间:2014-04-23 06:40:02

标签: java android listview listviewitem

这似乎是一个经常遇到的问题,但无法找到我所提供的解决方案。

所以我有一个Activity,它扩展了ListActivity及其中的以下代码:

super.onCreate(savedInstanceState);



        db = SQLiteDatabase.openDatabase(playersDatabaseName, null, SQLiteDatabase.CREATE_IF_NECESSARY | SQLiteDatabase.OPEN_READWRITE);
        SQLStatement = "create table if not exists Players_And_Scores(_id integer primary key autoincrement, Player String, Score String);";
        db.execSQL(SQLStatement);


        cv.put("Player", WelcomeActivity.playersName);
        cv.put("Score", GameActivity.currentScore);
        try
        {
            db.insertOrThrow("Players_And_Scores", null, cv);
        }
        catch (Exception e)
        {
            Log.i("SQLiteException", cv.toString() + " is there already");
        }

        c = db.query("Players_And_Scores", null, null, null, null, null, null, null);
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(getApplicationContext(), android.R.layout.simple_list_item_1,
                                                              c, new String[] {"Player", "Score"}, new int[] {android.R.id.text1}, 0);

        this.setListAdapter(adapter);

这会设置一个列表视图,每次添加条目时,都会在数据库中添加一个新行,并将新行添加到listwiev。但是所有的线都是空的。所以它识别行数,但不识别内容。

我在想是否应该向xml添加一些东西,所以我一直在玩它而没有结果。 这是我目前用于xml的内容:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <!-- the android:id is important -->
    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>

</LinearLayout>

任何想法都将不胜感激。

修改 这是logcat:

  04-23 03:05:16.083    3332-3332/statesgame.app D/dalvikvm﹕ Not late-enabling CheckJNI (already on)
    04-23 03:05:16.193    3332-3332/statesgame.app I/can read﹕ true
    04-23 03:05:16.193    3332-3332/statesgame.app I/this﹕ State                   Capital
    04-23 03:05:16.193    3332-3332/statesgame.app I/this﹕ ----------------        ---------------
    04-23 03:05:16.193    3332-3332/statesgame.app I/this﹕ Alabama                 Montgomery
   State=Wyoming
    04-23 03:05:16.473    3332-3332/statesgame.app I/SQLiteException﹕ Capital=Cheyenne State=Wyoming is there already
    04-23 03:05:16.483    3332-3332/statesgame.app I/db﹕ []
    04-23 03:05:16.513    3332-3332/statesgame.app D/dalvikvm﹕ GC_FOR_ALLOC freed 485K, 16% free 3261K/3856K, paused 5ms, total 13ms
    04-23 03:05:16.533    3332-3332/statesgame.app D/﹕ HostConnection::get() New Host Connection established 0xb88f79f0, tid 3332
    04-23 03:05:16.573    3332-3332/statesgame.app W/EGL_emulation﹕ eglSurfaceAttrib not implemented
    04-23 03:05:16.573    3332-3332/statesgame.app D/OpenGLRenderer﹕ Enabling debug mode 0
    04-23 03:05:17.453    3332-3332/statesgame.app D/dalvikvm﹕ GC_FOR_ALLOC freed 52K, 15% free 3298K/3856K, paused 3ms, total 4ms
    04-23 03:05:17.453    3332-3332/statesgame.app I/enteract﹕ act
    04-23 03:05:17.533    3332-3332/statesgame.app W/EGL_emulation﹕ eglSurfaceAttrib not implemented
    04-23 03:05:18.813    3332-3332/statesgame.app I/buttn﹕ Click
    04-23 03:05:18.813    3332-3332/statesgame.app I/cursorEntirs﹕ 3
    04-23 03:05:18.813    3332-3332/statesgame.app I/cCount﹕ 50
    04-23 03:05:19.143    3332-3332/statesgame.app I/buttn﹕ Click
    04-23 03:05:19.143    3332-3332/statesgame.app I/cursorEntirs﹕ 33
    04-23 03:05:19.143    3332-3332/statesgame.app I/cCount﹕ 50
    04-23 03:05:19.313    3332-3332/statesgame.app I/buttn﹕ Click
    04-23 03:05:19.313    3332-3332/statesgame.app I/cursorEntirs﹕ 34
    04-23 03:05:19.313    3332-3332/statesgame.app I/cCount﹕ 50
    04-23 03:05:19.483    3332-3332/statesgame.app I/buttn﹕ Click
    04-23 03:05:19.483    3332-3332/statesgame.app I/cursorEntirs﹕ 5
    04-23 03:05:19.483    3332-3332/statesgame.app I/cCount﹕ 50
    04-23 03:05:19.623    3332-3332/statesgame.app I/buttn﹕ Click
    04-23 03:05:19.623    3332-3332/statesgame.app I/cursorEntirs﹕ 2
    04-23 03:05:19.623    3332-3332/statesgame.app I/cCount﹕ 50
    04-23 03:05:19.743    3332-3332/statesgame.app I/buttn﹕ Click
    04-23 03:05:19.893    3332-3332/statesgame.app I/buttn﹕ Click
    04-23 03:05:20.063    3332-3332/statesgame.app I/buttn﹕ Click
    04-23 03:05:20.213    3332-3332/statesgame.app I/buttn﹕ Click
    04-23 03:05:20.533    3332-3332/statesgame.app I/buttn﹕ Click
    04-23 03:05:20.763    3332-3332/statesgame.app I/buttn﹕ Click
    04-23 03:05:20.793    3332-3332/statesgame.app D/dalvikvm﹕ GC_FOR_ALLOC freed 30K, 13% free 3427K/3896K, paused 2ms, total 3ms
    04-23 03:05:20.793    3332-3332/statesgame.app D/dalvikvm﹕ GC_FOR_ALLOC freed 5K, 12% free 3520K/3996K, paused 2ms, total 2ms
    04-23 03:05:20.803    3332-3332/statesgame.app I/dalvikvm-heap﹕ Grow heap (frag case) to 4.618MB for 1127532-byte allocation
    04-23 03:05:20.823    3332-3341/statesgame.app D/dalvikvm﹕ GC_FOR_ALLOC freed <1K, 10% free 4621K/5100K, paused 21ms, total 21ms
    04-23 03:05:20.873    3332-3332/statesgame.app W/EGL_emulation﹕ eglSurfaceAttrib not implemented
    04-23 03:09:38.607    3439-3439/statesgame.app D/AndroidRuntime﹕ Shutting down VM
    04-23 03:09:38.607    3439-3439/statesgame.app W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb2ce5b20)
    04-23 03:09:38.607    3439-3439/statesgame.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
        Process: statesgame.app, PID: 3439
        java.lang.RuntimeException: Unable to instantiate application android.app.Application: java.lang.IllegalStateException: Unable to get package info for statesgame.app; is package not installed?
                at android.app.LoadedApk.makeApplication(LoadedApk.java:516)
                at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4317)
                at android.app.ActivityThread.access$1500(ActivityThread.java:135)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
                at android.os.Handler.dispatchMessage(Handler.java:102)
                at android.os.Looper.loop(Looper.java:136)
                at android.app.ActivityThread.main(ActivityThread.java:5017)
                at java.lang.reflect.Method.invokeNative(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:515)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
                at dalvik.system.NativeStart.main(Native Method)
         Caused by: java.lang.IllegalStateException: Unable to get package info for statesgame.app; is package not installed?
                at android.app.LoadedApk.initializeJavaContextClassLoader(LoadedApk.java:376)
                at android.app.LoadedApk.getClassLoader(LoadedApk.java:329)
                at android.app.LoadedApk.makeApplication(LoadedApk.java:508)
                at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4317)
                at android.app.ActivityThread.access$1500(ActivityThread.java:135)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
                at android.os.Handler.dispatchMessage(Handler.java:102)
                at android.os.Looper.loop(Looper.java:136)
                at android.app.ActivityThread.main(ActivityThread.java:5017)
                at java.lang.reflect.Method.invokeNative(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:515)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
                at dalvik.system.NativeStart.main(Native Method)
    04-23 03:09:39.737    3468-3468/statesgame.app D/dalvikvm﹕ Not late-enabling CheckJNI (already on)
    04-23 03:09:39.787    3468-3468/statesgame.app I/can read﹕ true
    04-23 03:09:39.797    3468-3468/statesgame.app I/this﹕ State                   Capital
    04-23 03:09:39.797    3468-3468/statesgame.app I/this﹕ ----------------        ---------------
    04-23 03:09:39.797    3468-3468/statesgame.app I/this﹕ Alabama                 Montgomery
    04-23 03:09:39.817    3468-3468/statesgame.app D/dalvikvm﹕ GC_FOR_ALLOC freed 109K, 7% free 3230K/3448K, paused 18ms, total 19ms
    04-23 03:09:39.827    3468-3468/statesgame.app I/SQLiteException﹕ Capital=Montgomery State=Alabama is there already

    04-23 03:10:05.507    3468-3468/statesgame.app D/dalvikvm﹕ GC_FOR_ALLOC freed 75K, 12% free 3428K/3892K, paused 3ms, total 3ms
    04-23 03:10:05.507    3468-3468/statesgame.app D/dalvikvm﹕ GC_FOR_ALLOC freed 8K, 12% free 3518K/3992K, paused 2ms, total 2ms
    04-23 03:10:05.507    3468-3468/statesgame.app I/dalvikvm-heap﹕ Grow heap (frag case) to 4.616MB for 1127532-byte allocation
    04-23 03:10:05.527    3468-3477/statesgame.app D/dalvikvm﹕ GC_FOR_ALLOC freed <1K, 10% free 4619K/5096K, paused 13ms, total 13ms
    04-23 03:10:05.577    3468-3468/statesgame.app W/EGL_emulation﹕ eglSurfaceAttrib not implemented
    04-23 03:18:27.315    3575-3575/statesgame.app W/EGL_emulation﹕ eglSurfaceAttrib not implemented
    04-23 03:18:29.785    3575-3575/statesgame.app D/dalvikvm﹕ GC_FOR_ALLOC freed 276K, 11% free 3456K/3844K, paused 3ms, total 3ms
    04-23 03:18:29.785    3575-3575/statesgame.app D/dalvikvm﹕ GC_FOR_ALLOC freed 36K, 11% free 3518K/3944K, paused 2ms, total 3ms
    04-23 03:18:29.785    3575-3575/statesgame.app I/dalvikvm-heap﹕ Grow heap (frag case) to 4.616MB for 1127532-byte allocation
    04-23 03:18:29.805    3575-3584/statesgame.app D/dalvikvm﹕ GC_FOR_ALLOC freed <1K, 9% free 4619K/5048K, paused 12ms, total 12ms
    04-23 03:18:29.875    3575-3575/statesgame.app W/EGL_emulation﹕ eglSurfaceAttrib not implemented

修改 添加了清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="statesgame.app" >

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="statesgame.app.WelcomeActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="statesgame.app.GameActivity"
            android:label="@string/title_activity_game" >
        </activity>
        <activity
            android:name="statesgame.app.ScoresActivity"
            android:label="@string/title_activity_scores" >
        </activity>
    </application>

</manifest>

My activity shown as such

我以这种方式开始我的活动:

 Intent intent = new Intent(getApplicationContext(), ScoresActivity.class);
        startActivity(intent);

2 个答案:

答案 0 :(得分:0)

试试如下:

      Cursor cursor = db.query("Players_And_Scores", new String[] 
     {"Player","Score"}, null, null, null);
       startManagingCursor(cursor);

        // THE DESIRED COLUMNS TO BE BOUND
        String[] columns = new String[] { "Player", "Score"};
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(getApplicationContext(), android.R.layout.simple_expandable_list_item_2,
         cursor , columns, new int[] {android.R.id.text1,android.R.id.text2}, 0);

希望这对你有用。

答案 1 :(得分:0)

我找到了解决方案。很奇怪,我只是将我的光标对象更改为:

c = db.query("Players_And_Scores", new String[]{"_id", "Player", "Score"}, null, null, null, null, null, null);

添加我的数据库的所有行显式解决了问题,但遗憾的是只显示了一个条目"Players"。为了在ListView行中放置更多条目,我可能需要创建一个自定义适配器。