2017-03-22 89 views
0

我使用的是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部分打印出它的大小。我不知道有什麼問題,有什麼想法?

+0

什麼是'演示者'在這裏? – AndiGeeky

+0

如果不是null,你不會得到那個錯誤 –

+0

@AndiGeeky我的演示者類,返回一個軟化劑列表。 – Newbie

回答

0
List<Mill_Softener_Type> softenerList = new ArrayList<>(); 

softenerList = presenter.getSoftener(); 
+2

錯誤。如果'presenter.getSoftener()'返回null,那麼這個列表再次爲空 – Nizam

+0

雖然這段代碼可能(或不可能)回答這個問題,但提供了額外的[context](https://meta.stackexchange.com/q/114762)關於_how_和/或_why_它解決了這個問題會提高答案的長期價值。它也不會提到爲什麼這個答案比其他答案更合適。 –

0

here presenter.getSoftener();返回空值,所以你必須先檢查它是否給出空值引用。

List<Mill_Softener_Type> softenerList = presenter.getSoftener(); 

if (softenerList != null){ 
    if(softenerList.size() == 0) { 
    // your code 
    } else { 
    // your code 
    } 
    container.addView(spinnerSoftener); 
} 
+0

不,它不會返回null,因爲我可以在'else'部分打印出它的大小。 – Newbie

+0

如何打印出尺寸?你得到的這個尺寸是多少? –

+0

'System.out.println(「softener size:」+ softenerList.size());',尺寸爲1,因爲目前只有一種柔軟劑 – Newbie

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); 

如果你仍然得到例外,那麼他們很可能在其他地方的代碼導致,無論是在你或你正在使用的庫。