我使用的是com.weiwangcn.betterspinner
,但我沒有用XML文件聲明,而是以編程方式創建它。以下是我的代碼,簡短。MaterialBetterSpinner在點擊時崩潰
final MaterialBetterSpinner spinnerSoftener = new MaterialBetterSpinner(getActivity());
spinnerSoftener.setId((View.generateViewId()));
spinnerSoftener.setHint("Softener");
spinnerSoftener.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
List<Mill_Softener_Type> softenerList = presenter.getSoftener();
if(softenerList.size() == 0)
{
spinnerSoftener.setAdapter(new ArrayAdapter<>(getActivity(), android.R.layout.simple_spinner_dropdown_item, new ArrayList<>()));
spinnerSoftener.setEnabled(false);
}
else
{
System.out.println("softener size: " + softenerList.size());
spinnerSoftener.setAdapter(new ArrayAdapter<>(getActivity(), android.R.layout.simple_spinner_dropdown_item, softenerList));
spinnerSoftener.setEnabled(true);
}
container.addView(spinnerSoftener);
問題是當我點擊微調,它會拋出一個NullPointerException
。下面是錯誤代碼:
java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:337)
at android.widget.AdapterView.checkFocus(AdapterView.java:732)
at android.widget.AdapterView$AdapterDataSetObserver.onInvalidated(AdapterView.java:855)
at android.widget.AbsListView$AdapterDataSetObserver.onInvalidated(AbsListView.java:6187)
at android.database.DataSetObservable.notifyInvalidated(DataSetObservable.java:50)
at android.widget.BaseAdapter.notifyDataSetInvalidated(BaseAdapter.java:59)
at android.widget.ArrayAdapter$ArrayFilter.publishResults(ArrayAdapter.java:546)
at android.widget.Filter$ResultsHandler.handleMessage(Filter.java:282)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
softenerList
不爲空,我可以從else
部分打印出它的大小。我不知道有什麼問題,有什麼想法?
什麼是'演示者'在這裏? – AndiGeeky
如果不是null,你不會得到那個錯誤 –
@AndiGeeky我的演示者類,返回一個軟化劑列表。 – Newbie