片段中的适配器-android.app.Application不能强制转换为android.app.Activity

片段中的适配器-android.app.Application不能强制转换为android.app.Activity,android,android-fragments,android-adapter,Android,Android Fragments,Android Adapter,所以现在我们有一个 CustomAdapter adapter = new CustomAdapter(getActivity().getApplicationContext(), R.layout.list_style, RowBean_data); ListView lista = (ListView) rootView.findViewById(R.id.lista); lista.setAdapter(adapter); 在片段中,我们希望它显示一个包含项目和文本的列表 现在,我们得到

所以现在我们有一个

CustomAdapter adapter = new CustomAdapter(getActivity().getApplicationContext(), R.layout.list_style, RowBean_data);
ListView lista = (ListView) rootView.findViewById(R.id.lista);
lista.setAdapter(adapter);
在片段中,我们希望它显示一个包含项目和文本的列表

现在,我们得到一个错误,代码如下:

其他类别:

CustomAdapter.java

MainActivity.java

HomeFragment.java


我们怎样才能让它工作

您正在将
上下文
转换为
活动

错误就在这里

 LayoutInflater inflater = ((Activity)context).getLayoutInflater();
因此,解决方案是使用
上下文
获得充气机服务,如下所示

LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LayoutInflater inflater = LayoutInflater.from(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);

获取
LayoutInflater
的替代方法(我更喜欢):
LayoutInflater充气器=LayoutInflater.from(上下文)@krzk很乐意帮忙。:)谢谢我使用了LayoutInflater充气器=LayoutInflater.from(上下文);这对我很有用!!