我有一个带有滑动视图的应用程序,我有更多的 tabs.xml 而不是一个 main.xml。出于这个原因,如果我想在文本视图中显示某些内容,例如在第二个选项卡中,我写了这部分:
public static class TabFragment extends Fragment {
public static final String ARG_OBJECT = "object";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Bundle args = getArguments();
int position = args.getInt(ARG_OBJECT);
int tabLayout = 0;
switch (position) {
case 0:
tabLayout = R.layout.tab1;
break;
case 1:
tabLayout = R.layout.tab2;
break;
case 2:
tabLayout = R.layout.tab3;
break;
}
View rootView = inflater.inflate(tabLayout, container, false);
if (tabLayout == R.layout.tab2) {
TextView CPUinfo = (TextView) rootView.findViewById(R.id.CPUinfo);
CPUinfo.setText(getCPUinfo());
}
return rootView;
}
}
一切顺利。现在......我有这个内存信息的代码:
MemoryInfo mi = new MemoryInfo();
ActivityManager activityManager = (ActivityManager) appContext.getSystemService(Activity.ACTIVITY_SERVICE);
activityManager.getMemoryInfo(mi);
long availableMegs = mi.availMem / 1048576L;
我想在文本视图中显示它(id 可以是memoryinfos
),例如总是在第二个选项卡中。我试图把这段代码放进去if (tabLayout == R.layout.tab2)
,但我总是在getSystemService
. 我能怎么做?