尝试将SQLiteDatabase中的数据导入Spinner时获取NullPointerException [Android]

时间:2012-08-30 07:45:11

标签: android nullpointerexception spinner logcat sqlite

Helllo人我在LogCat中有NullPointerException。当我打开我的应用程序时,我会强制关闭。另外,在LogCat中说NullPointerException main.page getAllNames()引起的问题。所以我认为这与我的数据库文件中的getAllNames()方法有关。

我的代码如下: TestSpinnerDB.java

@Override
    public void onCreate(Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.spinner_test);

        Spinner testSpinner = (Spinner) findViewById(R.id.nameSpinner);
        Cursor testCursor = friendDB.getAllNames();
        startManagingCursor(testCursor);

        String[] from = new String[]{BuddyDBAdapter.KEY_NAME};
        int[] to = new int[]{android.R.id.text1};
        SimpleCursorAdapter testAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, testCursor, from, to);
        testAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        testSpinner.setAdapter(testAdapter);

        testSpinner.setOnItemSelectedListener(new OnItemSelectedListener()
            {

                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
                {
                    Cursor c = (Cursor)parent.getItemAtPosition(pos);
                    testSpinnerId = c.getInt(c.getColumnIndexOrThrow(BuddyDBAdapter.KEY_ROWID));

                }

                @Override
                public void onNothingSelected(AdapterView<?> arg0)
                {
                    // TODO Auto-generated method stub

                }
            });

    }

BuddyDBAdapter.java

public class BuddyDBAdapter extends AnniversaryDBAdapter
{
    public static final String KEY_ROWID = "name_id";
    public static final String KEY_NAME = "name";
    private static final String TAG = "DBAdapter";
    private static final String CREATE_TABLE_BUDDIESLIST = "buddiesList";

    public BuddyDBAdapter(Context ctx)
    {
        super(ctx);
    }

    public long insertNames(String name)
    {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_NAME, name);
        return db.insert(CREATE_TABLE_BUDDIESLIST, null, initialValues);
    }

    public boolean deleteName(long rowId)
    {
        return db.delete(CREATE_TABLE_BUDDIESLIST, KEY_ROWID + "=" + rowId, null) > 0;
    }

    public Cursor getAllNames()
    {
        return db.query(CREATE_TABLE_BUDDIESLIST, new String[] { KEY_ROWID, KEY_NAME }, null, null, null, null, null);
    }

    public Cursor getName(long rowId) throws SQLException
    {
        Cursor c = db.query(true, CREATE_TABLE_BUDDIESLIST, new String[] { KEY_ROWID, KEY_NAME }, KEY_ROWID + "=" + rowId, null, null, null, null, null);
        if(c != null)
        {
            c.moveToFirst();
        }
        return c;
    }
}

这是我的LogCat

08-30 15:03:53.361: I/dalvikvm(715): threadid=3: reacting to signal 3
08-30 15:03:53.381: I/dalvikvm(715): Wrote stack traces to '/data/anr/traces.txt'
08-30 15:03:53.781: D/gralloc_goldfish(715): Emulator without GPU emulation detected.
08-30 15:03:53.871: I/dalvikvm(715): threadid=3: reacting to signal 3
08-30 15:03:53.881: I/dalvikvm(715): Wrote stack traces to '/data/anr/traces.txt'
08-30 15:04:06.101: D/AndroidRuntime(715): Shutting down VM
08-30 15:04:06.101: W/dalvikvm(715): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
08-30 15:04:06.121: E/AndroidRuntime(715): FATAL EXCEPTION: main
08-30 15:04:06.121: E/AndroidRuntime(715): java.lang.RuntimeException: Unable to start activity ComponentInfo{main.page/main.page.TestSpinnerDB}: java.lang.NullPointerException
08-30 15:04:06.121: E/AndroidRuntime(715):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
08-30 15:04:06.121: E/AndroidRuntime(715):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
08-30 15:04:06.121: E/AndroidRuntime(715):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
08-30 15:04:06.121: E/AndroidRuntime(715):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
08-30 15:04:06.121: E/AndroidRuntime(715):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-30 15:04:06.121: E/AndroidRuntime(715):  at android.os.Looper.loop(Looper.java:137)
08-30 15:04:06.121: E/AndroidRuntime(715):  at android.app.ActivityThread.main(ActivityThread.java:4424)
08-30 15:04:06.121: E/AndroidRuntime(715):  at java.lang.reflect.Method.invokeNative(Native Method)
08-30 15:04:06.121: E/AndroidRuntime(715):  at java.lang.reflect.Method.invoke(Method.java:511)
08-30 15:04:06.121: E/AndroidRuntime(715):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-30 15:04:06.121: E/AndroidRuntime(715):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-30 15:04:06.121: E/AndroidRuntime(715):  at dalvik.system.NativeStart.main(Native Method)
08-30 15:04:06.121: E/AndroidRuntime(715): Caused by: java.lang.NullPointerException
08-30 15:04:06.121: E/AndroidRuntime(715):  at main.page.BuddyDBAdapter.getAllNames(BuddyDBAdapter.java:35)
08-30 15:04:06.121: E/AndroidRuntime(715):  at main.page.TestSpinnerDB.onCreate(TestSpinnerDB.java:32)
08-30 15:04:06.121: E/AndroidRuntime(715):  at android.app.Activity.performCreate(Activity.java:4465)
08-30 15:04:06.121: E/AndroidRuntime(715):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
08-30 15:04:06.121: E/AndroidRuntime(715):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
08-30 15:04:06.121: E/AndroidRuntime(715):  ... 11 more
08-30 15:04:06.481: I/dalvikvm(715): threadid=3: reacting to signal 3
08-30 15:04:06.501: I/dalvikvm(715): Wrote stack traces to '/data/anr/traces.txt'
08-30 15:04:06.661: I/dalvikvm(715): threadid=3: reacting to signal 3
08-30 15:04:06.681: I/dalvikvm(715): Wrote stack traces to '/data/anr/traces.txt'
08-30 15:04:10.601: I/dalvikvm(739): threadid=3: reacting to signal 3
08-30 15:04:10.621: I/dalvikvm(739): Wrote stack traces to '/data/anr/traces.txt'
08-30 15:04:11.011: D/gralloc_goldfish(739): Emulator without GPU emulation detected.
08-30 15:04:11.111: I/dalvikvm(739): threadid=3: reacting to signal 3
08-30 15:04:11.131: I/dalvikvm(739): Wrote stack traces to '/data/anr/traces.txt'
08-30 15:05:10.671: D/AndroidRuntime(739): Shutting down VM
08-30 15:05:10.671: W/dalvikvm(739): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
08-30 15:05:10.711: E/AndroidRuntime(739): FATAL EXCEPTION: main
08-30 15:05:10.711: E/AndroidRuntime(739): java.lang.RuntimeException: Unable to start activity ComponentInfo{main.page/main.page.TestSpinnerDB}: java.lang.NullPointerException
08-30 15:05:10.711: E/AndroidRuntime(739):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
08-30 15:05:10.711: E/AndroidRuntime(739):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
08-30 15:05:10.711: E/AndroidRuntime(739):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
08-30 15:05:10.711: E/AndroidRuntime(739):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
08-30 15:05:10.711: E/AndroidRuntime(739):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-30 15:05:10.711: E/AndroidRuntime(739):  at android.os.Looper.loop(Looper.java:137)
08-30 15:05:10.711: E/AndroidRuntime(739):  at android.app.ActivityThread.main(ActivityThread.java:4424)
08-30 15:05:10.711: E/AndroidRuntime(739):  at java.lang.reflect.Method.invokeNative(Native Method)
08-30 15:05:10.711: E/AndroidRuntime(739):  at java.lang.reflect.Method.invoke(Method.java:511)
08-30 15:05:10.711: E/AndroidRuntime(739):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-30 15:05:10.711: E/AndroidRuntime(739):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-30 15:05:10.711: E/AndroidRuntime(739):  at dalvik.system.NativeStart.main(Native Method)
08-30 15:05:10.711: E/AndroidRuntime(739): Caused by: java.lang.NullPointerException
08-30 15:05:10.711: E/AndroidRuntime(739):  at main.page.BuddyDBAdapter.getAllNames(BuddyDBAdapter.java:35)
08-30 15:05:10.711: E/AndroidRuntime(739):  at main.page.TestSpinnerDB.onCreate(TestSpinnerDB.java:32)
08-30 15:05:10.711: E/AndroidRuntime(739):  at android.app.Activity.performCreate(Activity.java:4465)
08-30 15:05:10.711: E/AndroidRuntime(739):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
08-30 15:05:10.711: E/AndroidRuntime(739):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
08-30 15:05:10.711: E/AndroidRuntime(739):  ... 11 more
08-30 15:05:10.881: I/dalvikvm(739): threadid=3: reacting to signal 3
08-30 15:05:10.891: I/dalvikvm(739): Wrote stack traces to '/data/anr/traces.txt'
08-30 15:05:11.301: I/dalvikvm(739): threadid=3: reacting to signal 3
08-30 15:05:11.311: I/dalvikvm(739): Wrote stack traces to '/data/anr/traces.txt'
08-30 15:10:10.791: I/Process(739): Sending signal. PID: 739 SIG: 9
08-30 15:34:24.541: I/dalvikvm(1844): threadid=3: reacting to signal 3
08-30 15:34:24.581: I/dalvikvm(1844): Wrote stack traces to '/data/anr/traces.txt'
08-30 15:34:24.861: D/gralloc_goldfish(1844): Emulator without GPU emulation detected.
08-30 15:34:28.141: D/AndroidRuntime(1844): Shutting down VM
08-30 15:34:28.141: W/dalvikvm(1844): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
08-30 15:34:28.161: E/AndroidRuntime(1844): FATAL EXCEPTION: main
08-30 15:34:28.161: E/AndroidRuntime(1844): java.lang.RuntimeException: Unable to start activity ComponentInfo{main.page/main.page.TestSpinnerDB}: java.lang.NullPointerException
08-30 15:34:28.161: E/AndroidRuntime(1844):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
08-30 15:34:28.161: E/AndroidRuntime(1844):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
08-30 15:34:28.161: E/AndroidRuntime(1844):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
08-30 15:34:28.161: E/AndroidRuntime(1844):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
08-30 15:34:28.161: E/AndroidRuntime(1844):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-30 15:34:28.161: E/AndroidRuntime(1844):     at android.os.Looper.loop(Looper.java:137)
08-30 15:34:28.161: E/AndroidRuntime(1844):     at android.app.ActivityThread.main(ActivityThread.java:4424)
08-30 15:34:28.161: E/AndroidRuntime(1844):     at java.lang.reflect.Method.invokeNative(Native Method)
08-30 15:34:28.161: E/AndroidRuntime(1844):     at java.lang.reflect.Method.invoke(Method.java:511)
08-30 15:34:28.161: E/AndroidRuntime(1844):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-30 15:34:28.161: E/AndroidRuntime(1844):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-30 15:34:28.161: E/AndroidRuntime(1844):     at dalvik.system.NativeStart.main(Native Method)
08-30 15:34:28.161: E/AndroidRuntime(1844): Caused by: java.lang.NullPointerException
08-30 15:34:28.161: E/AndroidRuntime(1844):     at main.page.BuddyDBAdapter.getAllNames(BuddyDBAdapter.java:35)
08-30 15:34:28.161: E/AndroidRuntime(1844):     at main.page.TestSpinnerDB.onCreate(TestSpinnerDB.java:32)
08-30 15:34:28.161: E/AndroidRuntime(1844):     at android.app.Activity.performCreate(Activity.java:4465)
08-30 15:34:28.161: E/AndroidRuntime(1844):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
08-30 15:34:28.161: E/AndroidRuntime(1844):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
08-30 15:34:28.161: E/AndroidRuntime(1844):     ... 11 more
08-30 15:34:28.491: I/dalvikvm(1844): threadid=3: reacting to signal 3
08-30 15:34:28.511: I/dalvikvm(1844): Wrote stack traces to '/data/anr/traces.txt'
08-30 15:34:28.722: I/dalvikvm(1844): threadid=3: reacting to signal 3
08-30 15:34:28.731: I/dalvikvm(1844): Wrote stack traces to '/data/anr/traces.txt'
08-30 15:34:30.381: I/Process(1844): Sending signal. PID: 1844 SIG: 9

哦顺便说一句,我已经设法在几周之前将数据显示到spinner,但是最近我在一个数据库文件中创建表后,我无法将数据显示到spinner


我遵循了这个例子Android: populate a Spinner from a SQLite database,这使我能够显示数据。
我再次关注这个例子,我收到NullPointerException并强行关闭。
任何帮助将不胜感激。谢谢
更新
我发布了BuddyDBAdapter.java的完整代码

2 个答案:

答案 0 :(得分:0)

唯一可能的是,

db getAllNames()对象 null   BuddyDBAdapter类的方法 ...

<强> TRY:

public Cursor getAllNames()
{
  if(db != null)
  return db.query(CREATE_TABLE_BUDDIESLIST, new String[] { KEY_ROWID, KEY_NAME }, null, null, null, null, null);
  return null;
}

修改

同时致电

BuddyDBAdapter friendDB = new BuddyDBAdapter(this);

TestSpinnerDB 活动的onCreate()

答案 1 :(得分:0)

您在friendDB中宣布onCreate的地方这就是NullpointerException即将到来的原因。尝试声明所需的BuddyDBAdapter.class. Ex,

Spinner testSpinner = (Spinner) findViewById(R.id.nameSpinner);
BuddyDBAdapter friendDB = new BuddyDBAdapter(); // Declare something like this
Cursor testCursor = friendDB.getAllNames();
startManagingCursor(testCursor);

我认为你刚刚创建了friendDB作为全局变量。而且,没有在任何地方声明发布完整的代码。