2015-01-20 108 views
2

我创建一个新选项卡界面内,每一个选项卡我有功能,例如:使用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) findViewById(R.id.classList); 

     return rootView; 
    } 
} 

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

我知道这些将工作正常,如果我的课程从Activity扩展,但它是一个选项卡片段,所以我如何使用此功能?

+0

'classListView =(ListView中)rootView.findViewById(R.id。班级列表); – DeeV 2015-01-20 18:45:25

+1

可能重复的[findViewById在片段android](http://stackoverflow.com/questions/6495898/findviewbyid-in-fragment-android)...什么可以通过简单的谷歌搜索... – Selvin 2015-01-20 19:38:03

+0

@Selvin是的它是。让投票关闭它作为重复的[findViewById在片段](http://stackoverflow.com/questions/6495898/findviewbyid-in-fragment)! :) – Sufian 2016-10-20 07:44:23

回答

6

你需要

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

,因为这是你所关注的

的观点得到runOnUiThred你需要做的getActivity().runOnUiThread

+0

'rootView'对象只在'onCreateView'中有accesbile,而不在类中的其他函数中? – Maven 2015-01-20 19:04:52

+0

正确的,如果你需要它在其他地方只是全球举行。你也可以抓住你特别需要的物品,而不是整个视野 – tyczj 2015-01-20 19:05:59

1

片段没有findViewById()之类的活动方法,所以你必须调用片段根视图的方法。改为尝试做rootView.findViewById(R.id.classList)。

6

您需要使用视图来引用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是Modifying an Android view from a different thread

可能重复使用其像,

activity.runOnUiThread(new Runnable()); 
2

这里是findViewById岸解释方法;

findViewById()方法将与主布局绑定的相关对象一起使用。例如,在一个活动,当你犯了一个

setContentView(activity_main); 

你意味着你Activity将从activity_main检索其布局。所以当你在一个活动中制作findViewById()时,这实际上意味着this.findViewById(),其中this是你的Activity

所以在你的情况下,布局class_view绑定到rootView。但是当你只写findViewById,这意味着this.findViewByIdthis是你所在的范围,你的情况就是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

使用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必须内部onViewCreated run()方法

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); 
} 

}`