我正在構建一個Android應用程序,我希望我的第一個活動由2個選項卡組成,其中一個用於用戶配置文件,另一個用於朋友的活動。對於這些標籤,我選擇了TabHost,因爲我的Sherlock Action Bar已經在使用列表導航來移動到其他活動,所以我無法使用操作欄中的標籤導航。java.lang.IllegalStateException:No activity
的應用程序工作了一段時間,但現在只有我一個選項卡的工作原理,當我嘗試移動到第二個選項卡,我得到一個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清單
<?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>
發佈您的AndroidManifest.xml文件代碼 – Pratik
Android Manifest已添加。 – 2Dee
在Activity類的上方,你還可以定義你的包嗎? 'package = com.enlavertical;' – Bigflow