片段和主要活动之间的nullPointerException,android / android-studio

时间:2019-02-18 16:31:21

标签: java android nullpointerexception

我想在片段和主要活动之间进行交流,所以我需要获得switch.isCheked但得到:

  

java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'android.view.View android.app.Activity.findViewById(int)'

MainActivity.java

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);     getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new NotesFragment()).commit();
        BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_navigation);
        final   NotesFragment notesFragment = new NotesFragment();
        final   RemindersFragment remindersFragment = new RemindersFragment();
        final   SettingsFragment settingsFragment = new SettingsFragment();

        final Switch aSwitch =    settingsFragment.getView().findViewById(R.id.switch_pop_up);

        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                Fragment selectedFragment = null;

                switch (menuItem.getItemId()) {
                    case R.id.action_notes:
                        if(aSwitch.isChecked())
                        Toast.makeText(MainActivity.this, "Your Notes", Toast.LENGTH_SHORT).show();
                        selectedFragment = notesFragment;
                        break;
                    case R.id.action_reminders:
                        if(aSwitch.isChecked())
                        Toast.makeText(MainActivity.this, "Your Reminders", Toast.LENGTH_SHORT).show();
                        selectedFragment = remindersFragment;
                        break;
                    case R.id.action_settings:
                        if(aSwitch.isChecked())
                        Toast.makeText(MainActivity.this, "Settings", Toast.LENGTH_SHORT).show();
                        selectedFragment = settingsFragment;

                        break;
                }
                assert selectedFragment != null;
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                        selectedFragment).commit();
                return true;
            }
        });
    }
}

SettingsFragment.java

public class SettingsFragment extends Fragment {

    Switch switch_pop_up;
    boolean stateSwitchPopUp;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.fragment_settings, null);

       switch_pop_up  = v.findViewById(R.id.switch_pop_up);


        return inflater.inflate(R.layout.fragment_settings,container,false);
    }
}

我认为是因为:

final Switch aSwitch = settingsFragment.getView().findViewById(R.id.switch_pop_up);

我该如何解决?

2 个答案:

答案 0 :(得分:0)

在您的片段上,使用onCreateView方法,更改以下行:

View v = inflater.inflate(R.layout.fragment_settings, null);

进入:

View v = inflater.inflate(R.layout.fragment_settings, container, false);

修改1: 发生这种情况是因为您在做v.findViewById(),但没有在v上返回onCreateView()

将片段中的return行更改为return v;

编辑2:

将片段上的吸气剂添加到switch_pop_up

public Switch getSwitchPopUp() {
    return switch_pop_up;
}

在您的活动中,替换以下行:

final Switch aSwitch = settingsFragment.getView().findViewById(R.id.switch_pop_up);

具有:

final Switch aSwitch = settingsFragment.getSwitchPopUp();

并且您必须在此行之前为您的SettingsFragment充气。

注意:无论您需要哪种结果,这都不是一个好办法,因此您可能希望研究其他方法。

答案 1 :(得分:0)

问题是:

  1. 您没有将SettingsFragment附加到活动上以使其在屏幕上可见(就像您使用NotesFragment一样)
  2. 您正在尝试通过MainActivity的{​​{1}}方法访问其子视图。

在创建活动并准备好承载它们之后,将对这些片段进行初始化。因此,onCreate返回null。还没有调用settingsFragment.getView()生命周期方法。

您需要重新考虑您的方法并尝试找到其他解决方案。我建议您阅读SO中的该主题,以获取有关如何解决问题的更多想法。

How to access Fragment's child views inside fragment's parent Activity?

Access Fragment View from Activity's onCreate

Android: how to notify Activity when Fragments views are ready?