0

大家好,我NullPointerException在 LogCat 中遇到了。当我打开我的应用程序时,我强制关闭。另外,在 LogCat 中说 Caused by NullPointerExceptionat 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:从 SQLite 数据库填充 Spinner,这使我能够显示数据。
我再次按照该示例进行操作,并且已收到NullPointerException并强制关闭。
任何帮助将不胜感激。谢谢
更新
我已经发布了 BuddyDBAdapter.java 的完整代码

4

2 回答 2

0

唯一可能的事情是,

您的db对象在BuddyDBAdapter 类的 方法中为null ...getAllNames()

尝试:

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);

TestSpinnerDBonCreate()活动中..

于 2012-08-30T07:47:09.857 回答
0

你在哪里声明friendDBonCreate这就是那个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为全局变量。而且,没有在任何地方声明。发布完整代码。

于 2012-08-30T07:49:15.367 回答