如何向Viewpager添加首选项屏幕?

时间:2012-11-27 21:11:22

标签: android exception view sharedpreferences android-viewpager

我是Android开发的小伙伴,我在viewpager中显示首选项屏幕时遇到了一些问题。目前,我的应用程序显示viewpager的所有页面混乱为一个页面,首选项屏幕作为背景。然后,当我做手势时,应用程序因InflateException而崩溃。我已经研究了SO问题herehere的潜在原因,但没有一个提供纠正我的问题的解决方案。我只会使用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 个答案:

答案 0 :(得分:0)

您应该使用片段获取帮助/约,并使用PreferenceFragment作为首选项。

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