我正在使用com.weiwangcn.betterspinner
,但我没有在XML文件中声明它,而是以编程方式创建它。下面是我的代码,简短而简单。
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
部分打印出它的大小。我不知道问题是什么,任何想法?
答案 0 :(得分:0)
List<Mill_Softener_Type> softenerList = new ArrayList<>();
softenerList = presenter.getSoftener();
答案 1 :(得分:0)
这里presenter.getSoftener();返回null值,因此您必须首先检查它是否提供空值引用。
List<Mill_Softener_Type> softenerList = presenter.getSoftener();
if (softenerList != null){
if(softenerList.size() == 0) {
// your code
} else {
// your code
}
container.addView(spinnerSoftener);
}
答案 2 :(得分:0)
通常,我不明白为什么你不能在这里使用XML,但是列表不是空的,因为你说你可以打印大小而堆栈跟踪说不然。
无论如何,我会在没有new ArrayList<>
的情况下重写它,因为如果你有一个空列表,那么你将有一个空的适配器。不需要以任何不同的方式对待它。
final Context context = getActivity();
final MaterialBetterSpinner spinnerSoftener = new MaterialBetterSpinner(context);
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 != null)
{
int size = softenerList.size();
System.out.println("softener size: " + size);
spinnerSoftener.setAdapter(new ArrayAdapter<>(context, android.R.layout.simple_spinner_dropdown_item, softenerList));
spinnerSoftener.setEnabled(size > 0);
}
container.addView(spinnerSoftener);
如果你仍然得到例外,那么它们很可能在代码的其他地方引起,无论是在你的代码中还是在你正在使用的库中。