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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25367060/
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 07:54:24  来源:igfitidea点击:

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

javaandroidandroid-listviewandroid-studio

提问by Quack

I am using Android Studio (Beta), and while using this java code in 'onCreateView()', I get an error.

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

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

This is the error:

这是错误:

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

How do I fix this?

我该如何解决?

回答by laalto

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.

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

In onCreateView()you need to call it on the root view you just inflated, e.g.

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

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

回答by Eran

onCreateView()shouldn't be a static method (I'm assuming you are defining it within an Activity class), so you must be doing something wrong.

onCreateView()不应该是静态方法(我假设您是在 Activity 类中定义它),所以您一定是做错了什么。

回答by live-love

If you are using it in an static AsyncTask, or any other class, you can pass the activity as a parameter to a method or constructor, for example:

如果您在静态 AsyncTask 或任何其他类中使用它,则可以将活动作为参数传递给方法或构造函数,例如:

activity onCreate:

活动创建:

//Pass activity variable (this)
new Main2Activity.MyTask().execute(this);

class inside activity:

课内活动:

private static class MyTask extends AsyncTask<Object, Void, String> {

        Main2Activity activity;

        @Override
        protected String doInBackground(Object... params) {
            activity = (Main2Activity)params[0];
            ....
        }

        @Override
        protected void onPostExecute(String str) {
            // Use parameter activity passed to class
            WebView webView = activity.findViewById(R.id.web_view);
            webView.loadData(str, "text/html; charset=UTF-8", null);
        }

回答by Nadeem Iqbal

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.frag_layout, container, false);
    ListView listView = (ListView) view.findViewById(R.id.listviewID);
    context = getActivity();
    return view;
}

You can implement this now... Use viewvariable to access xml ui things in oncreateView and getActivity().findViewById(...) to access other then onCreateView() Method.

你现在可以实现这个...使用视图变量访问 oncreateView 和 getActivity().findViewById(...) 中的 xml ui 东西来访问其他然后 onCreateView() 方法。

回答by Aravindh Gopi

Inside Activity class

活动类内部

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

OR

或者

to create inside fragmentClass(static)

在 fragmentClass(static) 内部创建

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