开发者

NullPointerException

So I'm trying to populate a spinner from a database and I am constantly getting a NullPointerException error. I tried this code once before and it worked just fine but I attempted to change things around and now when I try to do the code again it throws an error.

LogCat:

E/AndroidRuntime( 5406): FATAL EXCEPTION: main
E/AndroidRuntime( 5406): java.lang.RuntimeException: Unable to resume activity {
com.phearx.assignments/com.phearx.assignments.AssignmentEditActivity}: java.lang
.NullPointerException
E/AndroidRuntime( 5406):        at android.app.ActivityThread.performResumeActiv
ity(ActivityThread.java:2241)
E/AndroidRuntime( 5406):        at android.app.ActivityThread.handleResumeActivi
ty(ActivityThread.java:2256)
E/AndroidRuntime( 5406):        at android.app.ActivityThread.handleLaunchActivi
ty(ActivityThread.java:1789)
E/AndroidRuntime( 5406):        at android.app.ActivityThread.access$1500(Activi
tyThread.java:123)
E/AndroidRuntime( 5406):        at android.app.ActivityThread$H.handleMessage(Ac
tivityThread.java:939)
E/AndroidRuntime( 5406):        at android.os.Handler.dispatchMessage(Handler.ja
va:99)
E/AndroidRuntime( 5406):        at android.os.Looper.loop(Looper.java:130)
E/AndroidRuntime( 5406):        at android.app.ActivityThread.main(ActivityThrea
d.java:3835)
E/AndroidRuntime( 5406):        at java.lang.reflect.Method.invokeNative(Native
Method)
E/AndroidRuntime( 5406):        at java.lang.reflect.Method.invoke(Method.java:5
07)
E/AndroidRuntime( 5406):        at com.android.internal.os.ZygoteInit$MethodAndA
rgsCaller.run(ZygoteInit.java:847)
E/AndroidRuntime( 5406):        at com.android.internal.os.ZygoteInit.main(Zygot
eInit.java:605)
E/AndroidRuntime( 5406):        at dalvik.system.NativeStart.main(Native Method)

E/AndroidRuntime( 5406): Caused by: java.lang.NullPointerException
E/AndroidRuntime( 5406):        at com.phearx.assignments.AssignmentEditActivity
.fillSpinner(AssignmentEditActivity.java:91)
E/AndroidRuntime( 5406):        at com.phearx.assignments.AssignmentEditActivity
.onResume(AssignmentEditActivity.java:115)
E/AndroidRuntime( 5406):        at android.app.Instrumentation.callActivityOnRes
ume(Instrumentation.java:1150)
E/AndroidRuntime( 5406):        at android.app.Activity.performResume(Activity.j
ava:3832)
E/AndroidRuntime( 5406):        at android.app.ActivityThread.performResumeActiv
ity(ActivityThread.java:2231)
E/AndroidRuntime( 5406):        ... 12 more
W/ActivityManager( 1323):   Force finishing activity com.phearx.assignments/.Ass
ignmentEditActivity
W/ActivityManager( 1323):   Force finishing activity com.phearx.assignments/.Cla
ssActivity
W/ActivityManager( 1323): Activity pause timeout for HistoryRecord{407da4c0 com.
phearx.assignments/.AssignmentEditActivity}
W/ActivityManager( 1323): Launch timeout has expired, giving up wake lock!

开发者_JS百科AssignmentsEditActivity Class:

    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            mDbHelper = new RemindersDbAdapter(this);
            mDbHelper.open();
            setContentView(R.layout.reminder_edit);


            mCalendar = Calendar.getInstance();
            mTitleText = (EditText) findViewById(R.id.title);
            mBodyText = (EditText) findViewById(R.id.body);
            mDateButton = (Button) findViewById(R.id.reminder_date);
            mTimeButton = (Button) findViewById(R.id.reminder_time);
            mConfirmButton = (Button) findViewById(R.id.confirm);
            mClassSpinner = (Spinner) findViewById(R.id.class_spinner);

            mRowId = savedInstanceState != null 
                ? savedInstanceState.getLong(RemindersDbAdapter.CKEY_ROWID)
                : null;
                registerButtonListenersAndSetDefaultText();



    }
    private void fillSpinner(){
        mDbHelper.open();
        Cursor c = mDbHelper.fetchAllClasses();
        startManagingCursor(c);
         c.moveToFirst();
        Spinner classSpinner = (Spinner) findViewById(R.id.class_spinner);
        // create an array to specify which fields we want to display
        String[] from = new String[]{RemindersDbAdapter.CKEY_NAME};
        // create an array of the display item we want to bind our data to
        int[] to = new int[]{android.R.id.text1};
        // create simple cursor adapter
        SimpleCursorAdapter adapter =
          new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, c, from, to );
        adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
        // get reference to our spinner


        classSpinner.setAdapter(adapter); <--- line 91
        }


    private void setRowIdFromIntent() { 
        if (mRowId == null) {
        Bundle extras = getIntent().getExtras();
        mRowId = extras != null
        ? extras.getLong(RemindersDbAdapter.AKEY_ROWID)
        : null;
    }
}

    @Override
    protected void onPause() {
    super.onPause();
    mDbHelper.close(); 
    }
    @Override
    protected void onResume() {
    super.onResume();
    mDbHelper.open(); 
    setRowIdFromIntent(); 
    populateFields(); 
    fillSpinner();  <--- line 115

    }

I am unsure why I am getting this error. I have an assignmentlistactivity that populates a listview from this exact database table and it populates from the database just fine... HELP!


You don't need this line in fillSpinner() Spinner classSpinner = (Spinner) findViewById(R.id.class_spinner);

and update line 91 from

classSpinner.setAdapter(adapter);

to

mClassSpinner.setAdapter(adapter);

as you already got reference to class_spinner in onCreate(). Also you are opening database twice onResume.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜