4

这是第一次将偏好片段与偏好活动一起使用,因此取决于这篇文章的接受答案:PreferenceActivity Android 4.0 及更早版本

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

所以请任何帮助解决它将不胜感激,谢谢。

飞溅类:

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));
                }
           }

偏好类:

   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 类:

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

偏好.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> 

首选项_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>

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

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

4

1 回答 1

3

最后它完美地工作:我在已经在清单中提到的偏好主题中找到了解决方案:

 <activity android:name=".Prefs" 
           android:label="@string/app_name" 
           android:theme="@style/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>    
于 2013-05-10T22:24:44.747 回答