用相同的方法为 Activity 和 SupportMapFragment 填充列表视图
Fill listview with the same method for Activity and SupportMapFragment
我用来自网络服务器的信息填充 ListView
。
在 Activity
中,我的 ListAdapeter
运行良好,但是当我尝试在 Fragment
中使用它时,我无法初始化我的适配器。
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);
}
//........
}
工作完美,但是,当我尝试在我的 Fragment 中使用它时:
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)
您正在创建 MainActivity 的新对象,这就是您收到错误的原因
adapter = new ListAdapter(new MainActivity());
将 MainActivity 传递给 fragment,然后将其传递给 ListAdapter
MainActivity main;
public ListReportsFragment(MainActivity main){
this.main = main;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
list = (ListView) getView().findViewById(R.id.list);
adapter = new ListAdapter(main);
list.setAdapter(adapter); //This is the line 58
}
改变
adapter = new ListAdapter(new MainActivity());
至
adapter = new ListAdapter(getActivity());
变化自
adapter = new ListAdapter(new MainActivity());
至
adapter = new ListAdapter(getActivity());
另外,将这部分代码移动到onCreateView()中,如下:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
adapter = new ListAdapter(getActivity());
listView = (ListView) rootView.findViewById(R.id.listview_forecast);
listView.setAdapter(arrayAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//what to do on item click goes here!
});
return rootView;
}
我用来自网络服务器的信息填充 ListView
。
在 Activity
中,我的 ListAdapeter
运行良好,但是当我尝试在 Fragment
中使用它时,我无法初始化我的适配器。
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);
}
//........
}
工作完美,但是,当我尝试在我的 Fragment 中使用它时:
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)
您正在创建 MainActivity 的新对象,这就是您收到错误的原因
adapter = new ListAdapter(new MainActivity());
将 MainActivity 传递给 fragment,然后将其传递给 ListAdapter
MainActivity main;
public ListReportsFragment(MainActivity main){
this.main = main;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
list = (ListView) getView().findViewById(R.id.list);
adapter = new ListAdapter(main);
list.setAdapter(adapter); //This is the line 58
}
改变
adapter = new ListAdapter(new MainActivity());
至
adapter = new ListAdapter(getActivity());
变化自
adapter = new ListAdapter(new MainActivity());
至
adapter = new ListAdapter(getActivity());
另外,将这部分代码移动到onCreateView()中,如下:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
adapter = new ListAdapter(getActivity());
listView = (ListView) rootView.findViewById(R.id.listview_forecast);
listView.setAdapter(arrayAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//what to do on item click goes here!
});
return rootView;
}