gpt4 book ai didi

java - PreferenceFragment 支持所有 API 问题

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:03:33 24 4
gpt4 key购买 nike

这是第一次使用带有偏好 Activity 的偏好 fragment ,所以取决于这篇文章的公认答案:PreferenceActivity Android 4.0 and earlier

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

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

飞溅类:

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

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>

在果冻 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);

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

最佳答案

最后完美运行:我在偏好主题中找到了解决方案,该主题已在 list 中引用如下:

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

关于java - PreferenceFragment 支持所有 API 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16389061/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com