在onCreate中使用FragmentActivity时不能使用“setChecked”。

时间:2013-08-21 20:45:26

标签: android android-fragments

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

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

我目前已进行设置,以便在应用启动时显示弹出窗口,用户单击“确定”并触发检查文件并设置已检查的操作。

为了澄清,我所谓的所有的操作都按预期工作,除非它在onCreate()中。

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

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;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

片段内任何View对象的更新都应该使用片段的View对象来完成,例如你的案例rootView。

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

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

//对cb进行更新

该操作在Fragment Activity的onCreate()中不起作用,如在onCreate()方法中, 我们有对主活动视图的View引用,即viewPager,它充当不同片段视图的容器。