android - 在 Fragment 中使用 findViewById

标签 android android-fragments

我正在创建一个新的 Tab 界面,对于每个选项卡,我都具有如下功能:

public class ClassViewFragment extends Fragment {
    ListView classListView = null;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.class_view, container, false);

        classListView = (ListView) findViewById(R.id.classList);

        return rootView;
    }
}

但我得到 cannot resolve method findViewById同样在另一个地方我也无法使用runOnUiThread并得到 Error:(88, 29) error: cannot find symbol method runOnUiThread(<anonymous Runnable>)

我知道如果我的类(class)是从 Activity 扩展的,这些会很好用但它是一个标签 fragment ,所以我该如何使用这个功能?

最佳答案

你需要

classListView = (ListView) rootView.findViewById(R.id.classList);

因为那是你关心的观点

要获得 runOnUiThred,您需要执行 getActivity().runOnUiThread

关于android - 在 Fragment 中使用 findViewById,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28052862/

相关文章:

android - 如何在布局/绘图发生之前更改屏幕方向?

android - 尝试将数据( bundle/附加)传递到 fragment 中

android - 单击侧滑菜单中的项目时打开 Webview

java.lang.ClassCastException 无法转换为 android.app.Fragment

android - 如何访问不同 Activity 中 fragment 的 contentResolver?

java - (Project Tango)通过区域学习进行点云的旋转和平移

java - 如何在Android中使用Google Map api知道Google Map已加载我们当前的位置?

android - 所有 Activity View 均未选择主题

android - 找不到来源

android - 在两项 Activity 之间来回切换的最佳方式?