Reputation: 8629
I'm building an Android app for which I'd like my first activity to be composed of 2 tabs, one for the user's profile, and one for the friends' activity. For these tabs, I opted for a TabHost since my Sherlock Action Bar is already using list navigation to move to other activities so I can't use tab navigation from the action bar.
The app worked for a while, but now only one of my tabs works, as soon as I try to move to the second tab, I'm getting a java.lang.IllegalStateException: No activity
I added the Logcat output for this error, although I'm not sure it helps since there is no trace back to my code.
I tried replacing the TabHost with a FragmentTabHost, but the error persists...
Can anyone kindly point me to the origin/meaning of this error ? Thanks in advance !
LOGCAT :
04-03 08:19:39.380: W/dalvikvm(958): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
04-03 08:19:39.400: E/AndroidRuntime(958): FATAL EXCEPTION: main
04-03 08:19:39.400: E/AndroidRuntime(958): java.lang.IllegalStateException: No activity
04-03 08:19:39.400: E/AndroidRuntime(958): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1075)
04-03 08:19:39.400: E/AndroidRuntime(958): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1070)
04-03 08:19:39.400: E/AndroidRuntime(958): at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1861)
04-03 08:19:39.400: E/AndroidRuntime(958): at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1474)
04-03 08:19:39.400: E/AndroidRuntime(958): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:931)
04-03 08:19:39.400: E/AndroidRuntime(958): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
04-03 08:19:39.400: E/AndroidRuntime(958): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
04-03 08:19:39.400: E/AndroidRuntime(958): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
04-03 08:19:39.400: E/AndroidRuntime(958): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
04-03 08:19:39.400: E/AndroidRuntime(958): at android.os.Handler.handleCallback(Handler.java:725)
04-03 08:19:39.400: E/AndroidRuntime(958): at android.os.Handler.dispatchMessage(Handler.java:92)
04-03 08:19:39.400: E/AndroidRuntime(958): at android.os.Looper.loop(Looper.java:137)
04-03 08:19:39.400: E/AndroidRuntime(958): at android.app.ActivityThread.main(ActivityThread.java:5041)
04-03 08:19:39.400: E/AndroidRuntime(958): at java.lang.reflect.Method.invokeNative(Native Method)
04-03 08:19:39.400: E/AndroidRuntime(958): at java.lang.reflect.Method.invoke(Method.java:511)
04-03 08:19:39.400: E/AndroidRuntime(958): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-03 08:19:39.400: E/AndroidRuntime(958): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-03 08:19:39.400: E/AndroidRuntime(958): at dalvik.system.NativeStart.main(Native Method)
ACTIVITY
public class HomeActivity extends SherlockFragmentActivity implements ActionBar.OnNavigationListener {
private ActionBar mActionBar;
private TabHost tHost;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_activity_layout);
//Setting the list navigation on actionBar
mActionBar = getSupportActionBar();
Context context = getSupportActionBar().getThemedContext();
ArrayAdapter<CharSequence> list = ArrayAdapter.createFromResource(context, R.array.list_menu_items, R.layout.sherlock_spinner_item);
list.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);
mActionBar.setDisplayShowTitleEnabled(false);
mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
mActionBar.setListNavigationCallbacks(list, this);
//Setting the subnavigation with TabHost
tHost = (TabHost) findViewById(android.R.id.tabhost);
tHost.setup();
/** Defining tab builder for profile tab */
TabHost.TabSpec tabSpecProfile = tHost.newTabSpec("profile");
tabSpecProfile.setIndicator("PROFILE");
tabSpecProfile.setContent(new TabContentMaker(this));
tHost.addTab(tabSpecProfile);
/** Defining tab builder for community tab */
TabHost.TabSpec tabSpecCommunity = tHost.newTabSpec("community");
tabSpecCommunity.setIndicator("COMMUNITY");
tabSpecCommunity.setContent(new TabContentMaker(this));
tHost.addTab(tabSpecCommunity);
/** Defining Tab Change Listener event. This is invoked when tab is changed */
TabHost.OnTabChangeListener tabChangeListener = getOnTabChangeListener();
/** Setting tabchangelistener for the tab */
tHost.setOnTabChangedListener(tabChangeListener);
}
@Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
//changing activity here
return true;
}
private TabHost.OnTabChangeListener getOnTabChangeListener(){
TabHost.OnTabChangeListener tabChangeListener = new TabHost.OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
MyProfileFragment profileFragment = (MyProfileFragment) fm.findFragmentByTag("profile");
CommunityFeedFragment communityFragment = (CommunityFeedFragment) fm.findFragmentByTag("community");
android.support.v4.app.FragmentTransaction ft = fm.beginTransaction();
/** Detaches the profileFragment if exists */
if(profileFragment!=null)
ft.detach(profileFragment);
/** Detaches the communityFragment if exists */
if(communityFragment!=null)
ft.detach(communityFragment);
/** If current tab is profile */
if(tabId.equalsIgnoreCase("profile")){
if(profileFragment==null){
/** Create MyProfileFragment and adding to fragmenttransaction */
ft.add(R.id.realtabcontent,new MyProfileFragment(), "profile");
}else{
/** Bring to the front, if already exists in the fragmenttransaction */
ft.attach(profileFragment);
}
}else{ /** If current tab is community */
if(communityFragment==null){
/** Create CommunityFragment and adding to fragmenttransaction */
ft.add(R.id.realtabcontent,new CommunityFeedFragment(), "community");
}else{
/** Bring to the front, if already exists in the fragmenttransaction */
ft.attach(communityFragment);
}
}
ft.commit();
}
};
return tabChangeListener;
}
ANDROID MANIFEST
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.enlavertical"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.Styled" >
<activity
android:name="com.enlavertical.HomeActivity"
android:label="@string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Upvotes: 10
Views: 23625
Reputation: 4234
In my case I using a splash screen and I have had MainLauncher = true in the MainActivity.
Upvotes: 0
Reputation: 8629
Finally found out what this was about: the "no activity" crash was due to the fact that I was using a wrong FragmentManager
to nest Fragments.
For nested fragments, the only valid FragmentManager
is the one obtained in the containing Fragment, by calling getChildFragmentManager()
.
Upvotes: 24
Reputation: 3574
I was creating a fragment in myActivity.onCreate()
by calling:
getSupportFragmentManager()
.beginTransaction()
.remove(frag)
.replace(idOfViewBeingReplaced, frag)
.commit();
While viewing my app I would hit the power-off button and then the app would crash. Apparently, when the device is powered off, the Activity and Fragment states are saved, detached, and then onCreate()
and onCreateView()
are called on the Fragments. In this state, .remove(frag)
cannot be called or else it will throw:
illegal state: no activity
So, I just deleted .remove(frag)
and everything is working again.
Upvotes: 0
Reputation: 1620
I got this error too. Finally, found out that I was overriding the methods onResume() onStop() incorrectly
@Override
protected void onResume() {
super.onResume();
initilizeMap();
}
@Override
protected void onStop() {
super.onResume();
finish();
}
Changed it to
@Override
protected void onResume() {
super.onResume();
initilizeMap();
}
@Override
protected void onStop() {
super.onStop();
finish();
}
So Silly Mistake :P
Upvotes: 11
Reputation: 634
You need that the transaction to be committed after an activity's state is saved...
Use ft.commitAllowingStateLoss()
instead of ft.commit()
That will solve your problem.
Upvotes: 1