PreferenceFragment supports all API issues

This is the first time you need to use a preference fragment with preference activity dependent on the accepted answer to this post: PreferenceActivity Android 4.0 and earlier

i applied the sa code below and tested it on a running gingerbread device, it works great, but when testing on a bean, the running app force application is closed directly, I know that something is wrong with my code, or I missed some the thing

so please, any help for solving 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 running on a 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: custom name from MainActivity:

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

works well on both devices, but I want to keep a custom title.

+4
source share
1 answer

Finally, it works fine: I found a solution in the preference topic, which was already mentioned in the manifest as below:

  <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> 
+3
source

Source: https://habr.com/ru/post/1479271/


All Articles