4

This is first time to use preference fragment with preference activity so depend on the accepted answer of this post :PreferenceActivity Android 4.0 and earlier

i applied the code sa below and test it on gingerbread running device , its work fine but when tested on jelly bean running device app force closed directly , i know there is some thing wrong with my code or i miss some thing ,

so please any help to solve it will be highly appreciated , thanks .

Splash class:

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

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

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

   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>

logcat when run on jelly bean device:

  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

UPDATE:

IF i remove: the custom title from MainActivity :

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

its run well on both device but i want to keep custom title .

Community
  • 1
  • 1
Android Stack
  • 4,314
  • 6
  • 31
  • 49
  • You should look at the logcat. (p.s splash screens **do not** belong on Android) – Joe Simpson May 05 '13 at 21:02
  • QJoe Simpson logcat posted. – Android Stack May 05 '13 at 21:10
  • Check your theme, style, and layout XML for any other locations where you might be using a title feature such as `android:windowNoTitle` – Bryan Herbst May 09 '13 at 14:16
  • @Tanis.7x i check it nothing like this any where – Android Stack May 09 '13 at 20:13
  • Have you had a look at [this answer](http://stackoverflow.com/a/13944722/1553481)? It essentially boils down to using the same `minSDKVersion` as `targetSDKVersion` in `AndroidManifest.xml`. – likeitlikeit May 10 '13 at 16:21
  • @likeitlikeit but im still need to support both sdk version – Android Stack May 10 '13 at 19:44
  • What happens if you change your `targetSDKversion`? – likeitlikeit May 10 '13 at 20:39
  • @ likeitlikeit i set it to targetSDKversion = 8 its work but i cant set custom title to custom action bar but when i remove targetSDKversion totally just keep only its work fine , ifthere is any problem i will face it with my app if i just keep only regarding compatibality with all devices and android version – Android Stack May 10 '13 at 21:14
  • @likeitlikeit as google decumented : To maintain your application along with each Android release, you should increase the value of this attribute to match the latest API level, then thoroughly test your application on the corresponding platform version – Android Stack May 10 '13 at 21:17
  • @likeitlikeit why this happen with preference activity only , i creat custom title test app its work fine – Android Stack May 10 '13 at 21:44

1 Answers1

3

Finally it works perfectly : i found the solution in the preference theme which already refered in manifest as bellow :

 <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>    
Android Stack
  • 4,314
  • 6
  • 31
  • 49