不是一个封闭类

4
我的菜单选项按钮位于与提取HTTP数据的类不同的类中。它会给我一个“PhotoGalleryFragment不是封闭类”的错误。
new PhotoGalleryFragment.FetchItemsTask("top-rated").execute();

PhotoGalleryActivity.java - 在这里,我试图让"最受欢迎的电影"按钮被按下时,将"top-rated"参数传递给FetchItemsTask来运行,并更改API网址和将返回的JSON从"popular"更改为"top-rated"

    @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();
    }

}

我该如何修复这样的问题?谢谢。
2个回答

4
为了创建内部类,您需要从外部类的实例中进行创建,或者将内部类设置为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
    }

}

0

从您提供的来源中并不清楚,但似乎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://dev59.com/TWIj5IYBdhLWcg3wcEyI


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接