Android兼容性API的PreferenceFragment替代方案?

前端之家收集整理的这篇文章主要介绍了Android兼容性API的PreferenceFragment替代方案?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
参见英文答案 > Was PreferenceFragment intentionally excluded from the compatibility package?8个
我正在尝试使用Android兼容性软件包v4,使用基于片段的设计替换我的应用程序中的TabActivity.

我遇到的问题是我的TabActivity中的一个选项卡是PreferencesActivity,由于显而易见的原因,我无法从FragmentActivity启动.

所以,既然,PreferenceFragment没有包含在Android兼容包中,我也无法从我的片段活动中启动PreferenceActivity,我该怎么办?

对任何建议开放.

编辑:尝试从我的片段活动打开PreferenceActivity时得到的异常在这里:

FATAL EXCEPTION: main
java.lang.IllegalStateException: Did you forget to call 'public void setup(LocalActivityManager activityGroup)'?
    at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:652)
    at android.widget.TabHost.setCurrentTab(TabHost.java:326)
    at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:132)
    at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:456)
    at android.view.View.performClick(View.java:2485)
    at android.view.View$PerformClick.run(View.java:9080)
    at android.os.Handler.handleCallback(Handler.java:587)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:130)
    at android.app.ActivityThread.main(ActivityThread.java:3683)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:507)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
    at dalvik.system.NativeStart.main(Native Method)

我的FragmentActivity代码如下(大部分内容来自’tabs’的兼容包v4示例):

EDIT2:为了清楚起见,还添加了示例代码

import java.util.HashMap;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TabHost;
import android.widget.TextView;

public class PlayerTabActivity extends FragmentActivity {
    private static final String TAG = PlayerTabActivity.class.getSimpleName();
    TabHost mTabHost;
    TabManager mTabManager;

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

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

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

        //this is my fragment
        mTabManager.addTab(mTabHost.newTabSpec("player").setIndicator(createTabView(this,"Player")),PlayerMainFragment.class,null);

        //this is my activity
        mTabHost.addTab(mTabHost.newTabSpec("settings").setIndicator(createTabView(this,"Settings"))
                .setContent(new Intent(this,PlayerSettingsActivity.class)));

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

    //make the tab look pretty
    private static View createTabView(Context context,String tag) {
        View view = LayoutInflater.from(context).inflate(R.layout.tabs_bg,null);
        TextView tv = (TextView) view.findViewById(R.id.tab_text);
        tv.setText(tag);
        return view;
    }

    @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();
            }
        }
    }
}

解决方法

请参阅v7支持库的 latest revision,该库引入了 PreferenceFragmentCompat.
原文链接:https://www.f2er.com/android/316043.html

猜你在找的Android相关文章