在 Fragment 中使用 findViewById

3
我正在创建一个新的标签界面,对于每个标签,我都有以下功能:
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>)的错误。

我知道如果我的类继承自Activity,这些方法将正常工作,但它是一个选项卡片段,那么我该如何使用这些函数呢?


classListView = (ListView) rootView.findViewById(R.id.classList); - DeeV
1
可能是在 Android 片段中使用 findViewById 的重复问题。... 通过简单的谷歌搜索可以找到的内容... - Selvin
@Selvin 是的,让我们投票将其关闭为在Fragment中使用findViewById的重复问题! :) - Sufian
7个回答

5
你需要的是:
classListView = (ListView) rootView.findViewById(R.id.classList);

因为您关心的是此视图,所以需要进行翻译。

要使用runOnUiThred,您需要执行 getActivity().runOnUiThread


rootView 对象只能在 onCreateView 中访问,而不能在类的其他函数中访问? - user971741
如果你需要的话,可以正确地处理全局变量,或者只是保留你特定需要的项目,而不是整个视图。 - tyczj

5
您需要使用视图引用findViewById。
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) rootView .findViewById(R.id.classList);

        return rootView;
    }
}

对于 runOnUiThread,它可能是 在不同线程中修改 Android 视图 的一个重复问题。

使用方法如下:

activity.runOnUiThread(new Runnable());

1
这里是findViewById方法的简要说明:
findViewById()方法将与您的主布局绑定的相关对象一起工作。例如,在一个活动中,当您进行以下操作时:
setContentView(activity_main);

您的Activity将从activity_main检索其布局。因此,当您在活动中进行findViewById()时,实际上是指this.findViewById(),其中this是您的Activity
在您的情况下,布局class_view绑定到rootView。但是,当您只写findViewById时,它意味着this.findViewById。而this是您所在的范围,即您的情况下的Fragment。但是,我认为片段没有这个功能。因此,您应该参考rootView来检索视图。
rootView.findViewById()

0
 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v =  inflater.inflate(R.layout.fragment_content_mediaplayer, container, false);
        mSeekbar = (SeekBar) v.findViewById(R.id.seekBarMusic);
        return v;

0

findViewById必须在onViewCreated()方法中运行

ListView classListView = null;

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

    return inflater.inflate(R.layout.class_view, container, false);
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    classListView = (ListView) view.findViewById(R.id.classList);
}

}`


0

使用rootView.findViewById(R.id.classList)代替findViewById(R.id.classList)

尝试一下:

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) rootView.findViewById(R.id.classList);

        return rootView;
    }
}

0
碎片没有像活动那样的findViewById()方法,因此您必须调用碎片的根视图的方法。尝试使用rootView.findViewById(R.id.classList)。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接