相关疑难解决方法(0)

在PreferenceActivity中使用什么而不是"addPreferencesFromResource"?

我刚刚注意到这个方法在Android的文档(参考文献)中addPreferencesFromResource(int preferencesResId)被标记为已弃用.

不幸的是,该方法的描述中没有提供替代方法.

为了将preferenceScreen.xml连接到匹配的PreferenceActivity,应该使用哪种方法?

xml android

357
推荐指数
5
解决办法
16万
查看次数

具有透明背景的PreferenceFragment?

我创建了一个包含两个类别和一个复选框的PreferenceFragment,但是,当我在我的应用程序中显示它时,背景看起来是透明的.

我可以看到主要活动,字段和PreferenceFragment已经放在主要活动的顶部......这个解决方案是什么?

在我的主要活动中,我这样做是为了在选择设置按钮时打开PreferenceFragment:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    //handle presses on action bar items
    switch (item.getItemId()) {
        case R.id.action_settings:
            getFragmentManager().beginTransaction().replace(android.R.id.content,
                    new SettingsFragment()).commit();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}
Run Code Online (Sandbox Code Playgroud)

我的PreferenceFragment代码如下所示:public class SettingsFragment extends PreferenceFragment {

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

    //load the preferences from an XML resource
    addPreferencesFromResource(R.xml.preferences);
  }
}
Run Code Online (Sandbox Code Playgroud)

我的preferences.xml看起来像这样:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_height="wrap_content"
  android:layout_width="wrap_content"
  android:layout="@layout/fragment_settings">
  <PreferenceCategory
    android:title="@string/server_prefs_title"
    android:key="pref_key_server_settings" >
  <CheckBoxPreference
    android:key="pref_server_url"
    android:title="@string/pref_server_url"
    android:summary="@string/pref_server_url_summ"
    android:defaultValue="true" />
  </PreferenceCategory>
  <PreferenceCategory
    android:title="@string/company_prefs_title"
    android:key="pref_key_company_settings" >
  </PreferenceCategory>
</PreferenceScreen>     
Run Code Online (Sandbox Code Playgroud)

我尝试了Stack Overflow上的另一个答案并将其添加到我的Preference Fragment中,但是,虽然它阻止了从Main Activity中可见的字段,但它似乎只是一个掩码,因为片段字段也受到颜色的影响我设置: …

android preferenceactivity android-preferences android-fragments android-activity

7
推荐指数
2
解决办法
4073
查看次数

无法找到明确的活动类。您是否在 AndroidManifest.xml 中声明了此活动

我正在尝试在我的应用程序中实现一个 PreferenceActivity,遵循这个问题中接受的答案

我得到上述异常

android.content.ActivityNotFoundException: Unable to find explicit activity class {com.iphonik.chameleon/com.iphonik.AppPreferenceActivity}; have you declared this activity in your AndroidManifest.xml?
Run Code Online (Sandbox Code Playgroud)

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.iphonik.chameleon">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.GET_TASKS" />
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@android:style/Theme.NoTitleBar">
        <activity android:name=".MainMenu">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".Movies" />
        <activity android:name=".SettingsActivity" />
        <activity android:name=".InfoActivity" />

        <receiver
            android:name=".AppBroadcastReciever"
            android:enabled="true"
            android:exported="true"
            android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" …
Run Code Online (Sandbox Code Playgroud)

android android-manifest

7
推荐指数
1
解决办法
4万
查看次数