我无法在Android中找到Fragment的视图

时间:2016-08-04 06:35:50

标签: android android-fragments

我认为我的问题是我的偏好无法加载Fragment。为什么我无法访问它们。这是我的错误屏幕截图:

logcat的:

http://i.stack.imgur.com/UeTG2.png

SettingsActivity.java

package com.example.poo.sunshine;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;

public class SettingsActivity extends Activity {

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

    // Display the fragment as the main content.
    getFragmentManager().beginTransaction()
            .replace(R.id.container, new MyPreferenceFragment())
            .commit();
}

public static class MyPreferenceFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener 
{

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Add 'general' preferences, defined in the XML file
        addPreferencesFromResource(R.xml.pref_general);

        // For all preferences, attach an OnPreferenceChangeListener so the UI summary can be
        // updated when the preference changes.                

    PreferenceManager.getDefaultSharedPreferences(getActivity())
    .registerOnSharedPreferenceChangeListener(this);

    } 

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {

       Preference locationPref = findPreference(getString(R.string.pref_location_key));

       Preference unitsPref = findPreference(getString(R.string.pref_units_key));

       prefChanged(sharedPreferences, locationPref, key);

       prefChanged(sharedPreferences, unitsPref, key);
    }

     private void prefChanged(SharedPreferences sharedPreferences, Preference pref, String key) {

        if (sharedPreferences instanceof ListPreference) {
            // For list preferences, look up the correct display value in
            // the preference's 'entries' list (since they have separate labels/values).

            ListPreference listPreference = (ListPreference) sharedPreferences;

            int prefIndex = listPreference.findIndexOfValue(key);

            if (prefIndex >= 0) {
                pref.setSummary(listPreference.getEntries()[prefIndex]);
            } else {
                pref.setSummary(sharedPreferences.getString(key, ""));

            }
        }
    }
}
}

所以任何帮助都将不胜感激!我试图在MyPreferenceFragment类中访问以下首选项但不能。当我按下设置选项....我的应用程序崩溃。

pref_general

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent">

 <EditTextPreference
    android:defaultValue="@string/pref_location_default"
    android:inputType="text"
    android:key="@string/pref_location_key"
    android:singleLine="true"
    android:title="@string/pref_location_label"/>

 <ListPreference
    android:title="@string/pref_units_label"
    android:key="@string/pref_units_key"
    android:defaultValue="@string/pref_units_metric"
    android:entryValues="@array/pref_units_values"
    android:entries="@array/pref_units_options"/>

</PreferenceScreen>

这是我的应用程序ScreenShot

enter image description here

strings.xml

    <string name="pref_location_label">Location</string>

    <!-- Key name for storing location in SharedPreferences [CHAR LIMIT=NONE]->

    <string name="pref_location_key" translatable=`enter code here`"false">location</string>

    <!-- Default postal code for location preference [CHAR LIMIT=NONE] -->
    <string name="pref_location_default" translatable="false">94043</string>

1 个答案:

答案 0 :(得分:0)

您可以通过以下方式从资源中获取字符串:

getResources().getString(R.string.pref_location_key);

不是:

getString(R.string.pref_location_key)

更多细节:Android: How do I get string from resources using its name?