java.lang.IllegalStateException:没有活动

时间:2013-04-03 09:52:38

标签: android android-fragments android-tabhost

我正在构建一个Android应用,我希望我的第一个活动由2个标签组成,一个用于用户的个人资料,另一个用于朋友的活动。对于这些标签,我选择了TabHost,因为我的Sherlock Action Bar已经使用列表导航移动到其他活动,因此我无法使用操作栏中的标签导航。

该应用程序工作了一段时间,但现在只有我的一个标签工作,一旦我尝试移动到第二个标签,我得到一个java.lang.IllegalStateException:没有活动

我为这个错误添加了Logcat输出,虽然我不确定它是否有帮助,因为我的代码没有回溯。

我尝试用FragmentTabHost替换TabHost,但错误仍然存​​在......

有人可以指出这个错误的起源/含义吗?提前谢谢!

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)

活性

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>

5 个答案:

答案 0 :(得分:23)

最后发现了这个问题:“无活动”崩溃的原因是我使用错误的FragmentManager来嵌套碎片。

对于嵌套片段,唯一有效的FragmentManager是通过调用getChildFragmentManager()在包含片段中获得的片段。

答案 1 :(得分:11)

我也遇到了这个错误。最后,发现我正在重写onResume()onStop()上的方法

@Override
protected void onResume() {
    super.onResume();
    initilizeMap();
}
@Override
protected void onStop() {
    super.onResume();
    finish();
}

将其更改为

 @Override
protected void onResume() {
    super.onResume();
    initilizeMap();
}
@Override
protected void onStop() {
    super.onStop();
    finish();
}

如此愚蠢的错误:P

答案 2 :(得分:1)

您需要在保存活动状态后提交事务...

使用ft.commitAllowingStateLoss()代替ft.commit()

这将解决您的问题。

Source

答案 3 :(得分:0)

我在myActivity.onCreate()中通过调用:

创建了一个片段
getSupportFragmentManager()
    .beginTransaction()
    .remove(frag)
    .replace(idOfViewBeingReplaced, frag)
    .commit();

在查看我的应用程序时,我会点击电源关闭按钮然后应用程序会崩溃。显然,当设备关闭时,活动和片段状态将被保存,分离,然后在片段上调用onCreate()onCreateView()。在这种状态下,.remove(frag)无法被调用,否则它将抛出:

  

非法国家:没有活动

所以,我刚刚删除了.remove(frag),一切都恢复了。

答案 4 :(得分:0)

在我的情况下,我使用了启动画面,并且我在MainActivity中有MainLauncher = true。