我想在我的ProfileActivity中調用findViewById,它有三個片段,在這些片段的一個ProfileFragment中,我放置了4個空的TextView項目,以便可以根據登錄用戶更改它們。我不能在片段中使用getView()。findViewById,因爲我需要調用基類中的大型方法,該方法由ProfileActivity擴展,所以我可以在那裏調用它。但是,無論何時在onCreate方法中的profileActivity中調用findViewById時,都會發生錯誤,因爲片段尚未創建。任何建議,我應該做這部分?FindViewById在視圖中,但加載後的片段
public void setInfo(){
//TextView name = (TextView)findViewById(R.id.profile_name);
TextView name1 = (TextView)findViewById(R.id.profile_name1);
TextView age = (TextView)findViewById(R.id.profile_age);
TextView gender = (TextView)findViewById(R.id.profile_gender);
TextView email = (TextView)findViewById(R.id.profile_email);
User u = getCurrentUser();
//name.setText(u.getUserName());
name1.setText("test");
age.setText("test");
email.setText("test");
gender.setText("test");
}
此代碼現在在ProfileActivity中,並且4個TextViews在片段中。
如何在地球上你想訪問一個觀點,即是一個片段內?你有沒有聽說過片段交流?如果不。我建議你這樣做! – k0sh
*如何在地球上訪問一個片段內的視圖?*可能...與findViewById ...整個問題是片段必須被連接...(當然,它不會使sens,因爲Activity不應該知道片段的視圖) – Selvin
使用構造函數將值傳遞到'ProfileFragment'中。 –