如何将首选项屏幕添加到 Viewpager?

Posted

技术标签:

【中文标题】如何将首选项屏幕添加到 Viewpager?【英文标题】:How Do I Add Preference Screen to Viewpager? 【发布时间】:2012-11-27 21:11:22 【问题描述】:

我是 android 开发的菜鸟,在 viewpager 中显示首选项屏幕时遇到了一点问题。目前,我的应用程序将 viewpager 的所有页面显示为一个页面,并将首选项屏幕作为背景。然后,当我做手势时,应用程序由于 InflateException 而崩溃。我已经用 SO 问题 here 和 here 研究了潜在原因,但没有一个提供解决我问题的解决方案。我只会使用 Preference 片段,但我正在为 min 8 API 开发。非常感谢任何帮助。

我的代码

public class Options extends PreferenceActivity implements OnClickListener   ,SharedPreferences.OnSharedPreferenceChangeListener

ActionBarSherlock mSherlock = ActionBarSherlock.wrap(this);
View view;

@Override
public void onCreate(Bundle savedInstanceState) 
    setTheme(R.style.Theme_Sherlock);
    super.onCreate(savedInstanceState);

   addPreferencesFromResource(R.xml.prefs);
   PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this);

    mSherlock.setContentView(R.layout.options);

MyPagerAdapter adapter = new MyPagerAdapter(this);
ViewPager myPager = (ViewPager) findViewById(R.id.viewpager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(2);
TitlePageIndicator indicator = (TitlePageIndicator)findViewById( R.id.indicator ); 
indicator.setViewPager( myPager );
indicator.setOnPageChangeListener(new ViewPager.OnPageChangeListener() 
    @Override
    public void onPageSelected(int position) 

          if(position == 0 ||position == 1||position == 2||position == 4)

          
    

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) 
    

    @Override
    public void onPageScrollStateChanged(int state) 
    
);  


private class MyPagerAdapter extends PagerAdapter 

    private String[] titles = new String[] "HELP",  "PREFERENCES", "ABOUT" ;
private final Context context;
private int[] scrollPosition = new int[titles.length];

public MyPagerAdapter( Context context )

    this.context = context;
    for ( int i = 0; i < titles.length; i++ )
    
        scrollPosition[i] = 0;
    


@Override
public String getPageTitle( int position )

    return titles[position];


@Override
public int getCount()

    return titles.length;




    public Object instantiateItem(View collection, int position) 

        LayoutInflater inflater = (LayoutInflater) collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);


        int resId = 0;
        switch (position) 
        case 2:
            resId = R.layout.help;              
            view = inflater.inflate(resId, null);
            LinearLayout layout0=(LinearLayout)view.findViewById(R.id.helplayout);
            ((ViewPager) collection).addView(view, 0);
            return view;

        case 0:
            resId = R.xml.prefs;
            view = inflater.inflate(resId, null);
            LinearLayout layout1=(LinearLayout)view.findViewById(R.id.RLcoins);


            ((ViewPager) collection).addView(view, 0);
            return view;

        case 1:
            resId = R.layout.about;
            view = inflater.inflate(resId, null);
            LinearLayout layout2=(LinearLayout)view.findViewById(R.id.LLtwitter);


            ((ViewPager) collection).addView(view, 0);
            return view;



        
        return resId;

    

我的 LOGCAT

11-27 16:00:50.039: E/AndroidRuntime(3993): FATAL EXCEPTION: main
11-27 16:00:50.039: E/AndroidRuntime(3993): android.view.InflateException: Binary XML file line #2: Error inflating class PreferenceScreen
11-27 16:00:50.039: E/AndroidRuntime(3993):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:581)
11-27 16:00:50.039: E/AndroidRuntime(3993):     at android.view.LayoutInflater.inflate(LayoutInflater.java:386)
11-27 16:00:50.039: E/AndroidRuntime(3993):     at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
11-27 16:00:50.039: E/AndroidRuntime(3993):     at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
11-27 16:00:50.039: E/AndroidRuntime(3993):     at com.bryanjrichardson.GSCC.Options$MyPagerAdapter.instantiateItem(Options.java:130)
11-27 16:00:50.039: E/AndroidRuntime(3993):     at android.support.v4.view.PagerAdapter.instantiateItem(PagerAdapter.java:110)
11-27 16:00:50.039: E/AndroidRuntime(3993):     at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:692)
11-27 16:00:50.039: E/AndroidRuntime(3993):     at android.support.v4.view.ViewPager.populate(ViewPager.java:849)
11-27 16:00:50.039: E/AndroidRuntime(3993):     at android.support.v4.view.ViewPager.populate(ViewPager.java:772)
11-27 16:00:50.039: E/AndroidRuntime(3993):     at android.support.v4.view.ViewPager.completeScroll(ViewPager.java:1539)
11-27 16:00:50.039: E/AndroidRuntime(3993):     at android.support.v4.view.ViewPager.computeScroll(ViewPager.java:1422)
11-27 16:00:50.039: E/AndroidRuntime(3993):     at android.view.ViewGroup.drawChild(ViewGroup.java:1562)
11-27 16:00:50.039: E/AndroidRuntime(3993):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
11-27 16:00:50.039: E/AndroidRuntime(3993):     at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
11-27 16:00:50.039: E/AndroidRuntime(3993):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
11-27 16:00:50.039: E/AndroidRuntime(3993):     at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
11-27 16:00:50.039: E/AndroidRuntime(3993):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
11-27 16:00:50.039: E/AndroidRuntime(3993):     at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
11-27 16:00:50.039: E/AndroidRuntime(3993):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
11-27 16:00:50.039: E/AndroidRuntime(3993):     at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
11-27 16:00:50.039: E/AndroidRuntime(3993):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
11-27 16:00:50.039: E/AndroidRuntime(3993):     at android.view.View.draw(View.java:6883)
11-27 16:00:50.039: E/AndroidRuntime(3993):     at android.widget.FrameLayout.draw(FrameLayout.java:357)
11-27 16:00:50.039: E/AndroidRuntime(3993):     at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2116)
11-27 16:00:50.039: E/AndroidRuntime(3993):     at android.view.ViewRoot.draw(ViewRoot.java:1565)
11-27 16:00:50.039: E/AndroidRuntime(3993):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1301)
11-27 16:00:50.039: E/AndroidRuntime(3993):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1914)
11-27 16:00:50.039: E/AndroidRuntime(3993):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-27 16:00:50.039: E/AndroidRuntime(3993):     at android.os.Looper.loop(Looper.java:130)
11-27 16:00:50.039: E/AndroidRuntime(3993):     at android.app.ActivityThread.main(ActivityThread.java:3859)
11-27 16:00:50.039: E/AndroidRuntime(3993):     at java.lang.reflect.Method.invokeNative(Native Method)
11-27 16:00:50.039: E/AndroidRuntime(3993):     at java.lang.reflect.Method.invoke(Method.java:507)
11-27 16:00:50.039: E/AndroidRuntime(3993):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-27 16:00:50.039: E/AndroidRuntime(3993):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-27 16:00:50.039: E/AndroidRuntime(3993):     at dalvik.system.NativeStart.main(Native Method)
11-27 16:00:50.039: E/AndroidRuntime(3993): Caused by: java.lang.ClassNotFoundException: android.view.PreferenceScreen in loader dalvik.system.PathClassLoader[/system/framework/com.google.android.maps.jar:/data/app/com.bryan.GSCC-1.apk]
11-27 16:00:50.039: E/AndroidRuntime(3993):     at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
11-27 16:00:50.039: E/AndroidRuntime(3993):     at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
11-27 16:00:50.039: E/AndroidRuntime(3993):     at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
11-27 16:00:50.039: E/AndroidRuntime(3993):     at android.view.LayoutInflater.createView(LayoutInflater.java:471)
11-27 16:00:50.039: E/AndroidRuntime(3993):     at android.view.LayoutInflater.onCreateView(LayoutInflater.java:549)
11-27 16:00:50.039: E/AndroidRuntime(3993):     at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
11-27 16:00:50.039: E/AndroidRuntime(3993):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)

【问题讨论】:

【参考方案1】:

您应该使用片段作为帮助/关于,并使用PreferenceFragment 作为首选项。

这是 PreferenceFragment 的教程 http://android-er.blogspot.com.ar/2012/07/example-of-using-preferencefragment.html

【讨论】:

感谢您的及时回复。 PreferenceFragment 需要 Api 11,我正在为 min API 8 构建。你知道解决方法吗? 很遗憾地说我不知道​​任何解决方法,而且看起来也不太有希望:***.com/questions/9783368/… 和 ***.com/questions/5501431/…

以上是关于如何将首选项屏幕添加到 Viewpager?的主要内容,如果未能解决你的问题,请参考以下文章

如何将边距/填充添加到首选项屏幕

如何从(和替换)TabLayout 和 ViewPager 移动到新片段?

根据存储在共享首选项中的记住我状态导航到屏幕

从根目录为所有用户设置首选项文件

windows>首选项中没有服务器

如何使用 tabLayout 在 ViewPager 中显示片段?