不是封闭的阶级

时间:2016-02-04 20:23:45

标签: java android android-asynctask

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

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

PhotoGalleryActivity.java - 在这里,我试图这样做,当按下“最受好评的电影”按钮时,它会传递FetchItemsTask的“评级最高”的参数来运行并更改API网址并更改将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();
    }

}

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

2 个答案:

答案 0 :(得分:3)

要创建内部类,您需要从外部类的实例执行此操作,或者创建内部类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
    }

}

答案 1 :(得分: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://stackoverflow.com/questions/20252727/is-not-an-enclosing-class-java

相关问题