getActionBar().setDisplayHomeAsUpEnabled(true) throws NullPointerException

前端 未结 4 1295
一整个雨季
一整个雨季 2021-01-03 00:36

This question is asked several times in stackoverflow and I have tried all of them. But unfortunately neither is working for me.

I am trying to implement the naviga

4条回答
  •  天命终不由人
    2021-01-03 00:56

    Summay: To make sure you won't get a NullPointerException. You need to:

    1. Import the right library target to you minium SDK version.
    2. Use the right themes, just as the question owner said.

    But in my situation an if statement is necessary to solve my App from crash. By the way, I'm using an AppCompatActivity to hold my view fragment.

    public onCreateView(LayoutInflater inflater, final ViewGroup container,Bundle savedInstanceState){
        View view = inflater.inflate(R.layout.list_fragment, container, false);
        ActionBar actionBar = getActivity().getActionBar();
        if (actionBar != null){
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
    

提交回复
热议问题