gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 23:40:51 26 4
gpt4 key购买 nike

我使用的是 Android Studio (Beta),在 'onCreateView()' 中使用此 java 代码时,出现错误。

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

这是错误:

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

我该如何解决这个问题?

最佳答案

假设您在 Activity 中有一个静态 fragment 内部类:您试图调用该 Activity 的 findViewById() ,而在不包含对 parent 。

onCreateView() 中,您需要在刚刚膨胀的 Root View 上调用它,例如

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

关于java - 错误 : Non-static method 'findViewById(int)' cannot be referenced from a static context,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25367060/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com