本文介绍了错误:无法从静态上下文引用非静态方法“findViewById(int)”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用Android Studio(Beta),在'onCreateView()'中使用此java代码时,出现错误。
I am using Android Studio (Beta), and while using this java code in 'onCreateView()', I get an error.
ListView listView = (ListView) findViewById(R.id.someListView);
这是错误:
Non-static method 'findViewById(int)' cannot be referenced from a static context
如何解决此问题?
推荐答案
假设您在活动中有一个静态片段内部类:您是试图调用活动的 findViewById()
,你不能在一个没有父类引用的静态内部类中。
Assuming you have a static fragment inner class inside an activity: you're trying to call the activity's findViewById()
which you cannot in a static inner class that doesn't hold a reference to the parent.
在 onCreateView()
中,您需要在刚刚膨胀的根视图上调用它,例如
In onCreateView()
you need to call it on the root view you just inflated, e.g.
ListView listView = (ListView) rootView.findViewById(R.id.someListView);
这篇关于错误:无法从静态上下文引用非静态方法“findViewById(int)”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!