不正确使用首选项

发布于 2024-11-24 15:42:16 字数 2470 浏览 1 评论 0原文

我正在尝试向我的应用程序添加首选项,但无法使其工作。

这是我的res/xml/preferences.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
  <ListPreference 
        android:key="format"
        android:title="Saving Format"
        android:summary="Select the file format"
        android:defaultValue=".jpg" 
        android:entries="@array/format"
        android:entryValues="@array/formatValues" />        
</PreferenceScreen>

这是我的values/array.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <string-array name="format">
        <item name=".jpg">JPEG</item>
        <item name=".gif">GIF</item>

   </string-array>

    <string-array name="formatValues">
        <item name=".jpg">.jpg</item>
        <item name=".gif">.gif</item>

    </string-array>
</resources>

这是我的首选项活动:

package com.sass.recorder;

import android.os.Bundle;
import android.preference.PreferenceActivity;

public class MyPreferences extends PreferenceActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {        
       super.onCreate(savedInstanceState);        
       addPreferencesFromResource(R.xml.preferences);        
    }

}

这是主要活动类:

...imports..

public class MyApp extends Activity {
...Initializations..
SharedPreferences preferences;
.....
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
        ....
        private OnClickListener mOneShotListener = new OnClickListener() {
        public void onClick(View v) {
        //evaluate preferences and change the path variable
        if (preferences.getString("format", ".jpg") == ".jpg") {
        path = "/sdcard/output/" + editText1.getText() + ".jpg";
        } else if (preferences.getString("format", ".gif") == ".gif"){
        path = "/sdcard/output/" + editText1.getText() + ".gif";
        }
        }
        }
    }
}

首选项活动有效(来自我没有粘贴到此处的菜单),但是具有偏好评估的按钮根本不会单击或做出反应。有时,调试器会在该行中显示 NullPointerException if (preferences.getString("format", ".jpg") == ".jpg")

你知道我做错了什么吗? 谢谢!

I'm trying to add preferences to my app but cant make it work.

Here is my res/xml/preferences.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
  <ListPreference 
        android:key="format"
        android:title="Saving Format"
        android:summary="Select the file format"
        android:defaultValue=".jpg" 
        android:entries="@array/format"
        android:entryValues="@array/formatValues" />        
</PreferenceScreen>

Here is my values/array.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <string-array name="format">
        <item name=".jpg">JPEG</item>
        <item name=".gif">GIF</item>

   </string-array>

    <string-array name="formatValues">
        <item name=".jpg">.jpg</item>
        <item name=".gif">.gif</item>

    </string-array>
</resources>

Here is my preferences activity:

package com.sass.recorder;

import android.os.Bundle;
import android.preference.PreferenceActivity;

public class MyPreferences extends PreferenceActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {        
       super.onCreate(savedInstanceState);        
       addPreferencesFromResource(R.xml.preferences);        
    }

}

Here is the main activity class:

...imports..

public class MyApp extends Activity {
...Initializations..
SharedPreferences preferences;
.....
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
        ....
        private OnClickListener mOneShotListener = new OnClickListener() {
        public void onClick(View v) {
        //evaluate preferences and change the path variable
        if (preferences.getString("format", ".jpg") == ".jpg") {
        path = "/sdcard/output/" + editText1.getText() + ".jpg";
        } else if (preferences.getString("format", ".gif") == ".gif"){
        path = "/sdcard/output/" + editText1.getText() + ".gif";
        }
        }
        }
    }
}

The preferences acitivity works(from the menu which I didn't paste here), but the button that has the preferences evaluation simply won't click or react. Sometimes the debuger shows a NullPointerException in the line
if (preferences.getString("format", ".jpg") == ".jpg")

Any ideas what I'm doing wrong?
Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柠檬 2024-12-01 15:42:16

您已将变量 preferences 声明为类中的实例变量(在 OnClickListener 中使用)和 onCreate 方法中。实例变量从未初始化,因此在 OnClickListener 中访问时为 null。更改

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);

preferences = PreferenceManager.getDefaultSharedPreferences(this);

MyApp 活动,它可能会起作用。

You've declared the variable preferences both as instance variable in the class, which is used in the OnClickListener, and inside the onCreate method. The instance variable is never initialized and is therefor null when accessed in the OnClickListener. Change

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);

into

preferences = PreferenceManager.getDefaultSharedPreferences(this);

in the MyApp activity and it will probably work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文