PreferenceFragment支持所有API问题

时间:2013-05-05 20:57:00

标签: java android preferenceactivity

这是第一次使用偏好片段和偏好活动 取决于这篇文章的接受答案:PreferenceActivity Android 4.0 and earlier

我应用下面的代码sa并在姜饼运行设备上进行测试,它的工作正常但是当在果冻豆运行设备上测试应用程序强制直接关闭时,我知道我的代码有些问题或者我错过了一些事情,< / p>

所以请任何帮助解决它将非常感谢,谢谢。

启动课程

public class Splash extends Activity{   
  MediaPlayer ourSong;
  private Thread splashTread;
@Override
protected void onCreate(Bundle savedInstanceState) {
    this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
         setContentView(R.layout.splash);  

    ourSong = MediaPlayer.create(Splash.this, R.raw.splashsound); 

    SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences
                (getBaseContext());
    boolean music = getPrefs.getBoolean("checkbox", true);
    if (music == true)      
    ourSong.start();

    Thread timer = new Thread(){
       public void run(){
        try{
        sleep(2000); }
             catch (InterruptedException e){
           e.printStackTrace(); }
      finally{
          Intent i = new Intent(Splash.this, MainActivity.class);
                Splash.this.startActivity(i); }}        
                                           };
                 timer.start();   }

@Override
protected void onPause() {
            // TODO Auto-generated method stub
    super.onPause();
    ourSong.release();
    finish();}
          }
 @Override
 public boolean onTouchEvent(MotionEvent event) {
   if (event.getAction() == MotionEvent.ACTION_DOWN) {
       synchronized(splashTread){
            splashTread.notifyAll();
                  }
           }
       return true;
          }

      }

MainActivity类:

   public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    Boolean customTitleSupported = requestWindowFeature
                     (Window.FEATURE_CUSTOM_TITLE);   
    super.onCreate(savedInstanceState);
    if (customTitleSupported) { 
        getWindow().setFeatureInt
              (Window.FEATURE_CUSTOM_TITLE,R.layout.custom_title);  
        setContentView(R.layout.activity_main);}}

public boolean onCreateOptionsMenu(android.view.Menu menu) {

    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.cool_menu, menu);

    getLayoutInflater().setFactory(new Factory() {
    public View onCreateView(String name, Context context,
    AttributeSet attrs) {

    if (name .equalsIgnoreCase
                    ("com.android.internal.view.menu.IconMenuItemView")) {
    try {

    LayoutInflater li = LayoutInflater.from(context);
    final View view = li.createView(name, null, attrs);

    new Handler().post(new Runnable() {
    public void run() {

    ((TextView) view).setTextSize(25); 

    ((TextView) view).setTextColor(Color.RED);
                   }
            });
    return view;
    } catch (InflateException e) {
    } catch (ClassNotFoundException e) {
            }
        }
    return null;
             }
        });
    return super.onCreateOptionsMenu(menu);}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
    switch (item.getItemId()) {

    case R.id.preferences:

        if (Build.VERSION.SDK_INT < 11) {
            startActivity(new Intent(this, Prefs.class));}
         else {
            startActivity(new Intent(this, OtherPreferencesActivity.class));}                           return(true);
                  }

         return(super.onOptionsItemSelected(item));
                }
           }

Prefs类:

   public class Prefs extends PreferenceActivity{

@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
    Boolean customTitleSupported = requestWindowFeature
                   (Window.FEATURE_CUSTOM_TITLE);    
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    if (customTitleSupported) { 
        getWindow().setFeatureInt
                  (Window.FEATURE_CUSTOM_TITLE,R.layout.custom_title);  
        TextView tv = (TextView) findViewById(R.id.title_tv1); 
         tv.setText("preference screen"); }

    addPreferencesFromResource(R.xml.preference);

                                        }
                        }

PrefsFragment类:

   public class PrefsFragment extends PreferenceFragment {
            @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
         addPreferencesFromResource(R.xml.preference);
                                   }
                      }

OtherPreferencesActivity class:

   public class OtherPreferencesActivity extends PreferenceActivity {
  @Override
   public void onBuildHeaders(List<Header> target) {
      loadHeadersFromResource(R.xml.preference_headers, target);
                                }
                       }  

preference.xml:

  <?xml version="1.0" encoding="utf-8" ?> 
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
  <CheckBoxPreference 
    android:title="Splash Music" 
    android:defaultValue="true" 
    android:key="checkbox" 
    android:summary="Remove the mark to stop music" /> 
</PreferenceScreen> 

preference_headers.xml:

  <preference-headers xmlns:android="http://schemas.android.com/apk/res/android">
<header
    android:fragment="com.tsn.dr.PrefsFragment"
    android:summary="stting1"
    android:title="stting2">
     <extra
    android:name="resource"
    android:value="preferences"/>
</header>
 </preference-headers>
在jelly bean设备上运行时

logcat:

  FATAL EXCEPTION: main
     java.lang.RuntimeException: Unable to start activity ComponentInfo
    {com.test.demo/com.test.demo.MainActivity}: android.util.AndroidRuntimeException: You
       cannot combine custom titles with other title features
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
at android.app.ActivityThread.access$600(ActivityThread.java:140)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4898)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run
       (ZygoteInit.java:1006)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
at dalvik.system.NativeStart.main(Native Method)
     Caused by: android.util.AndroidRuntimeException: You cannot combine custom titles
       with other title features
at com.android.internal.policy.impl.PhoneWindow.requestFeature
       (PhoneWindow.java:279)
at com.android.internal.policy.impl.PhoneWindow.generateLayout
       (PhoneWindow.java:2839)
at com.android.internal.policy.impl.PhoneWindow.installDecor
       (PhoneWindow.java:3079)
at com.android.internal.policy.impl.PhoneWindow.setContentView
        (PhoneWindow.java:304)
at android.app.Activity.setContentView(Activity.java:1924)
at com.test.demo.MainActivity.onCreate(MainActivity.java:28)
at android.app.Activity.performCreate(Activity.java:5206)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
... 11 more

更新:

如果我删除:MainActivity中的自定义标题:

   Boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); 
    if (customTitleSupported) { getWindow().setFeatureInt
      (Window.FEATURE_CUSTOM_TITLE,R.layout.custom_title);  

它在两台设备上运行良好,但我希望保留自定义标题。

1 个答案:

答案 0 :(得分:3)

最后它完美地运作: 我在偏好主题中找到了解决方案,已经在明确表示如下:

 <activity android:name=".Prefs" 
           android:label="@string/app_name" 
           android:theme="@style/PreferencesTheme">

PreferencesTheme.XML:

<?xml version="1.0" encoding="utf-8"?>
 <resources xmlns:android="http://schemas.android.com/apk/res/android">
 <style name="CustomWindowTitleBackground" /> 
  <style name="PreferencesTheme" parent="android:Theme">
  <item name="android:windowTitleSize">35dp</item> 
  <item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item>
   </style>   
 </resources>