[英]Using findViewById inside a Fragment
我正在創建一個新的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>)
runOnUiThread
Error:(88, 29) error: cannot find symbol method runOnUiThread(<anonymous Runnable>)
我知道如果我的類從Activity
擴展但它是一個制表符片段,這些將工作正常,所以我如何使用這個功能?
你需要
classListView = (ListView) rootView.findViewById(R.id.classList);
因為這是你關心的觀點
要獲得runOnUiThred,你需要做getActivity().runOnUiThread
你需要使用視圖引用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());
這是findViewById
方法的岸邊解釋;
findViewById()
方法將使用主布局綁定的相關對象。 例如,在一個活動中,當你做一個
setContentView(activity_main);
您暗示您的Activity
將從activity_main
檢索其布局。 因此,當您在活動中創建findViewById()
時,實際上這意味着this.findViewById()
, this
是您的Activity
。
所以在你的情況下,布局class_view
綁定到rootView
。 但是當你只編寫findViewById
,就意味着this.findViewById
。 this
就是你所處的范圍,在你的情況下是Fragment
。 但是,我不認為Fragment具有這種能力。 所以你應該引用rootView
來檢索你的視圖。
rootView.findViewById()
片段沒有像Activity這樣的findViewById()方法,因此您必須調用片段的根視圖的方法。 嘗試改為使用rootView.findViewById(R.id.classList)。
@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;
使用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;
}
}
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);
}
}`
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.