簡體   English   中英

需要在DialogFragment內將適配器設置為ListView,但是使用getView()方法會得到空指針異常嗎? 為什么?

[英]Need to set an adapter to a ListView inside a DialogFragment but I get a null pointer exception with the getView() method? Why?

我有一個擴展DialogFragment的對話框:

public class MyCustomDialogFragment extends DialogFragment {

static MyCustomDialogFragment newInstance() {
    return new MyCustomDialogFragment();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.institutional_info_custom_list, container, false); 
    return view;
}
}

這是我使用此對話框的地方(它在onclick偵聽器內部):

MyCustomDialogFragment newFragment = MyCustomDialogFragment.newInstance();
                View DialogView = newFragment.getView();
                DetailListView = (ListView) DialogView.findViewById(R.id.custom_dialog_list);
                final MasterDetailArrayAdapter adapter = new MasterDetailArrayAdapter(ComeHaInvestito.this, MasterAndDetailstatisticsInfoList);         
                DetailListView.setAdapter(adapter);
                newFragment.show(getFragmentManager(), "master_detail_dialog"); 

MasterAndDetailstatitiscscsInfoList是我與自定義ArrayAdapter一起使用的列表,但是問題出在DialogFragment的視圖上:它為null! 為什么? 我閱讀了android文檔,他們說getView()返回對話框的View,這是onCreateView()重寫方法返回的對話框。

那么,為什么在嘗試撤消dialogFragments布局中的ListView時卻得到空指針異常?

對不起,但這對我來說沒有意義。 一個解釋將不勝感激。 謝謝!

使用setArguments()提供的數據集AdapterDialogFragmet DialogFragment覆蓋onViewCreated() ,檢索ListView ,創建適配器並將其設置為ListView

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM