首页 文章

空指针异常

提问于
浏览
-2

所以我试图从数据库填充一个微调器,我不断收到NullPointerException错误 . 我之前尝试过这个代码,它工作得很好,但我试图改变现状,现在当我再次尝试再做代码时,它会抛出一个错误 .

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!

AssignmentsEditActivity类:

@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

    }

我不确定为什么我收到这个错误 . 我有一个assignmentlistactivity,从这个确切的数据库表填充listview,它从数据库中填充就好......帮助!

1 回答

  • 0

    你在fillSpinner()中不需要这一行Spinner classSpinner =(Spinner)findViewById(R.id.class_spinner);

    并更新第91行

    classSpinner.setAdapter(适配器);

    mClassSpinner.setAdapter(适配器);

    因为你已经在onCreate()中引用了class_spinner . 您还在onResume上打开数据库两次 .

相关问题