错误:无法从静态上下文中引用非静态方法'findViewById(int)'

嘎嘎

我正在使用Android Studio(测试版),并且在“ onCreateView()”中使用此Java代码时,出现错误。

ListView listView = (ListView) findViewById(R.id.someListView);

这是错误:

Non-static method 'findViewById(int)' cannot be referenced from a static context

我该如何解决?

波:

假设您在活动中有一个静态片段内部类:您正在尝试调用活动的活动findViewById(),而该活动不能在不包含对父级引用的静态内部类中调用

在其中,onCreateView()您需要在刚膨胀的根视图上调用它,例如

 ListView listView = (ListView) rootView.findViewById(R.id.someListView);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

非静态方法<T> findViewById(int)不能从静态上下文中引用

如何解决此错误?:无法从静态上下文中引用非静态方法 maxOf(int,int)

不能从静态上下文中引用非静态方法“inflate(byte[],int,int)”

非静态方法count(int)不能从静态上下文中引用

非静态方法'getSharedPreferences(java.lang.String,int)'不能从静态上下文中引用

无法从活动错误类型静态引用非静态方法findViewById(int)

错误:无法在静态上下文中引用非静态方法“ edit”

错误:无法从静态上下文中引用非静态方法 setListMovie(ArrayList<Movie>)

出现错误:无法从静态上下文中引用非静态方法'getResources()'

错误:无法从静态上下文中引用非静态方法 getActivity()

“无法从静态上下文引用非静态方法”错误

我在animate()中遇到错误,这是“错误:无法从静态上下文中引用非静态方法animate()”

无法在非静态上下文中访问静态方法

错误:无法从Java的静态上下文中引用非静态变量扫描

Android:非静态方法无法从静态上下文中引用。困惑?

“无法从静态上下文中引用非静态方法”背后的原因是什么?

无法从静态上下文中引用luckyperms 非静态方法getUserManager()

无法从静态上下文引用Java非静态方法

无法解析方法'findViewById(int)'

setDisplayHomeAsUpEnabled()错误:无法从静态上下文引用非静态方法

错误:无法从静态上下文引用非静态方法unbind()

使用.setText在jTextArea上打印将返回错误非静态变量,无法从静态上下文中引用

java:不能从静态上下文中引用非静态变量错误

试图让我的显示方法工作(不能从静态上下文中引用非静态方法)

从静态上下文中的 a 调用非静态方法

从非静态上下文中调用静态方法

无法从类型2静态引用非静态方法fxn(int)

非静态方法不能从静态上下文中引用

非静态方法不能从静态上下文中引用