1

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

    }

我的日志猫

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

1 回答 1

0

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

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

于 2012-11-27T21:19:22.917 回答