This question already has answers here:
What is a NullPointerException, and how do I fix it?

(12个答案)


9个月前关闭。





在我的xml代码段中,有一个工具栏,我尝试在其中添加一个按钮到文本的左侧,但是执行代码时发生错误


java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'void androidx.appcompat.widget.Toolbar.setNavigationIcon(android.graphics.drawable.Drawable)'




    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    drawerLayout = getActivity().findViewById(R.id.Drawer);
    settings = getActivity().getSharedPreferences("Settings", getActivity().MODE_PRIVATE);
    toolbar =  getActivity().findViewById(R.id.toolbar);
    toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.menu)); //ERROR HERE


    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            drawerLayout.openDrawer(Gravity.LEFT);
        }
    });
    viewPager = getActivity().findViewById(R.id.rager);
    pagerAdapter = new MainActivity.PagerAdapter(getActivity().getSupportFragmentManager());
    viewPager.setAdapter(pagerAdapter);
    tabLayout = getActivity().findViewById(R.id.tabLayout4);
    tabLayout.setupWithViewPager(viewPager);
    tabLayout.setScrollX(tabLayout.getWidth());
    tabLayout.getTabAt(5).select();
    Date start = new Date();
    switch (start.toString().substring(0,3)) {
        case "Mon":
            viewPager.setCurrentItem(0);
            url = "Monday.txt";
            break;
        case "Tue":
            viewPager.setCurrentItem(1);
            url = "Tuesday.txt";
            break;
        case "Wed":
            viewPager.setCurrentItem(2);
            url = "Wednesday.txt";
            break;
        case "Thu":
            viewPager.setCurrentItem(3);
            url = "Thursday.txt";
            break;
        case "Fri":
            viewPager.setCurrentItem(4);
            url = "Friday.txt";
            break;
        case "Sat":
            viewPager.setCurrentItem(5);
            url = "Saturday.txt";
            break;
        default:
            viewPager.setCurrentItem(0);
            url = "Monday.txt";
            break;
    }


    SharedPreferences.Editor editor = settings.edit();
    editor.putString("Day", url);
    editor.apply();

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


发生错误

        toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.menu));


线

在主要活动中,此代码可以正常运行

最佳答案

您不能在getActivity()上执行findViewById。膨胀片段布局,并在膨胀视图上执行findViewById。

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_znonki, container, false);

        drawerLayout = view.findViewById(R.id.Drawer);
        settings = getActivity().getSharedPreferences("Settings", getActivity().MODE_PRIVATE);
        toolbar =  view.findViewById(R.id.toolbar);
        toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.menu)); //ERROR HERE


        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                drawerLayout.openDrawer(Gravity.LEFT);
            }
        });
        viewPager = view.findViewById(R.id.rager);
        pagerAdapter = new MainActivity.PagerAdapter(getActivity().getSupportFragmentManager());
        viewPager.setAdapter(pagerAdapter);
        tabLayout = view.findViewById(R.id.tabLayout4);
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.setScrollX(tabLayout.getWidth());
        tabLayout.getTabAt(5).select();
        Date start = new Date();
        switch (start.toString().substring(0,3)) {
            case "Mon":
                viewPager.setCurrentItem(0);
                url = "Monday.txt";
                break;
            case "Tue":
                viewPager.setCurrentItem(1);
                url = "Tuesday.txt";
                break;
            case "Wed":
                viewPager.setCurrentItem(2);
                url = "Wednesday.txt";
                break;
            case "Thu":
                viewPager.setCurrentItem(3);
                url = "Thursday.txt";
                break;
            case "Fri":
                viewPager.setCurrentItem(4);
                url = "Friday.txt";
                break;
            case "Sat":
                viewPager.setCurrentItem(5);
                url = "Saturday.txt";
                break;
            default:
                viewPager.setCurrentItem(0);
                url = "Monday.txt";
                break;
        }


        SharedPreferences.Editor editor = settings.edit();
        editor.putString("Day", url);
        editor.apply();

        addListenerOnButton();
        return view;
    }

10-08 17:03