【问题标题】:error: cannot find symbol method findviewbyid [duplicate]错误:找不到符号方法 findviewbyid [重复]
【发布时间】:2016-05-14 08:23:18
【问题描述】:

我有一个带有 listView 的 2 个片段的 tabLayout 的应用程序,我想使用 Json 从 URLConnection 加载内容,但是在 Fragment.java 文件中我得到错误:无法解析方法'findViewByID(int) '。

   public class Fragment1 extends Fragment implements URLConnectionResponse {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        CanteenProvider canProv = new CanteenProvider();
        canProv.delegate = this;
        canProv.getDishes(getActivity().getApplicationContext());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.activity_canteen_tab_mensa, container, false);
    }

    @Override
    public void processFinish(List<?> output) {
        ListView canteenList = (ListView) findViewById(R.id.mensalistView);
        ArrayList<Dish> canteenItemContent = new ArrayList<Dish>();
        canteenItemContent = (ArrayList<Dish>)output;

        CanteenListAdapter canteenAdapter = new CanteenListAdapter(canteenItemContent, this);
        canteenList.setAdapter(canteenAdapter);
    }


} 

【问题讨论】:

    标签: android fragment findviewbyid


    【解决方案1】:

    stack overflow上关注这个答案

     private ListView canteenList=null;
      private View view = null;
     @Override 
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            view = inflater.inflate(R.layout.activity_canteen_tab_mensa, container, false);
            canteenList = (ListView)view.findViewById(R.id.mensalistView);
            return view;
        }
    
        @Override
        public void processFinish(List<?> output) {
    
            ArrayList<Dish> canteenItemContent = new ArrayList<Dish>();
            canteenItemContent = (ArrayList<Dish>)output;
    
            CanteenListAdapter canteenAdapter = new CanteenListAdapter(canteenItemContent, view.getContext());
            canteenList.setAdapter(canteenAdapter);
        }
    

    【讨论】:

      【解决方案2】:

      您应该使用View 类的findViewById。所以你可以为你的ListView 创建一个全局变量:

      ListView canteenList;
      

      然后在您的onViewCreated 中初始化您的canteenList

          @Override
          public void onViewCreated(View view, Bundle savedInstanceState) {
              super.onViewCreated(view, savedInstanceState);
              canteenList = view.findViewById(R.id.mensalistView);
          }
      

      【讨论】:

        【解决方案3】:

        你必须创建一个全局变量

        查看主要内容;

        onCreateView 像这样膨胀之后

        mainContent = inflater.inflate(R.layout.activity_canteen_tab_mensa, container, false);
        return mainContent;
        

        最后加上 ma​​inContent 的引用,您可以像这样findViewById

        ListView canteenList = (ListView) mainContent.findViewById(R.id.mensalistView);

        【讨论】:

          猜你喜欢
          • 2016-11-10
          • 1970-01-01
          • 2017-04-27
          • 2018-01-23
          • 2019-05-31
          • 2020-02-08
          • 1970-01-01
          • 2011-06-24
          • 2012-12-27
          相关资源
          最近更新 更多