【问题标题】:not an enclosing class不是封闭类
【发布时间】:2016-05-14 15:34:09
【问题描述】:

我的菜单选项按钮与获取 HTTP 数据的类不同。它给了我“PhotoGalleryFragment 不是封闭类”的错误

new PhotoGalleryFragment.FetchItemsTask("top-rated").execute();

PhotoGalleryActivity.java - 在这里,我试图做到这一点,当按下“评分最高的电影”按钮时,它会传递 FetchItemsTask 的“评分最高”参数以运行并更改 API url 并更改返回 JSON 从“流行”到“顶级”

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.topRatedMovies) {
            Toast.makeText(getApplicationContext(), "Top Rated Movie selected", Toast.LENGTH_LONG).show();

            new PhotoGalleryFragment.FetchItemsTask("top-rated").execute();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

PhotoGalleryFragment.java - 在这里,我正在尝试获取数据。

public  class FetchItemsTask extends AsyncTask<Void,Void,List<MovieItem>> {
    private String mQuery;
    public FetchItemsTask(String query) {
        mQuery = query;
    }

    @Override
    protected List<MovieItem> doInBackground(Void... params) {
        return new MovieFetchr().fetchItems(mQuery);
    }

    @Override
    protected void onPostExecute(List<MovieItem> items) {
        mItems = items;
        for (int i = 0; i < mItems.size(); i++) {
        }
        setupAdapter();
    }

}

我该如何解决这样的问题?谢谢。

【问题讨论】:

    标签: java android android-asynctask


    【解决方案1】:

    从您包含的来源中不清楚,但 FetchItemsTask 似乎是 PhotoGalleryFragment 的内部类。

    要修复它,您应该使 FetchItemsTask 静态内部类,即更改:

    public class PhotoGalleryFragment extends Fragment {
        public  class FetchItemsTask extends AsyncTask<Void,Void,List<MovieItem>> {
            ...
        }
    }
    

    public class PhotoGalleryFragment extends Fragment {
        public static class FetchItemsTask extends AsyncTask<Void,Void,List<MovieItem>> {
            ...
        }
    }
    

    更多关于内部类的细节你可以在这里找到 https://stackoverflow.com/questions/20252727/is-not-an-enclosing-class-java

    【讨论】:

      【解决方案2】:

      要创建内部类,您需要从外部类的实例中执行此操作,或者将内部类设为static

      所以,在PhotoGalleryFragment 中创建实例:

      public class PhotoGalleryFragment {       
          void createTask(String query) {
              new FetchItemsTask(query); //return it if you like, or just call execute here, doesn't matter
          }
      }
      

      或者:

      public static class FetchItemsTask
      

      但我认为您需要执行第一个选项,因为setupAdapter 可能是PhotoGalleryFragment 上的一种方法。

      通过在PhotoGalleryFragment 中创建,内部类可以引用PhotoGalleryFragment,这就是它能够在其上调用方法的方式。

      把它想象成一个静默的构造函数参数和字段,它的行为很像这段代码,只是你不费吹灰之力:

      public class FetchItemsTask extends AsyncTask<Void,Void,List<MovieItem>> {
      
          private final PhotoGalleryFragment outer;
      
          public FetchItemsTask(PhotoGalleryFragment outer, //a reference to the outer is passed in automatically
              String query) {
              this.outer = outer; //and stored in this FetchItemsTask instance
          }
      
          @Override
          protected void onPostExecute(List<MovieItem> items) {
              outer.setupAdapter(); //then used when outer methods are invoked
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-05-06
        • 2011-07-05
        • 2013-12-13
        • 2016-04-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-31
        相关资源
        最近更新 更多