java 不是封闭类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35210910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
not an enclosing class
提问by Ben Wong
My menu option button is in a different class than the class that fetches HTTP data. And it's giving me "PhotoGalleryFragment is not an enclosing class" error for
我的菜单选项按钮与获取 HTTP 数据的类位于不同的类中。它给了我“PhotoGalleryFragment 不是封闭类”错误
new PhotoGalleryFragment.FetchItemsTask("top-rated").execute();
PhotoGalleryActivity.java - In here, I am trying to make it so when the "Top Rated Movie" button is pressed, it passes the "top-rated" argument for the FetchItemsTask to run and change the API url and change the returned JSON from "popular" to "top-rated"
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 - In here, I'm trying to fetch the data.
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();
}
}
How would I fix something like this? Thanks.
我将如何解决这样的问题?谢谢。
采纳答案by weston
To create an inner class, you need to do that from an instance of the outer class or make the inner class static
:
要创建内部类,您需要从外部类的实例或创建内部类static
:
So, create the instance within PhotoGalleryFragment
:
因此,在 内创建实例PhotoGalleryFragment
:
public class PhotoGalleryFragment {
void createTask(String query) {
new FetchItemsTask(query); //return it if you like, or just call execute here, doesn't matter
}
}
Or:
或者:
public static class FetchItemsTask
But I think you will need to do the first option as setupAdapter
is probably a method on PhotoGalleryFragment
.
但我认为您需要执行第一个选项,因为setupAdapter
这可能是PhotoGalleryFragment
.
By creating within PhotoGalleryFragment
, that gives the inner class a reference to a PhotoGalleryFragment
which is how it is able to call methods on it.
通过创建 inside PhotoGalleryFragment
,这为内部类提供了对 a 的引用,PhotoGalleryFragment
这就是它如何能够调用其上的方法。
Think of it as a silent constructor parameter and field which acts much like this code, only without you lifting a finger:
把它想象成一个静默的构造函数参数和字段,它的作用很像这个代码,只是你不用动手指:
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
}
}
回答by lrother
It's not clear from sources you included, but it seems that FetchItemsTask is an inner class of PhotoGalleryFragment.
从您包含的来源中不清楚,但似乎 FetchItemsTask 是 PhotoGalleryFragment 的内部类。
To fix it you should make FetchItemsTask static inner class i.e. change:
要修复它,您应该将 FetchItemsTask 设为静态内部类,即更改:
public class PhotoGalleryFragment extends Fragment {
public class FetchItemsTask extends AsyncTask<Void,Void,List<MovieItem>> {
...
}
}
to
到
public class PhotoGalleryFragment extends Fragment {
public static class FetchItemsTask extends AsyncTask<Void,Void,List<MovieItem>> {
...
}
}
More details about inner classes you may find here https://stackoverflow.com/questions/20252727/is-not-an-enclosing-class-java
您可以在此处找到有关内部类的更多详细信息https://stackoverflow.com/questions/20252727/is-not-an-enclosure-class-java