错误:无法从静态上下文中引用非静态方法“findViewById(int)" [英] Error: Non-static method 'findViewById(int)' cannot be referenced from a static context

查看:39
本文介绍了错误:无法从静态上下文中引用非静态方法“findViewById(int)"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Android Studio(测试版),在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

我该如何解决这个问题?

How do I fix this?

推荐答案

假设您在一个活动中有一个静态片段内部类:您正在尝试调用该活动的 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)"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆