Error: Non-static method 'findViewById(int)' cannot be referenced from a static context

后端 未结 5 1793
天命终不由人
天命终不由人 2020-12-31 05:27

I am using Android Studio (Beta), and while using this java code in \'onCreateView()\', I get an error.

ListView listView = (ListView) findViewById(R.id.some         


        
5条回答
  •  感情败类
    2020-12-31 06:30

    Assuming you have a static fragment inner class inside an activity: you're trying to call the activity's findViewById() which you cannot in a static inner class that doesn't hold a reference to the parent.

    In onCreateView() you need to call it on the root view you just inflated, e.g.

     ListView listView = (ListView) rootView.findViewById(R.id.someListView);
    

提交回复
热议问题