java–PreferenceFragment支持所有API问题

java–PreferenceFragment支持所有API问题,第1张

概述这是第一次使用偏好片段和偏好活动取决于这篇文章的接受答案:PreferenceActivityAndroid4.0andearlier我应用下面的代码sa并在姜饼运行设备上测试它,它的工作正常但是当在果冻豆运行设备app力直接关闭测试时,我知道我的代码有些问题或者我想念一些东西,所以请任何帮助解决它

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

我应用下面的代码sa并在姜饼运行设备上测试它,它的工作正常但是当在果冻豆运行设备app力直接关闭测试时,我知道我的代码有些问题或者我想念一些东西,

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

飞溅类:

public class Splash extends Activity{     MediaPlayer ourSong;  private Thread splashTread;@OverrIDeprotected 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();   }@OverrIDeprotected 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 {@OverrIDeprotected 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);}@OverrIDepublic 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")@OverrIDeprotected 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);                                }                       }  

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 featuresat androID.app.ActivityThread.performlaunchActivity(ActivityThread.java:2100)at androID.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)at androID.app.ActivityThread.access0(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 featuresat 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);  

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

解决方法:

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

 <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>    
总结

以上是内存溢出为你收集整理的java – PreferenceFragment支持所有API问题全部内容,希望文章能够帮你解决java – PreferenceFragment支持所有API问题所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1118976.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存