1

在主 Activity 中使用可滑动的片段。

我无法弄清楚如何进行查找文件的调用,然后根据文件是否存在来调用setChecked(boolean)Android Switch 上的方法。cb1

我目前进行了设置,以便在应用程序启动时显示一个弹出窗口并且用户单击确定,这会触发文件检查并设置检查操作。

澄清一下,我调用的所有操作都按预期工作,除非它位于onCreate().

如果它在 中onCreate(),则 logcat 将显示 NullPointerException 由DeviceSetup.setup第一行所在的任何一行引起setChecked(boolean)

MainActivity#onCreate()

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ChangeTheme.userchoice(this);
    DeviceState.startup(this);
    setContentView(R.layout.activity_main);

    // Create the adapter that will return a fragment for each of the three
    // primary sections of the app.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);
    mViewPager.setOffscreenPageLimit(3);
    mViewPager.setCurrentItem(1);
}

DeviceState班级:

public class DeviceState extends MainActivity {
    public static void startup(final MainActivity activity) {
        AlertDialog alertDialog = new AlertDialog.Builder(activity).create();
        alertDialog.setTitle("Welcome");
        alertDialog.setMessage("Select Ok to continue");
        alertDialog.setButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                Button startup = (Button) activity.findViewById(R.id.startup);
                startup.performClick(); /* <<<THAT BUTTON TRIGGERS "SETUP" BELOW */
                Rooted.rooted(activity);
            }
        });

        alertDialog.show();
    }

    public static void setup(MainActivity activity) {        
        File file1 = new File("/system/app/somerandom.apk");
        if (file1.exists()) {
            cb1.setChecked(true);
        } else {
            cb1.setChecked(false);
        }
    }

Fragments班级:

public class Fragments extends FragmentActivity {
    public static class FragMods extends Fragment {
        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        @Override
        public void onSaveInstanceState(Bundle outState) {
            outState.putString("BUGFIX", "BUGFIX");
            super.onSaveInstanceState(outState);
        }

        @Override
        public View onCreateView(LayoutInflater inflater,
                ViewGroup container, Bundle savedInstanceState) {
            View rootView = inflater.inflate(
                    R.layout.fragment_mods, container, false);
            return rootView;
        }
    }

    public static class FragExtras extends Fragment {
        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        public FragExtras() {}

        @Override
        public void onSaveInstanceState(Bundle outState) {
            outState.putString("BUGFIX", "BUGFIX");
            super.onSaveInstanceState(outState);
        }

        @Override
        public View onCreateView(LayoutInflater inflater,
                ViewGroup container, Bundle savedInstanceState) {
            View extrasView = inflater.inflate(
                    R.layout.fragment_extras, container, false);
            return extrasView;
        }
    }

    public static class FragCred extends Fragment {
        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        public FragCred() {}

        @Override
        public void onSaveInstanceState(Bundle outState) {
            outState.putString("BUGFIX", "BUGFIX");
            super.onSaveInstanceState(outState);
        }

        @Override
        public View onCreateView(LayoutInflater inflater,
                ViewGroup container, Bundle savedInstanceState) {
            View creditView = inflater.inflate(
                    R.layout.fragment_credit, container, false);
            return creditView;
        }
    }
}
4

1 回答 1

1

对片段内的任何 View 对象的更新都应该使用片段的 View 对象来完成,例如在您的情况下为 rootView。

// 使用 Fragment View Object 即 rootView,找到要更新的视图并进行更新。

Checkbox cb = (Checkbox) rootView.findViewById(R.id.checkBoxId);

//更新cb

该动作在 Fragment Activity 的 onCreate() 中不起作用,就像在 onCreate() 方法中一样,我们拥有对主 Activity 视图的 View 引用,即 viewPager,它充当不同 Fragment 视图的容器。

于 2013-08-21T20:56:17.880 回答