我使用來自Web服務器的信息填充ListView
。 在Activity
中,我的ListAdapeter
工作正常,但是當我試圖在Fragment
中使用它時,我無法初始化我的適配器。用Activity和SupportMapFragment的相同方法填充列表視圖
ListAdapter.java
public class ListAdapter extends BaseAdapter {
MainActivity main;
public ListAdapter(MainActivity main){
this.main = main;
}
//Some methods.
}
MainActivity.java
public class ListReportsFragment extends SupportMapFragment {
@Override
public ListView list;
public ListAdapter adapter;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//.........
list = (ListView) findViewById(R.id.list);
adapter = new ListAdapter(this);
list.setAdapter(adapter);
}
//........
}
還有就是可以正常使用,但是,當我嘗試在我的片段使用它:
ListReportsFragment
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
list = (ListView) getView().findViewById(R.id.list);
adapter = new ListAdapter(new MainActivity());
list.setAdapter(adapter); //This is the line 58
}
我得到 java.lang.NullPointerExceptionlayout.ListReportsFragment.onActivityCreated(ListReportsFragment.java:58)