我正在按照这些说明创建PreferenceActivity,但未显示 PereferenceActivity。活动开始,但布局为空白。
- 我还在我的应用程序中使用材料设计 NavDrawer 通过引用 developer.android.com 但我希望它不会影响
- 我正在运行 Kitkat 的设备上测试应用程序
- 这是我启动 PreferenceActivity “屏幕截图”时发生的情况
- 日志猫:
Could not find method android.preference.PreferenceActivity.onCreate VFY: unable to resolve virtual method 950: Landroid/preference/PreferenceActivity;.onCreate(Landroid/os/Bundle;Landroid/os/PersistableBundle;)V
代码:
app_preference.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="Settings">
<ListPreference
android:title="Timer"
android:summary="@string/timer_summary"
android:key="prefTime"
android:defaultValue="30"
android:entries="@array/prefTime"
android:entryValues="@array/prefTimeValue"/>
<SwitchPreference
android:title="Shuffle"
android:summary="@string/shuffle_summary"
android:defaultValue="false"
android:key="prefToggleShuffle"/>
<SwitchPreference
android:title="Allow over Wifi-only"
android:summary="@string/wifi_only_summary"
android:defaultValue="false"
android:key="prefWifiOnly"/>
<Preference
android:title="Restore Defaults"
android:summary="@string/restore_summ"/>
<Preference
android:title="Clear Data"
android:summary="@string/shuffle_summary"/>
</PreferenceCategory>
设置.java
public class Settings extends PreferenceActivity {
@Override
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
getActionBar().setDisplayHomeAsUpEnabled(true);
getFragmentManager().beginTransaction().replace(android.R.id.content, new SettingsFragment()).commit();
}
public static class SettingsFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.app_preference);
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
break;
}
return super.onOptionsItemSelected(item);
}
}
Mainfest.xml
.....
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="21"/>
<activity android:name=".preferences.Settings>
<intent-filter>
<category android:name="android.intent.category.PREFERENCE"/>
</intent-filter>
<meta-data android:name="android.support.PARENT_ACTIVITY"
android:value=".activity.MainActivity"/>
</activity>
.....
呼叫意图
case R.id.action_setting:
Intent intent = new Intent(this, Settings.class);
startActivityForResult(intent, RESULT_SETTINGS);//RESULT_SETTINGS = 1
break;