Reputation: 5698
I have activity with fragments:
for (String tab_name : tabs) {
getActionBar().addTab(mActionBar.newTab().setText(tab_name)
.setTabListener(this));
}
FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction().add(new FirstFragment(), "TEST").commit();
fm.beginTransaction().add(new SecondFragment(), "TEST").commit();
fm.beginTransaction().add(new ThirdFragment(), "TEST").commit();
When I want to call new Activity from my SecondFragment:
Intent intent = new Intent(App.context, SomeActivity.class);
startActivity(intent);
It crashes with this error:
03-22 00:22:19.439: E/AndroidRuntime(17438): java.lang.IllegalStateException: Fragment ... not attached to Activity
App.context
is from MainActivity:
App.context = getApplicationContext();
What I can know that to attach fragment to activity, can be done by adding the fragment to fragmentManager, but it still crash. What I am wrong here?
Upvotes: 0
Views: 678
Reputation: 12022
As holding a context is very bad. Just use this
Intent intent = new Intent(getActivity(), SomeActivity.class);
startActivity(intent);
Upvotes: 0
Reputation: 12378
Instead of
Intent intent = new Intent(App.context, SomeActivity.class);
startActivity(intent);
use
Intent intent = new Intent(getActivity(), SomeActivity.class);
startActivity(intent);
instead of using App.context
use getActivty()
Upvotes: 1