所以现在我们有一个
CustomAdapter adapter = new CustomAdapter(getActivity().getApplicationContext(), R.layout.list_style, RowBean_data);
ListView lista = (ListView) rootView.findViewById(R.id.lista);
lista.setAdapter(adapter);
在 fragment 中,我们希望它显示一个包含项目和文本的列表。
现在我们收到以下代码的错误:http://pastebin.com/BV9X6Dys
其他类:
CustomAdapter.java http://pastebin.com/eA5Xx1Ng
MainActivity.java pastebin.com/Nv1Zip11
HomeFragment.java pastebin.com/8DiXQHsy
我们如何让它发挥作用?
最佳答案
您正在将 Context
转换为 Activity
。
错误在这里..
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
所以解决方案是使用context
来获取充气服务,就像这样
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
另一种方法是像这样
LayoutInflater inflater = LayoutInflater.from(context);
就像@Zharf 说的
编辑
此外,我认为将 getActivity()
传递给 CustomAdapter 的构造函数就足够了。
CustomAdapter adapter = new CustomAdapter(getActivity(), R.layout.list_style, RowBean_data);
关于android - fragment 中的适配器 - android.app.Application 无法转换为 android.app.Activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32523767/