因为你的context生命周期,当你是内部类时,context还存在,所以你可以用inflate()方法,但是如果在另外一个包得话,你的context已得不到

解决方案 »

  1.   

    你的inflater没有初始化。
    加上: LayoutInflater inflater = getLayoutInflater();
      

  2.   

    我也遇到这个问题了,我是在自定义组件时遇到的,我写了一个类继承了linearlayout这个类,然后再类里LayoutInflater.from(context).inflate(R.layout.customcomponent, null)时,提示我customcomponent cannot be resolved or is not a field,同求解
      

  3.   

    我也碰到过这个问题,最后发现是因为在自定义的adapter类中并没有把R文件import进去。因为R文件所在的package是和mainActivity包相同名字,但是和自定义adapter包不同
      

  4.   

    不知道你inflater是怎么实例化的,在构造函数里面实例化inflater,LayoutInflater mInflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      

  5.   

    刚遇到这个问题,因为类中导入的是android.R而不是gen包中的那个R文件,所以导致找不到layout如六楼所说,解决了这个问题!