使用Sherlock ActionBar,Fragment Tab,ViewPager,PreferenceActivity

时间:2013-03-07 18:36:26

标签: java android android-fragments actionbarsherlock preferences

所以我有这个偏好激活,似乎无法弄清楚如何使这项工作或是否可以完成。我的应用程序使用Sherlock Actionbar,View-pager和Fragment Tabs。我的所有标签都不是最后一个,这是我的PreferenceActivity。

 public class Prefs extends SherlockPreferenceActivity implements
    OnSharedPreferenceChangeListener {

  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    addPreferencesFromResource(R.xml.settings);

    Preference customPref = (Preference) findPreference("clearcache");
    customPref
            .setOnPreferenceClickListener(new OnPreferenceClickListener() {

                public boolean onPreferenceClick(Preference preference) {
                    Toast.makeText(getBaseContext(), "Cache Cleared",
                            Toast.LENGTH_LONG).show();
                    Webview.web.clearCache(true);
                    return false;
                }
            });

    Preference customerPref1 = (Preference) findPreference("intent1");
    customerPref1
            .setOnPreferenceClickListener(new OnPreferenceClickListener() {

                public boolean onPreferenceClick(Preference preference) {
                    Intent myIntent = new Intent(Prefs.this, Intent1.class);
                    Prefs.this.startActivity(myIntent);
                    return true;
                }
            });

这就是我的首选项活动设置。现在,选择此选项卡时,主要问题如下。

logcat的

 03-07 10:55:09.553: E/AndroidRuntime(3968): FATAL EXCEPTION: main
 03-07 10:55:09.553: E/AndroidRuntime(3968): java.lang.ClassCastException:      com.application.example.EditPreferences cannot be cast to android.support.v4.app.Fragment
 03-07 10:55:09.553: E/AndroidRuntime(3968):    at android.support.v4.app.Fragment.instantiate(Fragment.java:394)
 03-07 10:55:09.553: E/AndroidRuntime(3968):    at com.application.example.ABSTabsViewPagerActivity$TabsAdapter.getItem(ABSTabsViewPagerActivity.java:106)
 03-07 10:55:09.553: E/AndroidRuntime(3968):    at android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:97)
 03-07 10:55:09.553: E/AndroidRuntime(3968):    at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:800)
 03-07 10:55:09.553: E/AndroidRuntime(3968):    at android.support.v4.view.ViewPager.populate(ViewPager.java:991)
 03-07 10:55:09.553: E/AndroidRuntime(3968):    at      android.support.v4.view.ViewPager.populate(ViewPager.java:880)
 03-07 10:55:09.553: E/AndroidRuntime(3968):    at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1374)
 03-07 10:55:09.553: E/AndroidRuntime(3968):    at android.view.View.measure(View.java:15518)
 03-07 10:55:09.553: E/AndroidRuntime(3968):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
 03-07 10:55:09.553: E/AndroidRuntime(3968):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
 03-07 10:55:09.553: E/AndroidRuntime(3968):    at android.view.View.measure(View.java:15518)
 03-07 10:55:09.553: E/AndroidRuntime(3968):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
 03-07 10:55:09.553: E/AndroidRuntime(3968):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
 03-07 10:55:09.553: E/AndroidRuntime(3968):    at android.view.View.measure(View.java:15518)
 03-07 10:55:09.553: E/AndroidRuntime(3968):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
 03-07 10:55:09.553: E/AndroidRuntime(3968):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
 03-07 10:55:09.553: E/AndroidRuntime(3968):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
 03-07 10:55:09.553: E/AndroidRuntime(3968):    at android.view.View.measure(View.java:15518)
 03-07 10:55:09.553: E/AndroidRuntime(3968):    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
 03-07 10:55:09.553: E/AndroidRuntime(3968):    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
 03-07 10:55:09.553: E/AndroidRuntime(3968):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
 03-07 10:55:09.553: E/AndroidRuntime(3968):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
 03-07 10:55:09.553: E/AndroidRuntime(3968):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
 03-07 10:55:09.553: E/AndroidRuntime(3968):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
 03-07 10:55:09.553: E/AndroidRuntime(3968):    at android.view.Choreographer.doCallbacks(Choreographer.java:562)
 03-07 10:55:09.553: E/AndroidRuntime(3968):    at android.view.Choreographer.doFrame(Choreographer.java:532)
 03-07 10:55:09.553: E/AndroidRuntime(3968):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
 03-07 10:55:09.553: E/AndroidRuntime(3968):    at android.os.Handler.handleCallback(Handler.java:725)
 03-07 10:55:09.553: E/AndroidRuntime(3968):    at android.os.Handler.dispatchMessage(Handler.java:92)
 03-07 10:55:09.553: E/AndroidRuntime(3968):    at android.os.Looper.loop(Looper.java:137)
 03-07 10:55:09.553: E/AndroidRuntime(3968):    at android.app.ActivityThread.main(ActivityThread.java:5204)
 03-07 10:55:09.553: E/AndroidRuntime(3968):    at java.lang.reflect.Method.invokeNative(Native Method)
 03-07 10:55:09.553: E/AndroidRuntime(3968):    at java.lang.reflect.Method.invoke(Method.java:511)
 03-07 10:55:09.553: E/AndroidRuntime(3968):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:799)
 03-07 10:55:09.553: E/AndroidRuntime(3968):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
 03-07 10:55:09.553: E/AndroidRuntime(3968):    at dalvik.system.NativeStart.main(Native Method)

任何想法,示例,其他建议,甚至其他能够进行设置页面的示例,我的大多数设置只是对其他活动的意图。

1 个答案:

答案 0 :(得分:1)

您正在尝试加载活动来代替片段。这就是它给出例外的原因。如果您的设置是简单的意图,则不需要将它们放在首选项活动中。只需扩展SherlockFragment并将代码放在那里。