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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 19:24:21  来源:igfitidea点击:

RecyclerView: Inner classes cannot have static declaration

javaandroidandroid-recyclerview

提问by Martin

I am a little confused. I've set up a RecyclerViewas 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 staticfor 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:

直接回答你的问题:

  1. Inner classes cannot have static declaration

    That's completely true. This is not a bug, and the error message is not even misleading.

  2. I hear its better to use nested class as a static so you are not wasting a reference

    You are absolutely correct.

  3. Solution for you:

    Create a new class(File) in your project for ItemsViewAdapterand there won't be such an error.

  1. 内部类不能有静态声明

    这是完全正确的。这不是错误,错误消息甚至没有误导性。

  2. 我听说最好将嵌套类用作静态类,这样您就不会浪费参考

    你是完全正确的。

  3. 为您解决:

    在你的项目中创建一个新的类(文件),ItemsViewAdapter就不会出现这样的错误。



General Discussion

一般讨论;一般交流

Java and Android both supports that you can declare staticinner classes/members/functions, BUTthat class should be a parent class. You cannot do that inside an inner class.

Java 和 Android 都支持您可以声明static内部类/成员/函数,该类应该是父类。你不能在内部类中这样做。

I.e., class Maincan have static class Adapterbut if the Adapterclass is an inner class of Mainis 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 staticthen the immediate parent class must be the top-level, main class in that file.

如果要将类的任何成员声明为,static则直接父类必须是该文件中的顶级主类。

Why?

为什么?

Quoting another answer, It's because an inner class is implicitly associated with an instance of its outer class, it cannot define any static methods itself. Since a static nested class cannot refer directly to instance variables or methods defined in its enclosing class, it can use them only through an object reference, it's safe to declare static methods in a static nested class.

引用另一个答案,这是因为内部类与其外部类的实例隐式关联,它本身不能定义任何静态方法。由于静态嵌套类不能直接引用其封闭类中定义的实例变量或方法,因此只能通过对象引用使用它们,因此在静态嵌套类中声明静态方法是安全的。

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

3 http://viralpatel.net/blogs/inner-classes-in-java/

3 http://viralpatel.net/blogs/inner-classes-in-java/

回答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

那应该处理错误