2012-10-29 47 views
1

創建標籤這裏是我的標籤屏幕代碼:習題使用FragmentActivity

CareActivity.xml

import java.util.HashMap; 

import android.content.Context; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentActivity; 
import android.support.v4.app.FragmentTransaction; 
import android.view.Menu; 
import android.view.View; 
import android.widget.TabHost; 

public class CareActivity extends FragmentActivity { 

    TabHost mTabHost; 
    TabManager mTabManager; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.activity_care); 
     mTabHost = (TabHost)findViewById(android.R.id.tabhost); 
     mTabHost.setup(); 

     mTabManager = new TabManager(this, mTabHost, R.id.realtabcontent); 

     /*TabSpec spec = mTabHost.newTabSpec("ARTICLES").setIndicator("ARTICLES").setContent(new Intent().setClass(this, ListingActivity.class).putExtra("tabId", 0)); 
     mTabHost.addTab(spec); 

     spec = mTabHost.newTabSpec("PRODUCTS").setIndicator("PRODUCTS").setContent(new Intent().setClass(this, ListingActivity.class).putExtra("tabId", 1)); 
     mTabHost.addTab(spec);*/ 

     mTabManager.addTab(mTabHost.newTabSpec("articles").setIndicator("ARTICLES"), 
       Listing411Activity.class, null); 

     mTabManager.addTab(mTabHost.newTabSpec("products").setIndicator("PRODUCTS"), 
       Listing411Activity.class, null); 

     if (savedInstanceState != null) { 
      mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab")); 
     } 
    } 

    @Override 
    protected void onSaveInstanceState(Bundle outState) { 
     super.onSaveInstanceState(outState); 
     outState.putString("tab", mTabHost.getCurrentTabTag()); 
    } 

    /** 
    * This is a helper class that implements a generic mechanism for 
    * associating fragments with the tabs in a tab host. It relies on a 
    * trick. Normally a tab host has a simple API for supplying a View or 
    * Intent that each tab will show. This is not sufficient for switching 
    * between fragments. So instead we make the content part of the tab host 
    * 0dp high (it is not shown) and the TabManager supplies its own dummy 
    * view to show as the tab content. It listens to changes in tabs, and takes 
    * care of switch to the correct fragment shown in a separate content area 
    * whenever the selected tab changes. 
    */ 
    public static class TabManager implements TabHost.OnTabChangeListener { 
     private final FragmentActivity mActivity; 
     private final TabHost mTabHost; 
     private final int mContainerId; 
     private final HashMap<String, TabInfo> mTabs = new HashMap<String, TabInfo>(); 
     TabInfo mLastTab; 

     static final class TabInfo { 
      private final String tag; 
      private final Class<?> clss; 
      private final Bundle args; 
      private Fragment fragment; 

      TabInfo(String _tag, Class<?> _class, Bundle _args) { 
       tag = _tag; 
       clss = _class; 
       args = _args; 
      } 
     } 

     static class DummyTabFactory implements TabHost.TabContentFactory { 
      private final Context mContext; 

      public DummyTabFactory(Context context) { 
       mContext = context; 
      } 

      @Override 
      public View createTabContent(String tag) { 
       View v = new View(mContext); 
       v.setMinimumWidth(0); 
       v.setMinimumHeight(0); 
       return v; 
      } 
     } 

     public TabManager(FragmentActivity activity, TabHost tabHost, int containerId) { 
      mActivity = activity; 
      mTabHost = tabHost; 
      mContainerId = containerId; 
      mTabHost.setOnTabChangedListener(this); 
     } 

     public void addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args) { 
      tabSpec.setContent(new DummyTabFactory(mActivity)); 
      String tag = tabSpec.getTag(); 

      TabInfo info = new TabInfo(tag, clss, args); 

      // Check to see if we already have a fragment for this tab, probably 
      // from a previously saved state. If so, deactivate it, because our 
      // initial state is that a tab isn't shown. 
      info.fragment = mActivity.getSupportFragmentManager().findFragmentByTag(tag); 
      if (info.fragment != null && !info.fragment.isDetached()) { 
       FragmentTransaction ft = mActivity.getSupportFragmentManager().beginTransaction(); 
       ft.detach(info.fragment); 
       ft.commit(); 
      } 

      mTabs.put(tag, info); 
      mTabHost.addTab(tabSpec); 
     } 

     @Override 
     public void onTabChanged(String tabId) { 
      TabInfo newTab = mTabs.get(tabId); 
      if (mLastTab != newTab) { 
       FragmentTransaction ft = mActivity.getSupportFragmentManager().beginTransaction(); 
       if (mLastTab != null) { 
        if (mLastTab.fragment != null) { 
         ft.detach(mLastTab.fragment); 
        } 
       } 
       if (newTab != null) { 
        if (newTab.fragment == null) { 
         newTab.fragment = Fragment.instantiate(mActivity, 
           newTab.clss.getName(), newTab.args); 
         ft.add(mContainerId, newTab.fragment, newTab.tag); 
        } else { 
         ft.attach(newTab.fragment); 
        } 
       } 

       mLastTab = newTab; 
       ft.commit(); 
       mActivity.getSupportFragmentManager().executePendingTransactions(); 
      } 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.activity_care411, menu); 
     return true; 
    } 

} 

active_care.xml

<TabHost 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/tabhost" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <TabWidget 
      android:id="@android:id/tabs" 
      android:orientation="horizontal" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="0"/> 

     <FrameLayout 
      android:id="@android:id/tabcontent" 
      android:layout_width="0dp" 
      android:layout_height="0dp" 
      android:layout_weight="0"/> 

     <FrameLayout 
      android:id="@+android:id/realtabcontent" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1"/> 

    </LinearLayout> 
</TabHost> 

ListingActivity.java

import android.os.Bundle; 
import android.support.v4.app.FragmentActivity; 
import android.view.Menu; 

public class ListingActivity extends FragmentActivity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_listing411); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.activity_listing411, menu); 
     return true; 
    } 

} 

它給ClassCastException。這裏是日誌貓:

10-29 17:52:18.706: E/AndroidRuntime(495): FATAL EXCEPTION: main 
10-29 17:52:18.706: E/AndroidRuntime(495): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jjwoundcare.main/com.jjwoundcare.main.CareActivity}: java.lang.ClassCastException: com.jjwoundcare.main.ListingActivity 
10-29 17:52:18.706: E/AndroidRuntime(495): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647) 
10-29 17:52:18.706: E/AndroidRuntime(495): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 
10-29 17:52:18.706: E/AndroidRuntime(495): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
10-29 17:52:18.706: E/AndroidRuntime(495): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 
10-29 17:52:18.706: E/AndroidRuntime(495): at android.os.Handler.dispatchMessage(Handler.java:99) 
10-29 17:52:18.706: E/AndroidRuntime(495): at android.os.Looper.loop(Looper.java:123) 
10-29 17:52:18.706: E/AndroidRuntime(495): at android.app.ActivityThread.main(ActivityThread.java:3683) 
10-29 17:52:18.706: E/AndroidRuntime(495): at java.lang.reflect.Method.invokeNative(Native Method) 
10-29 17:52:18.706: E/AndroidRuntime(495): at java.lang.reflect.Method.invoke(Method.java:507) 
10-29 17:52:18.706: E/AndroidRuntime(495): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
10-29 17:52:18.706: E/AndroidRuntime(495): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
10-29 17:52:18.706: E/AndroidRuntime(495): at dalvik.system.NativeStart.main(Native Method) 
10-29 17:52:18.706: E/AndroidRuntime(495): Caused by: java.lang.ClassCastException: com.jjwoundcare.main.ListingActivity 
10-29 17:52:18.706: E/AndroidRuntime(495): at android.support.v4.app.Fragment.instantiate(Fragment.java:388) 
10-29 17:52:18.706: E/AndroidRuntime(495): at com.jjwoundcare.main.CareActivity$TabManager.onTabChanged(CareActivity.java:138) 
10-29 17:52:18.706: E/AndroidRuntime(495): at android.widget.TabHost.invokeOnTabChangeListener(TabHost.java:359) 
10-29 17:52:18.706: E/AndroidRuntime(495): at android.widget.TabHost.setCurrentTab(TabHost.java:344) 
10-29 17:52:18.706: E/AndroidRuntime(495): at android.widget.TabHost.addTab(TabHost.java:216) 
10-29 17:52:18.706: E/AndroidRuntime(495): at com.jjwoundcare.main.CareActivity$TabManager.addTab(CareActivity.java:123) 
10-29 17:52:18.706: E/AndroidRuntime(495): at com.jjwoundcare.main.CareActivity.onCreate(CareActivity.java:35) 
10-29 17:52:18.706: E/AndroidRuntime(495): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
10-29 17:52:18.706: E/AndroidRuntime(495): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) 

問題在哪裏是我的代碼?

回答

1

ListingActivity.java必須通過延伸片段

例如:

public class FirstFragment extends Fragment{ 


    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 



    return inflater.inflate(R.layout.fragment1, container,false); 
    } 


} 

fragment1.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <Button 
     android:id="@+id/button1" 
     style="?android:attr/buttonStyleSmall" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="fregment2" /> 

</LinearLayout>