我正在构建一个 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>