Attempt to invoke virtual method

前端 未结 3 1644
北海茫月
北海茫月 2020-12-10 13:11

I am trying to get some LinearLayoutsin my onCreateView but my App is crashing with the following message:

23430-23430/? W/System.e         


        
相关标签:
3条回答
  • 2020-12-10 13:56

    You're calling getView() too early - it will only return the view you return yourself from onCreateView().

    Inside onCreateView() use the recently inflated rootView instead of getView().

    0 讨论(0)
  • 2020-12-10 14:01

    The same NullPointerException caused while I'm trying to make a component extends view, it may due to the using of LayoutInflater

    View view = LayoutInflater.from(context).inflate(R.layout.layout_index_bar, null);
    container = (LinearLayout) view.findViewById(R.id.indexContainer);
    
    0 讨论(0)
  • 2020-12-10 14:06

    Change to

     card01 = (LinearLayout) rootView.findViewById(R.id.card01);
     card02 = (LinearLayout) rootView.findViewById(R.id.card02);
     card03 = (LinearLayout) rootView.findViewById(R.id.card03);
     card04 = (LinearLayout) rootView.findViewById(R.id.card04);
     card05 = (LinearLayout) rootView.findViewById(R.id.card05);
     card06 = (LinearLayout) rootView.findViewById(R.id.card06);
    

    You inflate a layout. the views belong to the inflated layout. So use the view object to initialize views in onCreateView

    Fragment is hosted by a Acitvity

    You can use getView in onActivityCreated and initialize views

    Initialize your TextView's also in onCreateView. Since Asynctask is a inner class you can update ui there

    Declare

     RobotoTextView date_widget2;
    

    as instance variable

    Then in onCreateView

     date_widget2= (RobotoTextView) rootView.findViewById(R.id.date02);
    
    0 讨论(0)
提交回复
热议问题