在选项卡式活动中使用片段中的视图

时间:2017-01-22 13:59:12

标签: android android-fragments

我有一个带有2个片段的标签式活动。我想在这些片段中使用Views。

OnCreate(选项卡式主机活动)

switchButton1 = (Switch) findViewById(R.id.switch1);
switchButton1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean bChecked) {
            if (bChecked) {}

当我运行应用程序时,我收到一个错误,它找不到视图。 这是错误。

 java.lang.RuntimeException: Unable to start activity ComponentInfo{}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Switch.setOnCheckedChangeListener(android.widget.CompoundButton$OnCheckedChangeListener)' on a null object referencehere

0 个答案:

没有答案