0

我有一个偏好活动,其中包含一个复选框和一个列表偏好。此应用程序的主要活动获取列表首选项的当前值并将其用作获取数据的 url。我的问题是,在全新安装时未设置默认值,因此该值为 null 并且应用程序在能够进入首选项屏幕之前立即崩溃。有什么修复吗??

代码:

public class Preferences extends PreferenceActivity implements OnSlideMenuItemClickListener {
private SlideMenu slidemenu;
private final static int News = 1;
private final static int Library = 2;
private final static int Photos = 3;
private final static int Videos = 4;
private final static int Maps = 5;
private final static int Prefs = 6;
private final static int About = 7;
GoogleAnalyticsTracker tracker;
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.preferences);
    setContentView(R.layout.show_settings_layout);
    tracker = GoogleAnalyticsTracker.getInstance();
    tracker.startNewSession("UA-35087731-1", 10, this);
    Typeface blockFonts = Typeface.createFromAsset(getAssets(),"FUTURAM.TTF");
    TextView lblTitle = (TextView) findViewById(R.id.actionbar); 
    lblTitle.setText("Preferences");
    lblTitle.setTypeface(blockFonts);


          ListPreference listPreference = (ListPreference) findPreference("news_feed");
            if(listPreference.getValue()==null) {

                    listPreference.setValueIndex(1);
                final CheckBoxPreference checkboxPref = (CheckBoxPreference) getPreferenceManager().findPreference("french");

                checkboxPref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {            
                    public boolean onPreferenceChange(Preference preference, Object newValue) {
                        if(newValue.toString().equals("true")) {
                            ListPreference newsPref = (ListPreference) findPreference("news_feed");

                            String[] values = getResources().getStringArray(R.array.newsfeedfr);
                                    newsPref.setEntryValues(values);
                                    newsPref.setDefaultValue(values[1]);

                        }
                        else {
                            ListPreference news2Pref = (ListPreference) findPreference("news_feed");                                
                            String[] values2 = getResources().getStringArray(R.array.newsfeed);                                     news2Pref.setEntryValues(values2);                                  news2Pref.setDefaultValue(values2[1]);

                        }      
                        return true;
                    }
            }); }
}
    }

 SharedPreferences sharedPrefs = 
                PreferenceManager.getDefaultSharedPreferences(getBaseContext());
     URL= sharedPrefs.getString("news_feed", "N/A");
        if(URL==null) {
            URL = "http://scout.org/rss/feed/all";
        }

这就是我在新闻类中检索值的方式


LOGCAT 输出

10-08 00:52:06.569: E/WindowManager(305): Activity org.scouts.android.news.News has  leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@44eb8558 that was originally added here
10-08 00:52:06.569: E/WindowManager(305): android.view.WindowLeaked: Activity org.scouts.android.news.News has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@44eb8558 that was originally added here
10-08 00:52:06.569: E/WindowManager(305):   at android.view.ViewRoot.<init>(ViewRoot.java:247)
10-08 00:52:06.569: E/WindowManager(305):   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
10-08 00:52:06.569: E/WindowManager(305):   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
10-08 00:52:06.569: E/WindowManager(305):   at android.view.Window$LocalWindowManager.addView(Window.java:424)
10-08 00:52:06.569: E/WindowManager(305):   at android.app.Dialog.show(Dialog.java:241)
10-08 00:52:06.569: E/WindowManager(305):   at org.scouts.android.news.News$MyAsyncgTask.onPreExecute(News.java:171)
10-08 00:52:06.569: E/WindowManager(305):   at android.os.AsyncTask.execute(AsyncTask.java:391)
10-08 00:52:06.569: E/WindowManager(305):   at org.scouts.android.news.News.onCreate(News.java:100)
10-08 00:52:06.569: E/WindowManager(305):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-08 00:52:06.569: E/WindowManager(305):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
10-08 00:52:06.569: E/WindowManager(305):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
10-08 00:52:06.569: E/WindowManager(305):   at android.app.ActivityThread.access$2300(ActivityThread.java:125)
10-08 00:52:06.569: E/WindowManager(305):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
10-08 00:52:06.569: E/WindowManager(305):   at android.os.Handler.dispatchMessage(Handler.java:99)
10-08 00:52:06.569: E/WindowManager(305):   at android.os.Looper.loop(Looper.java:123)
10-08 00:52:06.569: E/WindowManager(305):   at android.app.ActivityThread.main(ActivityThread.java:4627)
10-08 00:52:06.569: E/WindowManager(305):   at java.lang.reflect.Method.invokeNative(Native Method)
10-08 00:52:06.569: E/WindowManager(305):   at java.lang.reflect.Method.invoke(Method.java:521)
10-08 00:52:06.569: E/WindowManager(305):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-08 00:52:06.569: E/WindowManager(305):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-08 00:52:06.569: E/WindowManager(305):   at dalvik.system.NativeStart.main(Native Method)
10-08 00:52:35.980: I/Process(305): Sending signal. PID: 305 SIG: 9
4

1 回答 1

0

问题有点傻,但在行

URL= sharedPrefs.getString("news_feed", "N/A");

它返回 N/A 的值,当没有用户选择一个,而不是 null 因此修复是

URL= sharePrefs.getString("news_feed", null);

于 2012-10-08T02:35:58.813 回答