如何在android中的textview中以不同的布局插入内存信息

时间:2013-06-04 08:34:52

标签: android android-layout textview

我有一个带滑动视图的应用程序,我有更多tabs.xml而不是单个main.xml。出于这个原因,如果我想在textview中显示某些东西,例如在第二个标签中,我写了这个部分:

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;

我想在textview(id可以是memoryinfos)中显示它,例如总是在第二个标签中。我尝试将此代码放入if (tabLayout == R.layout.tab2),但我总是在getSystemService中收到错误。我该怎么办?

0 个答案:

没有答案