Why am I getting a NullPointerException from Android 7 Nougat?

553 views Asked by At

The Fragment

public class PropertyHomeTabListScreen extends Fragment {


    private String TAG = "PropertyHomeTabListScreen";
    TabsPagerAdapter tabsAdapter;
    ViewPager viewPager;
    TabHost tabHost;
    private int totalCount = 0;
    private int currentTabid = 0;
    private String name = null;
    private String[] tabName = {"enquiries","properties","seller profile","more"};
    private DataHandler dataHandler;
    private String homeScreenStatus = null;
    SharedPreferences sharedPreferences;
    private int customerId = 0;
    AllCustomersObject allCustomersObject;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        final View rootView = inflater.inflate(R.layout.homescreen_tablist,null);
        sharedPreferences = getActivity().getSharedPreferences("CUSTOMER", MODE_PRIVATE);
        customerId = sharedPreferences.getInt("user_id", 0);
        GetProfile getProfile = new GetProfile(getActivity(), customerId);
        getProfile.execute();
        SharedPreferences userprofile =getActivity().getSharedPreferences("USER_PROFILE",getActivity().MODE_PRIVATE);
        allCustomersObject = new AllCustomersObject();
        allCustomersObject.setname(userprofile.getString("username",null));
        allCustomersObject.setImageUrl(userprofile.getString("image_path",null));
        allCustomersObject.setPhonenumber(userprofile.getString("user_phone",null));



        dataHandler = DataHandler.getDataHandler();
        tabHost = (TabHost) rootView.findViewById(android.R.id.tabhost);
        tabHost.setup();
        viewPager=(ViewPager) rootView.findViewById(R.id.viewpager);

        initialiseTabHost();
        tabsAdapter=new TabsPagerAdapter(getActivity(),getFragmentManager(),totalCount);
        viewPager.setAdapter(tabsAdapter);

        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                View tabView = tabHost.getTabWidget().getChildAt(position);
                View mHorizontalScroll = (HorizontalScrollView) rootView.findViewById(R.id.horizontallistview);
                try {
                    if (tabView != null) {
                        int width = mHorizontalScroll.getWidth();
                        int scrollPos = tabView.getLeft() - (width - tabView.getWidth()) / 2;
                        mHorizontalScroll.scrollTo(scrollPos,0);
                    } else {
                        mHorizontalScroll.scrollBy(positionOffsetPixels, 0);
                    }
                    try {
                        homeScreenStatus = dataHandler.getHomeScreenStatus();
                        if (homeScreenStatus != null && !homeScreenStatus.isEmpty()) {
                            if (homeScreenStatus.equalsIgnoreCase("homescreen")) {
                                currentTabid = dataHandler.getCurrentHomeScreenTab();
                                tabHost.getTabWidget().setCurrentTab(currentTabid);
                                viewPager.setCurrentItem(currentTabid);
                                dataHandler.setHomeScreenStatus("nothomescreen");
                            } else {
                                int pos = viewPager.getCurrentItem();
                                dataHandler.setHomeScreenStatus("nothomescreen");
                                dataHandler.setCurrentHomeScreenTab(pos);
                                viewPager.setCurrentItem(pos);
                                tabHost.setCurrentTab(pos);
                            }
                        } else {
                            int pos = viewPager.getCurrentItem();
                            dataHandler.setHomeScreenStatus("nothomescreen");
                            dataHandler.setCurrentHomeScreenTab(pos);
                            viewPager.setCurrentItem(pos);
                            tabHost.setCurrentTab(pos);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            @Override
            public void onPageSelected(int position) {
            }
            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });

        return rootView;

    }


    /**
     * Below function is used for Add the tab
     * @param activity
     * @param tabHost
     * @param tabSpec
     */
    private static void AddTab(Activity activity, TabHost tabHost, TabHost.TabSpec tabSpec) {
        tabSpec.setContent(new MyTabFactory(activity));
        tabHost.addTab(tabSpec);
    }

    /**
     * Below function is used for initialize the tabhost and set the which tab is Active or not.
     */
    private void initialiseTabHost() {
        int tabCount = 4;
        for(int i= 0;i < tabCount;i++) {
            name = tabName[i];
            AddTab(getActivity(),this.tabHost,this.tabHost.newTabSpec(name).setIndicator(name));
        }

        try {
            homeScreenStatus = dataHandler.getHomeScreenStatus();

            if (homeScreenStatus != null && !homeScreenStatus.isEmpty()) {
                if (homeScreenStatus.equalsIgnoreCase("homescreen")) {
                    currentTabid = dataHandler.getCurrentHomeScreenTab();
                    tabHost.getTabWidget().setCurrentTab(currentTabid);
                    viewPager.setCurrentItem(currentTabid);
                } else {
                    dataHandler.setHomeScreenStatus("nothomescreen");
                    tabHost.getTabWidget().setCurrentTab(0);
                    viewPager.setCurrentItem(0);
                }
            } else {
                dataHandler.setHomeScreenStatus("nothomescreen");
                tabHost.getTabWidget().setCurrentTab(0);
                viewPager.setCurrentItem(0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
            @Override
            public void onTabChanged(String s) {
                int pos = tabHost.getCurrentTab();
                dataHandler.setHomeScreenStatus("nothomescreen");
                dataHandler.setCurrentHomeScreenTab(pos);
                viewPager.setCurrentItem(pos);
                tabHost.getTabWidget().setCurrentTab(pos);
           }
        });

        totalCount = tabHost.getTabWidget().getTabCount();

    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    @Override
    public void onStop() {
        super.onStop();
    }

}

The XML referenced by R.layout.hometab

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:weightSum="10"
        android:background="@color/screen_background">

        <TabHost
            android:id="@android:id/tabhost"
            android:layout_width="fill_parent"
            android:layout_height="0dip"
            android:layout_weight="10">

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >

                <HorizontalScrollView
                    android:id="@+id/horizontallistview"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:fillViewport="true"
                    android:background="@color/greycolor"
                    android:scrollbars="none" >

                    <TabWidget
                        android:id="@android:id/tabs"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:tabStripEnabled="true"
                        android:background="@color/overallcolor"
                        android:orientation="horizontal"/>

                </HorizontalScrollView>

                <FrameLayout
                    android:id="@android:id/tabcontent"
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    android:layout_weight="0" />

                <android.support.v4.view.ViewPager
                    android:id="@+id/viewpager"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="bottom" />

            </LinearLayout>

        </TabHost>

    </LinearLayout>

</android.support.v4.widget.DrawerLayout>

I've checked to make sure R.layout.hometab and tabHost are not null when it runs the LayoutInflater line and they seem to be fine. They're defenitely not null. I've also checked to make sure LayoutInflater.

The logcat indicating the error says.

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.drawable.Drawable.setState(int[])' on a null object reference
                                                                                       at android.widget.TabWidget.dispatchDraw(TabWidget.java:378)
                                                                                       at android.view.View.draw(View.java:17086)
                                                                                       at android.view.View.updateDisplayListIfDirty(View.java:16065)
                                                                                       at android.view.View.draw(View.java:16849)
                                                                                       at android.view.ViewGroup.drawChild(ViewGroup.java:3768)
                                                                                       at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3554)
                                                                                       at android.view.View.draw(View.java:17086)
                                                                                       at android.widget.HorizontalScrollView.draw(HorizontalScrollView.java:1643)
                                                                                       at android.view.View.updateDisplayListIfDirty(View.java:16065)
                                                                                       at android.view.View.draw(View.java:16849)
                                                                                       at android.view.ViewGroup.drawChild(ViewGroup.java:3768)
                                                                                       at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3554)
                                                                                       at android.view.View.updateDisplayListIfDirty(View.java:16060)
                                                                                       at android.view.View.draw(View.java:16849)
                                                                                       at android.view.ViewGroup.drawChild(ViewGroup.java:3768)
                                                                                       at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3554)
                                                                                       at android.view.View.updateDisplayListIfDirty(View.java:16060)
                                                                                       at android.view.View.draw(View.java:16849)
                                                                                       at android.view.ViewGroup.drawChild(ViewGroup.java:3768)
                                                                                       at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3554)
                                                                                       at android.view.View.draw(View.java:17086)
                                                                                       at android.view.View.updateDisplayListIfDirty(View.java:16065)
                                                                                       at android.view.View.draw(View.java:16849)
                                                                                       at android.view.ViewGroup.drawChild(ViewGroup.java:3768)
                                                                                       at android.support.v4.widget.DrawerLayout.drawChild(DrawerLayout.java:1379)
                                                                                       at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3554)
                                                                                       at android.view.View.updateDisplayListIfDirty(View.java:16060)
                                                                                       at android.view.View.draw(View.java:16849)
                                                                                       at android.view.ViewGroup.drawChild(ViewGroup.java:3768)
                                                                                       at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3554)
                                                                                       at android.view.View.updateDisplayListIfDirty(View.java:16060)
                                                                                       at android.view.View.draw(View.java:16849)
                                                                                       at android.view.ViewGroup.drawChild(ViewGroup.java:3768)
                                                                                       at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3554)
                                                                                       at android.view.View.updateDisplayListIfDirty(View.java:16060)
                                                                                       at android.view.View.draw(View.java:16849)
                                                                                       at android.view.ViewGroup.drawChild(ViewGroup.java:3768)
                                                                                       at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3554)
                                                                                       at android.view.View.updateDisplayListIfDirty(View.java:16060)
                                                                                       at android.view.View.draw(View.java:16849)
                                                                                       at android.view.ViewGroup.drawChild(ViewGroup.java:3768)
                                                                                       at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3554)
                                                                                       at android.view.View.updateDisplayListIfDirty(View.java:16060)
                                                                                       at android.view.View.draw(View.java:16849)
                                                                                       at android.view.ViewGroup.drawChild(ViewGroup.java:3768)
                                                                                       at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3554)
                                                                                       at android.view.View.updateDisplayListIfDirty(View.java:16060)
                                                                                       at android.view.View.draw(View.java:16849)
                                                                                       at android.view.ViewGroup.drawChild(ViewGroup.java:3768)
                                                                                       at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3554)
                                                                                       at android.view.View.updateDisplayListIfDirty(View.java:16060)
                                                                                       at android.view.View.draw(View.java:16849)
                                                                                       at android.view.ViewGroup.drawChild(ViewGroup.java:3768)
                                                                                       at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3554)
                                                                                       at android.view.View.updateDisplayListIfDirty(View.java:16060)
                                                                                       at android.view.View.draw(View.java:16849)
                                                                                       at android.view.ViewGroup.drawChild(ViewGroup.java:3768)
                                                                                       at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3554)
                                                                                       at android.view.View.updateDisplayListIfDirty(View.java:16060)
                                                                                       at android.view.View.draw(View.java:16849)
                                                                                       at android.view.ViewGroup.drawChild(ViewGroup.java:3768)
                                                                                       at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3554)
                                                                                       at android.view.View.draw(View.java:17086)
                                                                                       at com.android.internal.policy.DecorView.draw(DecorView.java:751)
                                                                                       at android.view.View.updateDisplayListIfDirty(View.java:16065)
1

There are 1 answers

0
stripped_viper On

I had same problem. But when I removed android:tabStripEnabled="true" from my layout, the problem's gone.