用Fragment替换TabActivity:填充类片段时出错

时间:2012-02-09 09:27:32

标签: android fragment

我看到我在这个论坛上提出的类似问题的数量判断我有一个常见的错误,但我没有找到任何答案。

我尝试使用片段来替换已弃用的tabActivity。

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

public class MediaActivity extends FragmentActivity{

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

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

    <fragment
        android:name="fr.partisocialite.psa.activity.TabsFragment"
        android:id="@+id/tabs_fragment"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</LinearLayout>

我也尝试用类替换android:name

这是我的logCat:

02-09 10:07:48.738: E/AndroidRuntime(1713): FATAL EXCEPTION: main
> 02-09 10:07:48.738: E/AndroidRuntime(1713):
> java.lang.RuntimeException: Unable to start activity
> ComponentInfo{fr.partisocialiste.psa/fr.partisocialiste.psa.activity.MediaActivity}:
> android.view.InflateException: Binary XML file line #7: Error
> inflating class fragment 02-09 10:07:48.738: E/AndroidRuntime(1713):
>   at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
> 02-09 10:07:48.738: E/AndroidRuntime(1713):   at
> android.app.ActivityThread.startActivityNow(ActivityThread.java:1487)
> 02-09 10:07:48.738: E/AndroidRuntime(1713):   at
> android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
> 02-09 10:07:48.738: E/AndroidRuntime(1713):   at
> android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
> 02-09 10:07:48.738: E/AndroidRuntime(1713):   at
> android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:654)
> 02-09 10:07:48.738: E/AndroidRuntime(1713):   at
> android.widget.TabHost.setCurrentTab(TabHost.java:326) 02-09
> 10:07:48.738: E/AndroidRuntime(1713):     at
> android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:132) 02-09
> 10:07:48.738: E/AndroidRuntime(1713):     at
> android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:456)
> 02-09 10:07:48.738: E/AndroidRuntime(1713):   at
> android.view.View.performClick(View.java:2485) 02-09 10:07:48.738:
> E/AndroidRuntime(1713):   at
> android.view.View$PerformClick.run(View.java:9080) 02-09 10:07:48.738:
> E/AndroidRuntime(1713):   at
> android.os.Handler.handleCallback(Handler.java:587) 02-09
> 10:07:48.738: E/AndroidRuntime(1713):     at
> android.os.Handler.dispatchMessage(Handler.java:92) 02-09
> 10:07:48.738: E/AndroidRuntime(1713):     at
> android.os.Looper.loop(Looper.java:130) 02-09 10:07:48.738:
> E/AndroidRuntime(1713):   at
> android.app.ActivityThread.main(ActivityThread.java:3683) 02-09
> 10:07:48.738: E/AndroidRuntime(1713):     at
> java.lang.reflect.Method.invokeNative(Native Method) 02-09
> 10:07:48.738: E/AndroidRuntime(1713):     at
> java.lang.reflect.Method.invoke(Method.java:507) 02-09 10:07:48.738:
> E/AndroidRuntime(1713):   at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
> 02-09 10:07:48.738: E/AndroidRuntime(1713):   at
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 02-09
> 10:07:48.738: E/AndroidRuntime(1713):     at
> dalvik.system.NativeStart.main(Native Method) 02-09 10:07:48.738:
> E/AndroidRuntime(1713): Caused by: android.view.InflateException:
> Binary XML file line #7: Error inflating class fragment 02-09
> 10:07:48.738: E/AndroidRuntime(1713):     at
> android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:587)
> 02-09 10:07:48.738: E/AndroidRuntime(1713):   at
> android.view.LayoutInflater.rInflate(LayoutInflater.java:623) 02-09
> 10:07:48.738: E/AndroidRuntime(1713):     at
> android.view.LayoutInflater.inflate(LayoutInflater.java:408) 02-09
> 10:07:48.738: E/AndroidRuntime(1713):     at
> android.view.LayoutInflater.inflate(LayoutInflater.java:320) 02-09
> 10:07:48.738: E/AndroidRuntime(1713):     at
> android.view.LayoutInflater.inflate(LayoutInflater.java:276) 02-09
> 10:07:48.738: E/AndroidRuntime(1713):     at
> com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
> 02-09 10:07:48.738: E/AndroidRuntime(1713):   at
> android.app.Activity.setContentView(Activity.java:1657) 02-09
> 10:07:48.738: E/AndroidRuntime(1713):     at
> fr.partisocialiste.psa.activity.MediaActivity.onCreate(MediaActivity.java:14)
> 02-09 10:07:48.738: E/AndroidRuntime(1713):   at
> android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
> 02-09 10:07:48.738: E/AndroidRuntime(1713):   at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
> 02-09 10:07:48.738: E/AndroidRuntime(1713):   ... 18 more 02-09
> 10:07:48.738: E/AndroidRuntime(1713): Caused by:
> android.support.v4.app.Fragment$InstantiationException: Unable to
> instantiate fragment fr.partisocialite.psa.activity.TabsFragment: make
> sure class name exists, is public, and has an empty constructor that
> is public 02-09 10:07:48.738: E/AndroidRuntime(1713):     at
> android.support.v4.app.Fragment.instantiate(Fragment.java:332) 02-09
> 10:07:48.738: E/AndroidRuntime(1713):     at
> android.support.v4.app.Fragment.instantiate(Fragment.java:300) 02-09
> 10:07:48.738: E/AndroidRuntime(1713):     at
> android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:248)
> 02-09 10:07:48.738: E/AndroidRuntime(1713):   at
> android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
> 02-09 10:07:48.738: E/AndroidRuntime(1713):   ... 27 more 02-09
> 10:07:48.738: E/AndroidRuntime(1713): Caused by:
> java.lang.ClassNotFoundException:
> fr.partisocialite.psa.activity.TabsFragment in loader
> dalvik.system.PathClassLoader[/system/framework/com.google.android.maps.jar:/data/app/fr.partisocialiste.psa-1.apk]
> 02-09 10:07:48.738: E/AndroidRuntime(1713):   at
> dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
> 02-09 10:07:48.738: E/AndroidRuntime(1713):   at
> java.lang.ClassLoader.loadClass(ClassLoader.java:551) 02-09
> 10:07:48.738: E/AndroidRuntime(1713):     at
> java.lang.ClassLoader.loadClass(ClassLoader.java:511) 02-09
> 10:07:48.738: E/AndroidRuntime(1713):     at
> android.support.v4.app.Fragment.instantiate(Fragment.java:322) 02-09
> 10:07:48.738: E/AndroidRuntime(1713):     ... 30 more 02-09 10:07:48.804:
> I/global(1713): Loaded time zone names for en_US in 1110ms. 02-09
> 10:07:49.957: D/dalvikvm(1713): GC_CONCURRENT freed 908K, 51% free
> 3705K/7431K, external 2360K/3860K, paused 2ms+2ms 02-09 10:07:50.242:
> I/global(1713): Loaded time zone names for en_US in 1147ms. 02-09
> 10:07:50.410: D/dalvikvm(1713): GC_FOR_MALLOC freed 598K, 51% free
> 3698K/7431K, external 2360K/3860K, paused 38ms 02-09 10:07:50.574:
> D/dalvikvm(1713): GC_FOR_MALLOC freed 164K, 50% free 3779K/7431K,
> external 2360K/3860K, paused 51ms 02-09 10:07:50.593:
> D/dalvikvm(1713): GC_FOR_MALLOC freed 243K, 50% free 3773K/7431K,
> external 2360K/3860K, paused 15ms 02-09 10:07:50.609:
> D/dalvikvm(1713): GC_FOR_MALLOC freed <1K, 47% free 4010K/7431K,
> external 2360K/3860K, paused 16ms 02-09 10:07:50.699:
> D/dalvikvm(1713): GC_CONCURRENT freed 1194K, 49% free 3982K/7687K,
> external 2360K/3860K, paused 2ms+3ms

非常感谢,我没有成功让我的logCat看起来更好,对不起。

这是我的班级TabsFragment,

&LT;&LT;&LT;&LT;&LT;

package fr.partisocialiste.psa.activity;

import fr.partisocialiste.psa.R;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;

public class TabsFragment extends Fragment implements OnTabChangeListener{

    private static final String TAG = "FragmentTabs";
    public static final String TAB_HOLL = "Hollande";
    public static final String TAB_PS = "PS";

    private View mRoot;
    private TabHost mTabHost;
    private int mCurrentTab;

    public TabsFragment(){
        super();
    }


    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        mRoot = inflater.inflate(R.layout.tabs_fragment, null);
        mTabHost = (TabHost) mRoot.findViewById(android.R.id.tabhost);
        setupTabs();
        return mRoot;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setRetainInstance(true);

        mTabHost.setOnTabChangedListener(this);
        mTabHost.setCurrentTab(mCurrentTab);
        // manually start loading hollande
        updateTab(TAB_HOLL, R.id.tab_hollande);
    }

    private void setupTabs() {
        mTabHost.setup(); // important!
        mTabHost.addTab(newTab(TAB_HOLL, R.string.tab_hollande, R.id.tab_hollande));
        mTabHost.addTab(newTab(TAB_PS, R.string.tab_ps, R.id.tab_ps));
    }

    private TabSpec newTab(String tag, int labelId, int tabContentId) {
        Log.d(TAG, "buildTab(): tag=" + tag);

        View indicator = LayoutInflater.from(getActivity()).inflate(
                R.layout.tab,
                (ViewGroup) mRoot.findViewById(android.R.id.tabs), false);
        ((TextView) indicator.findViewById(R.id.text)).setText(labelId);

        TabSpec tabSpec = mTabHost.newTabSpec(tag);
        tabSpec.setIndicator(indicator);
        tabSpec.setContent(tabContentId);
        return tabSpec;
    }

    @Override
    public void onTabChanged(String tabId) {
        if (TAB_HOLL.equals(tabId)) {
            updateTab(tabId, R.id.tab_hollande);
            mCurrentTab = 0;
            return;
        }
        if (TAB_PS.equals(tabId)) {
            updateTab(tabId, R.id.tab_ps);
            mCurrentTab = 1;
            return;
        }

    }

    private void updateTab(String tabId, int placeholder) {
        FragmentManager fm = getFragmentManager();
        if (fm.findFragmentByTag(tabId) == null) {
            if(TAB_HOLL.equals(tabId)){
//              fm.beginTransaction()
//              .replace(placeholder, new MyListFragment(tabId), tabId)
//              .commit();
            }
            else if(TAB_PS.equals(tabId)){
//              fm.beginTransaction()
//              .replace(placeholder, new MyListFragment(tabId), tabId)
//              .commit();
            }
        }
    }

}

3 个答案:

答案 0 :(得分:0)

问题如下

android.support.v4.app.Fragment $ InstantiationException:无法实例化片段fr.partisocialite.psa.activity.TabsFragment:make 确定类名存在,是公共的,并且具有空构造函数 公共

检查上面并相应地进行更正。

答案 1 :(得分:0)

我通常使用inflater.inflate(R.layout.tabs_fragment,container,false);但问题是:

<fragment
        android:name="fr.partisocialite.psa.activity.TabsFragment"...

你想要

<fragment
       class="fr.partisocialite.psa.activity.TabsFragment"...

答案 2 :(得分:0)

在出错之前检查您是否有这样的警告:

05-16 10:08:00.033: W/dalvikvm(1695): Unable to resolve superclass of Lmy/fragment/test/FragmentTestActivity; (7)
05-16 10:08:00.076: W/dalvikvm(1695): Link of class 'Lmy/fragment/test/FragmentTestActivity;' failed

如果是,请尝试:

配置Java构建路径 - &gt;订单和导出选项卡 - &gt;设置复选框android-support-v4.jar

(可能需要首先删除现有的)