你能告诉我为什么当用户进入设置屏幕时,这个点击监听器会崩溃Android设备吗?
/*
* Create the preference from the xml file. This will be used in a click
* listener.
*/
Preference settingWallpaperChangingIsActivated = (Preference) findPreference("checkbox_change_wallpaper_is_activated");
settingWallpaperChangingIsActivated
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
return true;
}
});
以下是与此单击侦听器相关的settings.xml文件。
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="@string/category_title_wallpaper">
<CheckBoxPreference android:key="checkbox_change_wallpaper_is_activated"
android:title="@string/item_title_change_wallpaper" android:summary="@string/item_summary_change_wallpaper"
android:defaultValue="false" />
<ListPreference android:title="@string/list_title_time_before_changing_wallpaper"
android:key="list_time_before_changing_wallpaper" android:summary="@string/list_summary_time_before_changing_wallpaper"
android:entries="@array/label_time_before_changing_wallpaper"
android:entryValues="@array/value_time_before_changing_wallpaper"
android:defaultValue="Default" />
<!-- -->
</PreferenceCategory>
</PreferenceScreen>
如果点击监听器被注释掉,则可以显示设置屏幕,因此看起来xml文件或点击监听器有问题。
如果我可以让它工作,那么我将在return语句之前添加额外的编码。
感谢。
诚然, 伊马德
更新
这是整个课程,现在正在工作,感谢每个人的帮助:
import hajj.auto.wallpaper.R;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.preference.Preference.OnPreferenceClickListener;
import android.widget.Toast;
public class SettingsActivity extends PreferenceActivity implements
OnSharedPreferenceChangeListener {
SharedPreferences pref;
/*
* This is called when the class is created. It displays a Settings screen
* from the settings.xml file.
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*
* Read the settings definition from XML and show them in the current
* activity (screen).
*/
addPreferencesFromResource(R.xml.settings);
/*
* This Preference Manager is required for the change listener to work.
*/
pref = PreferenceManager.getDefaultSharedPreferences(this);
/*
* This will allow changes in lists to be trapped.
*/
pref.registerOnSharedPreferenceChangeListener(this);
/*
* Create the preference from the xml file. This will be used in a click
* listener.
*/
CheckBoxPreference settingWallpaperChangingIsActivated = (CheckBoxPreference) findPreference("checkbox_changing_is_activated");
//Preference settingWallpaperChangingIsActivated = (CheckBoxPreference) findPreference("checkbox_change_wallpaper_is_activated");
settingWallpaperChangingIsActivated.setOnPreferenceChangeListener(new CheckBoxPreference.OnPreferenceChangeListener() {
public boolean onPreferenceChange(final Preference preference, final Object newValue) {
Toast.makeText(getApplicationContext(), "Test.",
Toast.LENGTH_LONG).show();
boolean activated = (Boolean) newValue;
// updateStuff(activated);
return true;
}
});
} // End method onCreate.
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
// TODO Auto-generated method stub
} // End method onSharedPreferenceChanged.
private void finishThisActivity() {
this.finish();
} // End method finishThisActivity.
}
答案 0 :(得分:2)
在代码中,您使用的是Preference
对象,而在XML中,您有CheckBoxPreference
。这些是两件不同的事情,如果我没记错的话,你就不能把它们抛到一起。
另外,我认为你想要的合适的听众是
CheckBoxPreference.OnPreferenceChangeListener() {
public boolean onPreferenceChange(final Preference preference, final Object newValue) {
// Your code here. Make good use of preference and newValue.
// You can cast newValue to boolean, for example.
}
}
完整的工作样本:
Preference settingWallpaperChangingIsActivated = (CheckBoxPreference) findPreference("checkbox_change_wallpaper_is_activated");
settingWallpaperChangingIsActivated.setOnPreferenceChangeListener(new CheckBoxPreference.OnPreferenceChangeListener() {
public boolean onPreferenceChange(final Preference preference, final Object newValue) {
boolean activated = (Boolean) newValue;
updateStuff(activated);
return true;
}
});
答案 1 :(得分:1)
错误位于hajj.auto.wallpaper.free.SettingsActivity
方法中onCreate()
的第51行。如果没有看到代码,我就不能告诉你了。虽然我想这可能是因为你试图将一个侦听器添加到尚未初始化的对象(可能仍然是null
)。