不正确使用首选项
我正在尝试向我的应用程序添加首选项,但无法使其工作。
这是我的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 lineif (preferences.getString("format", ".jpg") == ".jpg")
Any ideas what I'm doing wrong?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您已将变量 preferences 声明为类中的实例变量(在 OnClickListener 中使用)和 onCreate 方法中。实例变量从未初始化,因此在 OnClickListener 中访问时为 null。更改
为
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
into
in the MyApp activity and it will probably work.