10

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

该应用程序工作了一段时间,但现在只有一个选项卡可以工作,一旦我尝试移动到第二个选项卡,我就会收到 java.lang.IllegalStateException: No activity

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

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

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

日志猫:

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;
}

安卓清单

<?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>
4

5 回答 5

24

终于知道这是怎么回事了:“无活动”崩溃是由于我使用错误FragmentManager嵌套 Fragments 造成的。

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

于 2013-06-16T08:15:33.583 回答
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

于 2014-11-03T12:38:27.257 回答
1

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

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

这将解决你的问题。

来源

于 2015-05-11T16:19:28.860 回答
0

myActivity.onCreate()我通过调用创建了一个片段:

getSupportFragmentManager()
    .beginTransaction()
    .remove(frag)
    .replace(idOfViewBeingReplaced, frag)
    .commit();

在查看我的应用程序时,我会按下关机按钮,然后应用程序就会崩溃。显然,当设备断电时,Activity 和 Fragment 状态会被保存、分离,然后onCreate()onCreateView()Fragments 上被调用。在这种状态下,.remove(frag)不能被调用,否则会抛出:

非法状态:无活动

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

于 2015-08-26T19:34:50.587 回答
0

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

于 2018-01-20T15:36:07.790 回答