单击侦听器使设备崩溃
你能告诉我为什么当用户进入设置屏幕时这个点击监听器会导致 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 语句之前添加额外的编码。
谢谢。
确实, Emad
更新:
这是整个班级现在正在工作的内容,感谢大家的帮助:
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.
}
Can you tell me why this click listener crashes the Android device when the user enters the settings screen?
/*
* 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;
}
});
Here is the settings.xml file related to this click listener.
<?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>
If the click listener is commented out than the settings screen can be displayed so it looks like there's something wrong with the xml file or the click listener.
If I can get it to work, then I will put the additional coding before the return statement.
Thanks.
Truly,
Emad
Update:
This is the entire class that is now working thanks for everyone's help:
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.
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在代码中,您使用
Preference
对象,而在 XML 中您使用CheckBoxPreference
对象。这是两件不同的事情,如果我没记错的话,你不能将其中之一投射到另一件事上。另外,我认为您想要的合适的监听器是
完整的工作示例:
In the code, you're using a
Preference
object, when in the XML you have aCheckBoxPreference
. Those are two different things, and you can't cast one to the other, if I recall correctly.Also, the appropriate listener that I think you are wanting is
Full working sample:
错误位于
onCreate()
方法中hajj.auto.wallpaper.free.SettingsActivity
的第 51 行。如果没有看到该代码,我无法告诉您更多信息。尽管我猜想这可能是因为您尝试向尚未初始化的对象添加侦听器(可能仍然为 null)。The error is on line 51 of
hajj.auto.wallpaper.free.SettingsActivity
in theonCreate()
method. I can't tell you anymore than that without seeing that code. Although I guess it is probably because you try to add a listener to an object that hasn't been initialized yet (is probably stillnull
).