2015-12-14 20 views
-3

我想在我的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在片段中。

+0

如何在地球上你想訪問一個觀點,即是一個片段內?你有沒有聽說過片段交流?如果不。我建議你這樣做! – k0sh

+0

*如何在地球上訪問一個片段內的視圖?*可能...與findViewById ...整個問題是片段必須被連接...(當然,它不會使sens,因爲Activity不應該知道片段的視圖) – Selvin

+0

使用構造函數將值傳遞到'ProfileFragment'中。 –

回答

-1

您必須具有CREATE在片段的Java文件和像這樣的片段佈局:

public class InvestorProfileFragment{ 
    @Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

    View mRootView = inflater.inflate(R.layout.investor_profile_fragment, container, false); 
    init(mRootView); 
    return mRootView; 
} 

公共無效的init(查看視圖){

txvName = (TextView) view.findViewById(R.id.txv_name); 

}

並做你想要的...

+0

這個答案如何解決如何從Activity獲取TextBox的問題? – Selvin

+0

來自活動發送數據通過集Agrument和在片段中使用。不能玩小孩,直到他/她出生 –

1

你必須通過ProfileActivity當你實例化它時,你會進入ProfileFragment

public static final ProfileFragment newInstance(String name, String age, String gender, String email) 
{ 
    ProfileFragment f = new ProfileFragment(); 
    Bundle bdl = new Bundle(); 
    bdl.putInt(EXTRA_NAME, name); 
    bdl.putInt(EXTRA_AGE, age); 
    bdl.putInt(EXTRA_GENDER, gender); 
    bdl.putInt(EXTRA_EMAIL, email); 
    f.setArguments(bdl); 
    return f; 
} 

@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    name = getArguments().getString(EXTRA_NAME); 
    age = getArguments().getString(EXTRA_AGE); 
    gender = getArguments().getString(EXTRA_GENDER); 
    email = getArguments().getString(EXTRA_EMAIL); 

    ... 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.fragment_profile, container, false); 
    setUser(view); 

    ... 

    return view; 
} 

private void setUser(View view){ 

    TextView name1 = (TextView) view.findViewById(R.id.profile_name1); 
    TextView age = (TextView) view.findViewById(R.id.profile_age); 
    TextView gender = (TextView) view.findViewById(R.id.profile_gender); 
    TextView email = (TextView) view.findViewById(R.id.profile_email); 

    name1.setText(name); 
    age.setText(age); 
    email.setText(gender); 
    gender.setText(email); 
} 

,然後用它喜歡:

public void setInfo(){ 
    User u = getCurrentUser(); 
    get[Support]FragmentManager().beginTransaction().replace(R.id.fragment_placehoder_id, ProfileFragment.newInstance(u.getName(), u.getAge(), u.getGender(), u.getEmail())).commit(); 
} 
+0

好的,但是當他在活動的佈局中有他的片段時會有一些問題(因爲你不能在這種情況下使用setArguments情況)...所以這個片段應該創建並添加後,他獲得用戶信息... – Selvin

+0

是的,這取決於他實際上加載片段。但是這個問題其實並不明確。任何改進以添加@Selvin? –

+0

我會在答案(用法)中添加片段代碼的創建......這應該足夠了......除非他會編輯問題並使其更加清晰...... – Selvin