Reputation: 1119
why i get null on the following code when i use getView?
public void menuItemSelected(int itemId) {
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.layout.fragment_main, mShoppingCartFragment, "ShoppingCartFragment");
ft.commit();
ft.addToBackStack(null);
View aaa = mShoppingCartFragment.getView(); <-- null here
TextView fk = (TextView)findViewById(R.id.textView1); <-- null here
fk.setText("clicked by " + itemId);
}
Upvotes: 0
Views: 108
Reputation: 863
As Steve mentioned FragmentTransaction.commit()
is an asynchronous call, but you can always call to:
ft.executePendingTransactions();
And you will block on this method until all the pending transactions will complete
Upvotes: 1
Reputation: 12933
The FragmentTransaction.commit() is an asynchronous call, hence it doesn't return immediatly. So, your Fragment could be null, if you call View aaa = mShoppingCartFragment.getView();
right after it.
Furthermore Fragment.getView() will be null, if the callback onCreateView(...)
hasn't returned or you return null for it, which means there is no View associated with this Fragment.
Both is possible in your code snippet.
If TextView fk = (TextView)findViewById(R.id.textView1);
returns null make sure, the id textView1 is part of the layout of the Activity. If it's a View for the Fragment get the reference there.
Upvotes: 0