3

我正在按照这些说明创建PreferenceActivity,但未显示 PereferenceActivity。活动开始,但布局为空白

  1. 我还在我的应用程序中使用材料设计 NavDrawer 通过引用 developer.android.com 但我希望它不会影响
  2. 我正在运行 Kitkat 的设备上测试应用程序
  3. 这是我启动 PreferenceActivity “屏幕截图”时发生的情况
  4. 日志猫: 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;
4

4 回答 4

9

Delete the second parameter in onCreate method:

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

Update for Aviel Fedida: According with this, void onCreate (Bundle savedInstanceState, PersistableBundle persistentState) is called when the activity is being re-initialized. So when you start a new Activity with startActivityForResult or startActivity that method is not called, and the code inside is not executed.

Black screen appears becouse addPreferencesFromResource(R.xml.app_preference); is "unreachable code"

于 2015-03-25T12:23:51.313 回答
2

使用protected void onCreate(Bundle savedInstanceState)代替public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState)

于 2015-08-21T08:22:27.610 回答
1

我遇到了同样的问题,我通过添加以下行解决了它:

setContentView(R.layout.activity_settings);

SettingsActivity这里我的意思是在类的OnCreate方法中添加与你相关的布局文件Settings

在此处输入图像描述

于 2018-03-28T15:11:59.770 回答
0

[重新编辑]

主要问题似乎是您正在使用 aPreferenceActivity和 a PreferenceFragment,并且您假设 aPreferenceActivity具有可以直接访问的操作栏之类的东西。这可能是一个糟糕的假设。

APreferenceFragment实际上是要与“常规”一起使用Activity,而不是与PreferenceActivity.

因此,例如,如果您真的想访问操作栏,则应该使用ActionBarActivity。另请参阅API 指南中的文档

于 2015-01-30T00:23:04.337 回答