android - fragment 中的适配器 - android.app.Application 无法转换为 android.app.Activity

标签 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);

在 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/

相关文章:

android - 在 ionic LaunchNavigator.navigate 中使用坐标而不是城市文本

android - 图像处理安卓应用

java.util.ConcurrentModificationException 从数组列表中删除元素时,即使使用迭代器

java - 如何使用 Here Android SDK 将 SVG 图标设置为 HERE map 上的 map 标记

android - FragmentPagerAdapter 覆盖 getItem() 方法冲突

android - ListView 适配器不想设置文本

android - 如何删除带有 actionbar、viewpager 和多个 fragment 的选项卡?

android - fragment 中的 Facebook 登录

android - 在 ViewHolder 中引用 Activity

android - Android RecyclerView中实现多选