抛出java.lang.ClassNotFoundException:android.view.fragment [英] java.lang.ClassNotFoundException: android.view.fragment

查看:451
本文介绍了抛出java.lang.ClassNotFoundException:android.view.fragment的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个活动(MainActivity和WelcomeActividy)无论是从 android.support.v4.app.FragmentActivity

扩展的应用程序

WelcomeActivity使用ViewFlipper显示片段(第一步,第二步,...,StepN)在一个步骤使用的片段列表(CategoryStarredFragment)的WelcomeActivity不会有问题。

MainActivity使用tabhost显示片段也都工作正常,但当尝试包括一个相同的CategoryStarredFragment(在WelcomeActivity正常工作),我收到了异常

  04-11 15:32:28.197:E / AndroidRuntime(16124):由:抛出java.lang.ClassNotFoundException:android.view.fragment装载机dalvik.system.PathClassLoader [ /data/app/com.package.apk]
 

我在想,是在产生问题的tabhost实施

这里的 MainActivity.java

 公共类MainActivity扩展FragmentActivity实现OnTabChangeListener {


    私人TabHost mTab​​Host;

    私人SparseArray<类<>> mSparseFragments =新SparseArray<类<>>(){{
        把(R.id.tab_home,HomeFragment.class);
                //其他选项卡
        把(R.id.tab_settings,SettingsFragment.class);
    }};

    私人SparseArray<字符串> mSparseTags =新SparseArray<字符串>(){{
        把(R.id.tab_home,家);
                //其他选项卡
        把(R.id.tab_settings,设置);
    }};

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);

        initTabs();
    }

    私人无效initTabs(){
        mTabHost =(TabHost)findViewById(android.R.id.tabhost);
        mTabHost.setup();
        mTabHost.setOnTabChangedListener(本);

        mTabHost.addTab(buildTabSpec(家,R.string.title_tab_home,R.layout.regular_home_fragment));
                //其他选项卡
        mTabHost.addTab(buildTabSpec(设置,R.string.title_tab_settings,R.layout.regular_settings_fragment));
    }
    @覆盖
    公共无效onTabChanged(字符串tabId){
        Log.i(TAG,标签更改为:+ tabId);

        最终FragmentManager FM = getSupportFragmentManager();
        最终FragmentTransaction英尺= fm.beginTransaction();
        片段片段
        INT电流= 0;


        的for(int i = R.id.tab_home; I< = R.id.tab_settings;我++){
            如果(mSparseTags.get(ⅰ).equals(tabId)){
                电流= I;
            }否则如果(空!=(片段= fm.findFragmentByTag(mSparseTags.get(ⅰ)))){
                ft.detach(片段);
            }
        }


        如果(空==(片段= fm.findFragmentByTag(tabId))){
            尝试 {
                ft.add(电流,(片段)mSparseFragments.get(电流).newInstance(),tabId);
            }赶上(InstantiationException E){
                e.printStackTrace();
            }赶上(IllegalAccessException E){
                e.printStackTrace();
            }
        } 其他 {
            ft.attach(片段);
        }

        ft.commit();
    }

    私营则tabspec buildTabSpec(字符串标记,诠释labelId,诠释的ViewID){
        最后查看指示灯= LayoutInflater.from(getApplicationContext())
                .inflate(R.layout.tab,(ViewGroup中)findViewById(android.R.id.tabs),FALSE);
        ((TextView中)indicator.findViewById(R.id.text))的setText(labelId)。
        返回mTabHost.newTabSpec(标签)
                .setIndicator(指标)
                .setContent(新TabContent(getApplicationContext(),的ViewID));
    }

    公共类TabContent实现TabContentFactory {

        私人语境mContext;
        私人诠释mViewId;

        公共TabContent(上下文的背景下,诠释的ViewID){
            mContext =背景;
            mViewId =的ViewID;
        }

        @覆盖
        公共查看createTabContent(字符串变量){
            Log.i(TAG,通胀选项卡内容+标签);
            查看查看= LayoutInflater.from(mContext).inflate(mViewId,NULL);
            返回查看;
        }

    }

}
 

下面是MainActivity布局

 < RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:方向=垂直
    工具:上下文=MainActivity。>

    < TabHost
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =FILL_PARENT
        机器人:ID =@机器人:ID / tabhost>

        <的LinearLayout
            机器人:方向=垂直
            机器人:layout_height =FILL_PARENT
            机器人:layout_width =FILL_PARENT>

            < TabWidget
                机器人:ID =@机器人:ID /标签
                机器人:layout_width =FILL_PARENT
                机器人:layout_height =WRAP_CONTENT
                机器人:背景=@可绘制/ ab_stacked_solid_mainbar>
            < / TabWidget>

            <的FrameLayout
                机器人:ID =@机器人:ID / tabcontent
                机器人:layout_height =FILL_PARENT
                机器人:layout_width =FILL_PARENT>
                <的FrameLayout
                    机器人:ID =@ + ID / tab_home
                    机器人:layout_height =FILL_PARENT
                    机器人:layout_width =FILL_PARENT/>
                <! - 其它标签 - >
                <的FrameLayout
                    机器人:ID =@ + ID / tab_settings
                    机器人:layout_height =FILL_PARENT
                    机器人:layout_width =FILL_PARENT/>
             < /的FrameLayout>

        < / LinearLayout中>
    < / TabHost>
< / RelativeLayout的>
 

下面是布局的第一个标签

 < XML版本=1.0编码=UTF-8&GT?;
<的LinearLayout
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:方向=垂直>

    <的TextView
        机器人:ID =@ + ID /标题
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:重力=center_horizo​​ntal
        机器人:文本=@字符串/ title_tab_home
        机器人:TEXTSIZE =64sp/>

    <片段
            机器人:ID =@ + ID / category_list
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_margin =5DP
            机器人:layout_marginTop =18dp
            类=com.package.CategoryStarredFragment/>

< / LinearLayout中>
 

解决方案

<打击>您正试图加载包含布局的&LT;片断&gt; 标签,上API级别10或更低的设备,从活动,而不是 FragmentActivity

实际上,在这种情况下,这是一个有点更微妙。您正在尝试使用 LayoutInflater.from()。这是好的,但你不能在API级别使用10或更低的设备间preT布局资源文件&LT;片断&gt; 标记他们,<$从那时候有C $ C> LayoutInflater 不知道什么是&LT;片断&gt; 标签了。你需要使用 getLayoutInflater返​​回 LayoutInflater 实例() FragmentActivity ,以除preT &LT;片断&gt; 标签

I have an application with Two Activities (MainActivity and WelcomeActividy) both extends from android.support.v4.app.FragmentActivity

WelcomeActivity uses a ViewFlipper for display Fragments (Step1, Step2, ..., StepN) in one Step uses a fragment list (CategoryStarredFragment) the WelcomeActivity no have problems

MainActivity uses a tabhost for display fragment too all work fine but when try to include a the same CategoryStarredFragment (works fine in WelcomeActivity) i get a exception

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]

I'm thinking that is the implementation of the tabhost which is generating the problem

here's 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;
        }

    }   

}

Here is MainActivity layout

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

Here is layout first tab

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

解决方案

You are attempting to load a layout containing a <fragment> tag, on an API Level 10 or lower device, from an Activity instead of a FragmentActivity.

Actually, in this case, it is a bit more subtle. You are attempting to use LayoutInflater.from(). This is fine, but you cannot use it on API Level 10 or lower devices to interpret layout resource files with <fragment> tags in them, as LayoutInflater from back then had no idea what a <fragment> tag was. You need to use the LayoutInflater instance returned by getLayoutInflater() on your FragmentActivity, in order to interpret <fragment> tags.

这篇关于抛出java.lang.ClassNotFoundException:android.view.fragment的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆