I have read "What is a NullPointerException and how do I fix it?" and it does not answer my question.
Okay, so I've got a fragment as an inner class inside my activity. From the fragment, I am trying to invoke a method in the activity class to remove all tabs from a TabLayout, but for some reason, I am getting a NullPointerException when I try to assign the TabLayout
code to its resource.
Here is the method to remove all the tabs:
public void removeAllTabs() {
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mTabLayout = findViewById(R.id.tabs);
mViewPager = findViewById(R.id.container);
int cnp = mSectionsPagerAdapter.getCount();
while (cnp > 0) {
mSectionsPagerAdapter.destroyItem(mViewPager, cnp - 1, mSectionsPagerAdapter.getItem(cnp - 1));
mTabLayout.removeTabAt(cnp - 1);
cnp--;
}
}
Here is the code that calls it:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.fragment_menu, container, false);
final Activity activity = getActivity();
final MenuActivity menuAct = new MenuActivity();
final MenuViewModel viewModel = ViewModelProviders.of(this, new MenuViewModelFactory(activity.getApplication(), MenuActivity.ResID)).get(MenuViewModel.class);
viewModel.getAllMenuTitles().observe(this, new Observer<String[]>() {
@Override
public void onChanged(String[] strings) {
final Spinner spinner = rootView.findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(), R.layout.support_simple_spinner_dropdown_item, strings);
adapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
menuAct.removeAllTabs();
SubMenuViewModel vm = ViewModelProviders.of(getParentFragment(), new SubMenuViewModelFactory(menuAct.getApplication(), i)).get(SubMenuViewModel.class);
vm.getSubMenus().observe(getActivity(), new Observer<String[]>() {
@Override
public void onChanged(@Nullable String[] strings) {
menuAct.populateTabs(strings);
}
});
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
});
return rootView;
}
Here is the stack trace:
07-27 15:11:59.434 17744-17744/com.whatamidoingwithmylife.splitbill E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.whatamidoingwithmylife.splitbill, PID: 17744
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.whatamidoingwithmylife.splitbill/com.whatamidoingwithmylife.splitbill.MenuActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2679)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:440)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:117)
at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java:149)
at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:29)
at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:54)
at android.support.v7.app.AppCompatDelegateImplV23.<init>(AppCompatDelegateImplV23.java:31)
at android.support.v7.app.AppCompatDelegateImplN.<init>(AppCompatDelegateImplN.java:31)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:198)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:183)
at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:519)
at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:190)
at com.whatamidoingwithmylife.splitbill.MenuActivity.<init>(MenuActivity.java:48)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1174)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2669)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:440)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)