java.lang.ClassNotFoundException:android.view.fragment

rkm*_*max 0 android android-fragments

我有一个带有两个活动的应用程序(MainActivity和WelcomeActividy)都延伸自 android.support.v4.app.FragmentActivity

WelcomeActivity使用ViewFlipper显示片段(Step1,Step2,...,StepN),一步使用片段列表(CategoryStarredFragment),WelcomeActivity没有问题

MainActivity使用tabhost进行显示片段,所有工作正常但是当尝试包含相同的CategoryStarredFragment(在WelcomeActivity中正常工作)时,我得到一个例外

04-11 15:32:28.197: E/AndroidRuntime(16124): Caused by: java.lang.ClassNotFoundException: android.view.fragment in loader dalvik.system.PathClassLoader[/data/app/com.package.apk]
Run Code Online (Sandbox Code Playgroud)

我想这是生成问题的tabhost的实现

这里的 MainActivity.java

public class MainActivity extends FragmentActivity implements OnTabChangeListener {


    private TabHost mTabHost;

    private SparseArray<Class<?>> mSparseFragments = new SparseArray<Class<?>>(){{
        put(R.id.tab_home, HomeFragment.class);
                // other tabs
        put(R.id.tab_settings, SettingsFragment.class);
    }};

    private SparseArray<String> mSparseTags = new SparseArray<String>(){{
        put(R.id.tab_home, "home");
                // other tabs
        put(R.id.tab_settings, "settings");
    }};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initTabs();         
    }

    private void initTabs() {
        mTabHost = (TabHost)findViewById(android.R.id.tabhost);
        mTabHost.setup();
        mTabHost.setOnTabChangedListener(this);

        mTabHost.addTab(buildTabSpec("home", R.string.title_tab_home, R.layout.regular_home_fragment));
                // other tabs
        mTabHost.addTab(buildTabSpec("settings", R.string.title_tab_settings, R.layout.regular_settings_fragment));
    }
    @Override
    public void onTabChanged(String tabId) {        
        Log.i(TAG, "Tab changed to: " + tabId);

        final FragmentManager fm = getSupportFragmentManager();
        final FragmentTransaction ft = fm.beginTransaction();
        Fragment fragment;
        int current = 0;


        for (int i = R.id.tab_home; i <= R.id.tab_settings; i++) {              
            if(mSparseTags.get(i).equals(tabId)) {
                current = i;
            } else if(null != (fragment = fm.findFragmentByTag(mSparseTags.get(i)))) {
                ft.detach(fragment);
            }
        }


        if(null == (fragment = fm.findFragmentByTag(tabId))) {
            try {
                ft.add(current, (Fragment) mSparseFragments.get(current).newInstance(), tabId);             
            } catch (InstantiationException e) {        
                e.printStackTrace();
            } catch (IllegalAccessException e) {        
                e.printStackTrace();
            }
        } else {
            ft.attach(fragment);
        }

        ft.commit();
    }

    private TabSpec buildTabSpec(String tag, int labelId, int viewId) {     
        final View indicator = LayoutInflater.from(getApplicationContext())
                .inflate(R.layout.tab, (ViewGroup) findViewById(android.R.id.tabs), false);
        ((TextView)indicator.findViewById(R.id.text)).setText(labelId);
        return mTabHost.newTabSpec(tag)
                .setIndicator(indicator)
                .setContent(new TabContent(getApplicationContext(), viewId));
    }

    public class TabContent implements TabContentFactory {

        private Context mContext;
        private int mViewId;

        public TabContent(Context context, int viewId) {
            mContext = context;
            mViewId = viewId;
        }

        @Override
        public View createTabContent(String tag) {
            Log.i(TAG, "Inflation tab content " + tag);
            View view = LayoutInflater.from(mContext).inflate(mViewId, null);           
            return view;
        }

    }   

}
Run Code Online (Sandbox Code Playgroud)

这是MainActivity布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <TabHost
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@android:id/tabhost">

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

            <TabWidget 
                android:id="@android:id/tabs"            
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/ab_stacked_solid_mainbar">            
            </TabWidget>

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_height="fill_parent"
                android:layout_width="fill_parent">             
                <FrameLayout
                    android:id="@+id/tab_home"
                    android:layout_height="fill_parent"
                    android:layout_width="fill_parent"/>
                <!-- Other tabs --> 
                <FrameLayout 
                    android:id="@+id/tab_settings"
                    android:layout_height="fill_parent"
                    android:layout_width="fill_parent"/>            
             </FrameLayout>

        </LinearLayout>
    </TabHost>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

这是布局第一个选项卡

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

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="@string/title_tab_home"
        android:textSize="64sp"/>

    <fragment
            android:id="@+id/category_list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:layout_marginTop="18dp"
            class="com.package.CategoryStarredFragment" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

Com*_*are 8

您正在尝试从<fragment>API级别10或更低版本的设备加载包含标记的布局,Activity而不是从FragmentActivity.

实际上,在这种情况下,它有点微妙.你正在尝试使用LayoutInflater.from().这很好,但你不能在API Level 10或更低版本的设备上使用它来解释带有<fragment>标签的布局资源文件,因为LayoutInflater从那时起就不知道<fragment>标签是什么.你需要使用LayoutInflater返回的情况下getLayoutInflater()你的FragmentActivity,以此来诠释<fragment>标签.