java RecyclerView:内部类不能有静态声明
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31956340/
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
RecyclerView: Inner classes cannot have static declaration
提问by Martin
I am a little confused. I've set up a RecyclerView
as per the tutorial on google/android site and I get the following error:
我有点困惑。我已经RecyclerView
按照 google/android 站点上的教程设置了一个,但出现以下错误:
Inner classes cannot have static declaration
Of course, I do have a nested static class but this is how android/google defined it.
当然,我确实有一个嵌套的静态类,但这就是 android/google 定义它的方式。
public class ItemsViewAdapter extends RecyclerView.Adapter<ItemsViewAdapter.ViewHolder> {
// ...
// ...
public static class ViewHolder extends RecyclerView.ViewHolder {
// ...
}
Why am I getting this error? I hear its better to use static
for nested classes so you are not wasting a reference, but the current version of android studio is complaining
为什么我收到这个错误?我听说static
用于嵌套类更好,因此您不会浪费参考,但是当前版本的 android studio 正在抱怨
Any ideas?
有任何想法吗?
回答by MKJParekh
Straight to your questions:
直接回答你的问题:
Inner classes cannot have static declaration
That's completely true. This is not a bug, and the error message is not even misleading.
I hear its better to use nested class as a static so you are not wasting a reference
You are absolutely correct.
Solution for you:
Create a new class(File) in your project for
ItemsViewAdapter
and there won't be such an error.
内部类不能有静态声明
这是完全正确的。这不是错误,错误消息甚至没有误导性。
我听说最好将嵌套类用作静态类,这样您就不会浪费参考
你是完全正确的。
为您解决:
在你的项目中创建一个新的类(文件),
ItemsViewAdapter
就不会出现这样的错误。
General Discussion
一般讨论;一般交流
Java and Android both supports that you can declare static
inner classes/members/functions, BUTthat class should be a parent class. You cannot do that inside an inner class.
Java 和 Android 都支持您可以声明static
内部类/成员/函数,但该类应该是父类。你不能在内部类中这样做。
I.e., class Main
can have static class Adapter
but if the Adapter
class is an inner class of Main
is not static then it can't have its own static inner class/member.
即,class Main
可以有,static class Adapter
但如果Adapter
该类是非Main
静态的内部类,则它不能拥有自己的静态内部类/成员。
What You Can Have?
你能拥有什么?
class Main
static class Adapter
static class Holder
Or
或者
class Adapter
static class Holder
If you want to declare any member of class as static
then the immediate parent class must be the top-level, main class in that file.
如果要将类的任何成员声明为,static
则直接父类必须是该文件中的顶级主类。
Why?
为什么?
引用另一个答案,这是因为内部类与其外部类的实例隐式关联,它本身不能定义任何静态方法。由于静态嵌套类不能直接引用其封闭类中定义的实例变量或方法,因此只能通过对象引用使用它们,因此在静态嵌套类中声明静态方法是安全的。
Further Reading on the Topic
关于该主题的进一步阅读
1 http://www.geeksforgeeks.org/inner-class-java/
1 http://www.geeksforgeeks.org/inner-class-java/
2 http://www.javaworld.com/article/2077372/learn-java/static-class-declarations.html
2 http://www.javaworld.com/article/2077372/learn-java/static-class-declarations.html
回答by Eugene Kwame Anane
You could also simply implement ItemViewAdapter as a static class
您也可以简单地将 ItemViewAdapter 实现为静态类
public static class ItemsViewAdapter extends RecyclerView.Adapter<ItemsViewAdapter.ViewHolder> {
...
...
public static class ViewHolder extends RecyclerView.ViewHolder {
...
}
That should take care of the error
那应该处理错误