Android 为什么getSystemService在使用片段时不能在适配器中工作

Android 为什么getSystemService在使用片段时不能在适配器中工作,android,layout,fragment,Android,Layout,Fragment,现在我正在尝试移植源代码,但我和github代码之间的区别在于他是由Activty制作的,但我制作了片段,所以在我修改了片段移植后,我需要在移植时修改一些代码 getSystemService方法不起作用 这件事是怎么回事 这是我的片段,它是由活动生成的,但我将其更改为片段 我不明白这不是错误,但在片段中更改活动源后,它发生了错误 如何修复此问题?更改这行选项卡Fragment1 myCategoriesExpandableListAdapter = new MyCategoriesExpan

现在我正在尝试移植源代码,但我和github代码之间的区别在于他是由Activty制作的,但我制作了片段,所以在我修改了片段移植后,我需要在移植时修改一些代码 getSystemService方法不起作用 这件事是怎么回事

这是我的片段,它是由活动生成的,但我将其更改为片段

我不明白这不是错误,但在片段中更改活动源后,它发生了错误
如何修复此问题?

更改这行选项卡Fragment1

 myCategoriesExpandableListAdapter = new MyCategoriesExpandableListAdapter(TabFragment1.this, parentItems, childItems, false);
对此

 myCategoriesExpandableListAdapter = new MyCategoriesExpandableListAdapter(getActivity(), parentItems, childItems, false);
和MyCategoriesExpandableListAdapter构造函数

 public MyCategoriesExpandableListAdapter(Activity activity, ArrayList<HashMap<String, String>> parentItems,
                                         ArrayList<ArrayList<HashMap<String, String>>> childItems, boolean isFromMyCategoriesFragment) {

    this.parentItems = parentItems;
    this.childItems = childItems;
    this.activity = activity;
    this.isFromMyCategoriesFragment = isFromMyCategoriesFragment;
    inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

还可以将“活动类型”更改为“活动”

一个简单的修复程序。将活动转换为片段时,会发生代码更改

而不是:

inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
只是一个小小的改变:

inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
这是一段完整的代码

 myCategoriesExpandableListAdapter = new MyCategoriesExpandableListAdapter(getActivity(),TabFragment1.this, parentItems, childItems, false);
请在适配器中尝试此代码

public MyCategoriesExpandableListAdapter(Context context,TabFragment1 activity, ArrayList<HashMap<String, String>> parentItems,
                                     ArrayList<ArrayList<HashMap<String, String>>> childItems, boolean isFromMyCategoriesFragment) {

this.parentItems = parentItems;
this.childItems = childItems;
this.mContext= context;
this.activity = activity;
this.isFromMyCategoriesFragment = isFromMyCategoriesFragment;
inflater = LayoutInflater.from(context);
}

使用以下语法:inflater=LayoutInflater.fromactivity;而不是inflater=LayoutFlater activity.getSystemServiceContext.LAYOUT\u inflater\u SERVICE;。它不起作用了。。LayoutInflater中android.content.Context中的getSystem无法应用于com.example.together.Fragment.TabFragment1
inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 myCategoriesExpandableListAdapter = new MyCategoriesExpandableListAdapter(getActivity(),TabFragment1.this, parentItems, childItems, false);
public MyCategoriesExpandableListAdapter(Context context,TabFragment1 activity, ArrayList<HashMap<String, String>> parentItems,
                                     ArrayList<ArrayList<HashMap<String, String>>> childItems, boolean isFromMyCategoriesFragment) {

this.parentItems = parentItems;
this.childItems = childItems;
this.mContext= context;
this.activity = activity;
this.isFromMyCategoriesFragment = isFromMyCategoriesFragment;
inflater = LayoutInflater.from(context);
}