Witam ludzi, których mam NullPointerException w LogCat. Kiedy otwieram aplikację, zbliżam się siłą. Plus, w LogCat mówi, że Spowodowane przez NullPointerException o main.page getAllNames(). Pomyślałem więc, że ma to coś wspólnego z moją metodą getAllNames() w moim pliku bazy danych.

Moje kody są poniżej: 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;
    }
}

To jest mój 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

A tak przy okazji, udało mi się wyświetlić dane w spinner, tak jak kilka tygodni temu, ale ostatnio po utworzeniu tabel w jednym pliku bazy danych nie mogłem wyświetlić danych w spinner.


Podążałem za tym przykładem Android: zapełnij Spinnera z bazy danych SQLite, co umożliwiło mi wyświetlanie danych.
Ponownie poszedłem za tym przykładem i otrzymałem NullPointerException i wymusiłem zamknięcie.
Każda pomoc będzie bardzo mile widziana. Dziękuję
AKTUALIZUJ
Wysłałem pełny kod BuddyDBAdapter.java

0
Preeyah 30 sierpień 2012, 11:45

2 odpowiedzi

Najlepsza odpowiedź

Jedyną możliwą rzeczą jest,

Twój obiekt db jest null w getAllNames() metoda Klasy BuddyDBAdapter...

spróbuj:

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

EDYTUJ:

Także zadzwoń

BuddyDBAdapter friendDB = new BuddyDBAdapter(this);

W onCreate() aktywności TestSpinnerDB..

0
user370305 30 sierpień 2012, 11:58

Gdzie zadeklarowałeś friendDB w swoim onCreate To jest powód, dla którego NullpointerException nadchodzi. Spróbuj zadeklarować wymagany BuddyDBAdapter.class. Ex,

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

Myślę, że właśnie utworzyłeś friendDB jako zmienną globalną. I nigdzie nie zadeklarowany. Opublikuj pełny kod.

0
Praveenkumar 30 sierpień 2012, 11:49