我有一个微调器这个问题+我是新的关于android所以idk做什么我已经搜索了几天没有找到任何东西但是PLZ帮助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$2.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上的视图为null但我不知道为什么,我认为光标是在设置适配器之前获取数据....
答案 0 :(得分:0)
我假设您的TextView为空,getText()返回null。而不是直接转换为String,尝试类似:
CharSequence cidAsignatura = ((TextView) arg1).getText();
if (cidAsignatura != null) {
setIdAsignatura(cidAsignatura.toString());
}
答案 1 :(得分:0)
好的,我发现一个简单的但不确定这是一个有效的方法,使它工作,但如果你
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
idAsignatura = (String) ((TextView) arg1).getText();
setIdAsignatura(idAsignatura);
}
采取&#34; arg1&#34;你这样做:
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
if(arg1!=null){
setIdAsignatura((String) ((TextView) arg1).getText());
}
}
它不会崩溃!
谢谢Rares Barbatan。