1
我的片段初始化按鈕,我可以看到,在調試器。 當我從佈局中有片段的活動調用setButtonText方法時,該按鈕將變爲null。我看着在互聯網上,但到目前爲止,我沒有發現,在類中的任何解決方案Android的片段忘記按鈕
public class PrepareTrainingFragment extends Fragment
{
private Button button;
private TextView tv;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View v = inflater.inflate(R.layout.preparetrainingfragment, container, false);
button=(Button) v.findViewById(R.id.ptfragmentbtn);
Log.w("button init","button init");
return v;
}
public void setButtonText(String text)
{
button.setText(text);
}
封裝的片段:
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION); //hide navigation bar temporarily
driverFragment=new PrepareTrainingFragment();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction();
fragmentTransaction.replace(android.R.id.content, driverFragment);
driverFragment.setButtonText("hello");
}
導致按鈕上的文本有時需要從其他類中更改 – PrisonMike 2014-09-24 21:45:50
並且executePendingTransactions是否解決了問題? – 2014-09-24 22:04:44
nope它不,btw不是它fragmentManager.executePendingTransactions()? 我只是想使用片段來重用其中的代碼...... – PrisonMike 2014-09-24 22:06:49