简体   繁体   English

为什么我从Android 7 Nougat收到NullPointerException?

[英]Why am I getting a NullPointerException from Android 7 Nougat?

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 R.layout.hometab引用的XML

<?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. 我检查过以确保R.layout.hometab和tabHost在运行LayoutInflater行时不为null,并且看起来还不错。 They're defenitely not null. 它们绝对不是null。 I've also checked to make sure LayoutInflater. 我也检查过以确保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)

I had same problem. 我有同样的问题。 But when I removed android:tabStripEnabled="true" from my layout, the problem's gone. 但是,当我从布局中删除android:tabStripEnabled="true" ,问题就消失了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我会收到 nullpointerexception (Android)? - Why am I getting a nullpointerexception (Android)? 为什么会出现NullPointerException? - Why am I getting NullPointerException? Android - 为什么我在SQLiteOpenHelper onCreate中得到NullPointerException? - Android - Why am I getting NullPointerException in SQLiteOpenHelper onCreate? 为什么我在Android 4.0.3中得到TimePicker NullPointerException? - Why am I getting TimePicker NullPointerException in android 4.0.3? 为什么我会得到一个nullpointerexception,可能是类错误(Android)? - Why am I getting a nullpointerexception, probably a class error (Android)? 为什么在添加linearlayout(Android)时会出现nullpointerexception? - Why am I getting a nullpointerexception when adding a linearlayout (Android)? 为什么我在此行上收到NullPointerException? - Why am I getting a NullPointerException on this line? 为什么在Firebase中的onChildAdded中得到NullPointerException? - Why am I getting NullPointerException in onChildAdded in Firebase? 为什么在此应用程序中出现NullPointerException? - Why am I getting a NullPointerException in this app? 为什么在.setOnTouchListener()上得到nullpointerexception? - Why am I getting a nullpointerexception on .setOnTouchListener()?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM