2017-06-05 57 views
0

我剛開始使用片段。我在片段中創建了一個編輯文本,我想從活動中填充它,但它給了我空指針異常。可能是在片段事務提交後運行OnCreateView。我用另一種方式使它工作,但我只是有點好奇它爲什麼不起作用。我搜查了它,但找不到我的問題的答案。任何幫助,將不勝感激設置活動給出空指針的片段的編輯文本

這裏是我的代碼在活動:

FragmentManager fragmentManager = getFragmentManager(); 
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
Myfragment mFragment = new Myfragment(); 
fragmentTransaction.replace(R.id.fragment_container, mFragment); 
fragmentTransaction.commit(); 

mFragment.set_fragment_editext("Hello world");//setting edit text in fragment throws exception 

然後在片段類:您撥打的setText功能,因此空指針後

public class Myfragment extends Fragment { 
    EditText edittext_fragment; 

    public static Myfragment newInstance() { 
     return new Myfragment(); 
    } 


    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View myInflatedView = inflater.inflate(R.layout.Myfragment, container, false); 
     InitializeFragment(myInflatedView); 
     return myInflatedView; 
    } 

    private void InitializeFragment(View myInflatedView) { 
     edittext_fragment = (EditText) myInflatedView.findViewById(R.id.edittext_fragment_caption); 
    } 

    public void set_fragment_editext(String value) { 
     edittext_fragment.setText(value); 
    } 

} 

回答

0

oncreateview被調用。

將字符串作爲參數傳遞。

public class Myfragment extends Fragment { 
EditText edittext_fragment; 
String textViewText; 

public static Myfragment newInstance(String textViewText){ 
this.textViewText = textViewText; 
return new Myfragment(); 
} 

@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
View myInflatedView = inflater.inflate(R.layout.Myfragment, container, false); 
InitializeFragment(myInflatedView); 
set_fragment_editext(textViewText); 
return myInflatedView; 
} 

private void InitializeFragment(View myInflatedView) { 
edittext_fragment = (EditText) myInflatedView.findViewById(R.id.edittext_fragment_caption); 
} 
private void set_fragment_editext(String value) { 
edittext_fragment.setText(value); 
} 
} 

活動碼:

Myfragment mFragment = Myfragment.newInstance("Hello World"); 
0

1)它需要一些時間來穿過片段生命週期。

2)它發生在後臺。

Myfragment mFragment = new Myfragment(); 
fragmentTransaction.replace(R.id.fragment_container, mFragment); 
fragmentTransaction.commit(); 

// EditText is not yet initialized 
mFragment.set_fragment_editext("Hello world");//setting edit text in fragment throws exception 

這是常見的android任務,你應該做一些事情,只有當一些組件被初始化。

在你的情況,你可以通過幾種方式做到這一點:

1)推遲串集。

class Myfragment extedns Fragment { 
    private String mEditTextString; 
    private EditText edittext_fragment; 

    public void setEditText(String string) { 
     mEditTextString = string; 
     if (edittext_fragment != null) { 
      edittext_fragment.setText(string); 
     } 
    } 

    private void InitializeFragment(View myInflatedView) { 
     edittext_fragment = (EditText) myInflatedView.findViewById(R.id.edittext_fragment_caption); 
     if (mEditTextString != null) { 
      edittext_fragment.setText(mEditTextString); 
     } 
    } 
} 

2)編輯文本初始化時通過回調活動。請看看本指南https://developer.android.com/guide/components/fragments.html#CommunicatingWithActivity

3)正如@Karishnu波達提到的,如果你要設置編輯文本只有一次,你可以通過它進行分段參數

+0

非常感謝您的回答似乎是正確的一個 – SimpleMan

+0

@SimpleMan直截了當地說這個解決方案存在問題。 onAttachFragment在片段中的onViewCreated之前被調用。請稍等,我會修復我的解決方案:) –

+0

@SimpleMan我修復了我的答案。對不起,第一個與onAttachFragment() –

相關問題