2015-01-20 104 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); 
} 

}`