Android findViewById返回null(再次)

Android findViewById返回null(再次),android,android-fragments,findviewbyid,Android,Android Fragments,Findviewbyid,我已经浏览了我能找到的解决方案的大多数问题,但似乎没有一个建议是我的问题。我尝试过清理和重建项目,并重新启动android studio。在尝试检索UI元素之前,请确保已完成膨胀,并且我已尝试使用几种不同的方法放置findViewById。我认为应该是这样(但仍然不起作用)。在提交片段事务后,我还尝试在父活动的on Create上检索其他视图。也没有成功 @Override public View onCreateView(LayoutInflater inflater, ViewGroup c

我已经浏览了我能找到的解决方案的大多数问题,但似乎没有一个建议是我的问题。我尝试过清理和重建项目,并重新启动android studio。在尝试检索UI元素之前,请确保已完成膨胀,并且我已尝试使用几种不同的方法放置findViewById。我认为应该是这样(但仍然不起作用)。在提交片段事务后,我还尝试在父活动的on Create上检索其他视图。也没有成功

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);
    View v = inflater.inflate(R.layout.search_fragment,container,false);

    seekBarDuration = (SeekBar) getActivity().findViewById(R.id.seekBarDuration);
    return v;
}

@Override
public void onActivityCreated (Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    seekBarDuration.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
应用程序在最后一行崩溃,因为SeekBaduration是空引用。
Seekbar位于片段自己的布局中。

不要在
onCreateView()中调用
getActivity()
上的
findViewById()
。在刚刚充气的
视图中调用它

注意,替换:

seekBarDuration = (SeekBar) getActivity().findViewById(R.id.seekBarDuration);
与:


不要在
onCreateView()
中的
getActivity()
上调用
findViewById()
。在刚刚充气的
视图中调用它

注意,替换:

seekBarDuration = (SeekBar) getActivity().findViewById(R.id.seekBarDuration);
与:


您好,您正试图从活动中获取它当您声明您的视图位于片段上时,请将代码更改为这样

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View v = inflater.inflate(R.layout.search_fragment,container,false);

        seekBarDuration = (SeekBar) v.findViewById(R.id.seekBarDuration);
//Also set listener in here since seekBarDuration is on the Fragment not in Activity
  seekBarDuration.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {});


        return v;
    }

    @Override
    public void onActivityCreated (Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    //This is called when the Activity is created, not the fragment.
}
我还更改了设置
侦听器的位置,您可以将其保留在
@onCreateView
中,或者让
片段
实现它,并将其设置为
seekbarderation.setonseekbarchaneglistener(此)

希望能有帮助


有关

嗨,您试图从活动中获取它的更多信息当您声明您的视图位于片段`上时,请将代码更改为这样

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View v = inflater.inflate(R.layout.search_fragment,container,false);

        seekBarDuration = (SeekBar) v.findViewById(R.id.seekBarDuration);
//Also set listener in here since seekBarDuration is on the Fragment not in Activity
  seekBarDuration.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {});


        return v;
    }

    @Override
    public void onActivityCreated (Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    //This is called when the Activity is created, not the fragment.
}
我还更改了设置
侦听器的位置,您可以将其保留在
@onCreateView
中,或者让
片段
实现它,并将其设置为
seekbarderation.setonseekbarchaneglistener(此)

希望能有帮助

更多关于