Android 旋转屏幕时微调器崩溃

Android spinner crash when you rotate the screen

我有一个微调器的问题,而且我是 android 的新手,所以我不知道该怎么办我已经搜索了好几天但还没有找到任何东西请帮助 D:

asignatura = (Spinner) findViewById(R.id.spinnerAsignaturas);
    modificar = 0;
    // fin inflates
    Cursor cursor = getContentResolver().query(
            BDProvider.ASIGNATURAS_CONTENT_URI,
            new String[] { BDProvider.ID, BDProvider.ASIGNATURA_NOMBRE },
            null, null, BDProvider.ID);
    SimpleCursorAdapter adaptador = new SimpleCursorAdapter(this,
            android.R.layout.simple_spinner_item, cursor,
            new String[] { BDProvider.ASIGNATURA_NOMBRE },
            new int[] { android.R.id.text1 });

    adaptador
            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    asignatura.setAdapter(adaptador);
    asignatura.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            idAsignatura = (String) ((TextView) arg1).getText();
            setIdAsignatura(idAsignatura);
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {}

    });

日志:

  01-06 14:25:54.790: E/AndroidRuntime(5170): java.lang.NullPointerException
01-06 14:25:54.790: E/AndroidRuntime(5170):     at es.uvigo.esei.dm.p06.InsertarTarea.onItemSelected(InsertarTarea.java:85)
01-06 14:25:54.790: E/AndroidRuntime(5170):     at android.widget.AdapterView.fireOnSelected(AdapterView.java:893)
01-06 14:25:54.790: E/AndroidRuntime(5170):     at android.widget.AdapterView.selectionChanged(AdapterView.java:880)
01-06 14:25:54.790: E/AndroidRuntime(5170):     at android.widget.AdapterView.checkSelectionChanged(AdapterView.java:1043)
01-06 14:25:54.790: E/AndroidRuntime(5170):     at android.widget.AdapterView.handleDataChanged(AdapterView.java:1023)
01-06 14:25:54.790: E/AndroidRuntime(5170):     at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:180)
01-06 14:25:54.790: E/AndroidRuntime(5170):     at android.widget.Spinner.onMeasure(Spinner.java:482)
01-06 14:25:54.790: E/AndroidRuntime(5170):     at android.view.View.measure(View.java:16458)
01-06 14:25:54.790: E/AndroidRuntime(5170):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
01-06 14:25:54.790: E/AndroidRuntime(5170):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
01-06 14:25:54.790: E/AndroidRuntime(5170):     at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1052)
01-06 14:25:54.790: E/AndroidRuntime(5170):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:590)
01-06 14:25:54.790: E/AndroidRuntime(5170):     at android.view.View.measure(View.java:16458)
01-06 14:25:54.790: E/AndroidRuntime(5170):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
01-06 14:25:54.790: E/AndroidRuntime(5170):     at android.widget.LinearLayout.forceUniformWidth(LinearLayout.java:940)
01-06 14:25:54.790: E/AndroidRuntime(5170):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:920)
01-06 14:25:54.790: E/AndroidRuntime(5170):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
01-06 14:25:54.790: E/AndroidRuntime(5170):     at android.view.View.measure(View.java:16458)
01-06 14:25:54.790: E/AndroidRuntime(5170):     at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
01-06 14:25:54.790: E/AndroidRuntime(5170):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)
01-06 14:25:54.790: E/AndroidRuntime(5170):     at android.view.View.measure(View.java:16458)
01-06 14:25:54.790: E/AndroidRuntime(5170):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
01-06 14:25:54.790: E/AndroidRuntime(5170):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-06 14:25:54.790: E/AndroidRuntime(5170):     at android.view.View.measure(View.java:16458)

它说 onItemSelected 上的视图为空,但我不知道为什么,我认为光标在设置适配器之前正在获取数据....

我假设您的 TextView 为空且 getText() returns 为空。不要直接转换为字符串,而是尝试类似的方法:

CharSequence cidAsignatura = ((TextView) arg1).getText();
if (cidAsignatura != null) {
  setIdAsignatura(cidAsignatura.toString());
}

好的,我只是找到了一个简单但不确定是否是使其工作的有效方法,但是如果您

@Override
    public void onItemSelected(AdapterView<?> arg0, View arg1,
            int arg2, long arg3) {
        idAsignatura = (String) ((TextView) arg1).getText();
        setIdAsignatura(idAsignatura);
    }

拿 "arg1" 然后你这样做:

@Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {

            if(arg1!=null){
                setIdAsignatura((String) ((TextView) arg1).getText());
            }
        }

不会崩溃! 感谢 Rares Barbatan。