旋转屏幕时获取android Spinner NullPointerException
嗨!伙计们,我在旋转器在旋转屏幕时抛出nullPointerExpression时遇到问题
<com.weiwangcn.betterspinner.library.material.MaterialBetterSpinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/department_spinner"
android:hint="@string/select_department"
android:gravity="center"
android:drawableEnd="@drawable/spinner_icon"
android:textColorHighlight="@color/colorButton"
android:textColor="@color/colorButton"
android:theme="@style/AppTheme"
app:met_floatingLabel="highlight"
android:layout_marginStart="25dp"
android:layout_marginEnd="25dp"
android:layout_marginTop="5dp"/>
在我的onCreateView内
//MaterialBetterSpinner code goes from here
this.materialDesignSpinner = (MaterialBetterSpinner) findViewById(R.id.department_spinner);
materialDesignSpinner.setHintTextColor(Color.parseColor("#00BE84"));
materialDesignSpinner.setTextColor(Color.parseColor("#00BE84"));
materialDesignSpinner.setTextSize(17);
materialDesignSpinner.setHighlightColor(Color.parseColor("#00BE84"));
materialDesignSpinner.setUnderlineColor(Color.parseColor("#00BE84"));
callDepartment= service.getAllOpdIpdDepartments();
ArrayAdapter<String> departmentAdapter = new ArrayAdapter<>(OpdIpdAnalysis.this, android.R.layout.simple_dropdown_item_1line, new ArrayList<String>());
materialDesignSpinner.setAdapter(departmentAdapter);
callDepartment.enqueue(new Callback<List<OpdIpdDepartmentModel>>() {
@Override
public void onResponse(Call<List<OpdIpdDepartmentModel>> call, Response<List<OpdIpdDepartmentModel>> response) {
departmentLists = response.body();
if (departmentLists != null){
for (int i = 0; i<departmentLists.size(); i++){
spinnerLists.add(departmentLists.get(i).getName());
}
}
ArrayAdapter<String> adapter = (ArrayAdapter<String>) materialDesignSpinner.getAdapter();
adapter.clear();
adapter.addAll(spinnerLists);
adapter.notifyDataSetChanged();
}
这是日志记录器:
E / Android运行时:致命异常:主要 流程:com.example.ritecare,PID:15345 java.lang.NullPointerException:尝试调用虚拟方法'void android.widget.Filter.filter(java.lang.CharSequence, android.widget.Filter $ FilterListener)'引用空对象 在android.widget.AutoCompleteTextView.performFiltering(AutoCompleteTextView.java:1026) 在com.weiwangcn.betterspinner.library.material.MaterialBetterSpinner.onFocusChanged(MaterialBetterSpinner.java:49) 在android.view.View.handleFocusGainInternal(View.java:7478) 在android.view.View.requestFocusNoSearch(View.java:12441) 在android.view.View.requestFocus(View.java:12415) 在android.view.View.requestFocus(View.java:12382) 在android.view.View.requestFocus(View.java:12324) 在com.android.internal.policy.PhoneWindow.restoreHierarchyState(PhoneWindow.java:2166) 在android.app.Activity.onRestoreInstanceState(Activity.java:1563) 在android.app.Activity.performRestoreInstanceState(Activity.java:1518) 在android.app.Instrumentation.callActivityOnRestoreInstanceState(Instrumentation.java:1346) 在android.app.ActivityThread.handleStartActivity(ActivityThread.java:3310) 在android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:221) 在android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:201) 在android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:173) 在android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2016) 在android.os.Handler.dispatchMessage(Handler.java:107) 在android.os.Looper.loop(Looper.java:214) 在android.app.ActivityThread.main(ActivityThread.java:7356) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:492) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
答案 0 :(得分:1)
尝试在 MaterialBetterSpinner 初始化期间添加此内容:
ArrayAdapter<String> departmentAdapter = new ArrayAdapter<>(OpdIpdAnalysis.this, android.R.layout.simple_dropdown_item_1line, new ArrayList<String>());
materialDesignSpinner.setAdapter(departmentAdapter);
在 onResponse 内:
ArrayAdapter<String> adapter = (ArrayAdapter<String>) materialDesignSpinner.getAdapter();
adapter.clear()
adapter.addAll(spinnerLists)
adapter.notifyDataSetChanged()
答案 1 :(得分:0)