2014-09-24 2 views
1

내 조각이 버튼을 초기화, 나는 디버거에서 볼 수 있습니다. 레이아웃의 조각이있는 활동에서 setButtonText 메서드를 호출하면 단추가 null이됩니다. 나는 인터넷에서 봤는데하지만 지금까지 내가 조각 캡슐화하는 클래스의 모든 솔루션안드로이드 조각 잊어 버렸습니다 버튼

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"); 


}  

답변

0

하여 FragmentTransaction 실시간으로 실행되지 않습니다, 당신이에게 부울 결과를 호출하려면 result = fragmentTransaction.executePendingTransactions(); 버튼을 업데이트하기 전에. 또한, 귀하의 단편 내의 단추를 업데이트하지 않으시겠습니까?

+0

버튼의 텍스트를 다른 클래스에서 때때로 변경해야합니다. – PrisonMike

+0

그리고 executePendingTransactions가 문제를 해결합니까? –

+0

아니, btw 아니 fragmentManager.executePendingTransactions()? 코드 조각을 재사용하기 위해 조각을 사용하고 싶습니다 ... – PrisonMike

관련 문제