如何修复 PreferenceActivity 中的空指针?
How to fix null pointer in PreferenceActivity?
在Android突然报错
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.preference.Preference.setOnPreferenceClickListener(android.preference.Preference$OnPreferenceClickListener)' on a null object reference
at com.impyiablue.stoxx.UserSettingActivity.onCreate(UserSettingActivity.java:32)
at android.app.Activity.performCreate(Activity.java:5977)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2258)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2365)
at android.app.ActivityThread.access0(ActivityThread.java:148)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5272)
at java.lang.reflect.Method.invoke(Native Method)
...
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704)
在这段代码中:
public class UserSettingActivity extends PreferenceActivity {
private Preference myPreference;
MainActivity.MyCallBack callBack;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// replaced on 5.1.2016
//
//addPreferencesFromResource(R.xml.preferences);
getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();
myPreference = findPreference("reset");
myPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference arg0) {
...
并且 preferences.xml
文件如下所示:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<EditTextPreference android:title="Your Name"
android:key="username"
android:summary="Please provide your username"></EditTextPreference>
<CheckBoxPreference android:title="Application Updates"
android:defaultValue="false"
android:summary="This option if selected will allow the application to check for latest versions."
android:key="applicationUpdates" />
<Preference
android:key="reset"
android:title="Reset database"
android:summary="This will remove every entry in the database"
/>
</PreferenceScreen>
它之前有效(我无法指定具体时间)。我只想在我的设置中有一个项目 'reset' ,它将删除应用程序的数据库。方法 findPreference
被删除(暗示它是一些过时的代码,不知道如何 'fix' 它)。
如何修复这个空指针异常?
之后
super.onCreate(savedInstanceState);
添加行
addPreferencesFromResource(R.xml.preferences);
在对首选项执行任何操作之前,需要调用 addPreferencesFromResource。
您的 onCreate() 函数中没有 addPreferencesFromResource,因此目前未加载首选项 xml。
使用PreferenceActivity#addPreferencesFromResource(R.xml.preferences);
在Android突然报错
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.preference.Preference.setOnPreferenceClickListener(android.preference.Preference$OnPreferenceClickListener)' on a null object reference
at com.impyiablue.stoxx.UserSettingActivity.onCreate(UserSettingActivity.java:32)
at android.app.Activity.performCreate(Activity.java:5977)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2258)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2365)
at android.app.ActivityThread.access0(ActivityThread.java:148)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5272)
at java.lang.reflect.Method.invoke(Native Method)
...
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704)
在这段代码中:
public class UserSettingActivity extends PreferenceActivity {
private Preference myPreference;
MainActivity.MyCallBack callBack;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// replaced on 5.1.2016
//
//addPreferencesFromResource(R.xml.preferences);
getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();
myPreference = findPreference("reset");
myPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference arg0) {
...
并且 preferences.xml
文件如下所示:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<EditTextPreference android:title="Your Name"
android:key="username"
android:summary="Please provide your username"></EditTextPreference>
<CheckBoxPreference android:title="Application Updates"
android:defaultValue="false"
android:summary="This option if selected will allow the application to check for latest versions."
android:key="applicationUpdates" />
<Preference
android:key="reset"
android:title="Reset database"
android:summary="This will remove every entry in the database"
/>
</PreferenceScreen>
它之前有效(我无法指定具体时间)。我只想在我的设置中有一个项目 'reset' ,它将删除应用程序的数据库。方法 findPreference
被删除(暗示它是一些过时的代码,不知道如何 'fix' 它)。
如何修复这个空指针异常?
super.onCreate(savedInstanceState);
添加行
addPreferencesFromResource(R.xml.preferences);
在对首选项执行任何操作之前,需要调用 addPreferencesFromResource。
您的 onCreate() 函数中没有 addPreferencesFromResource,因此目前未加载首选项 xml。
使用PreferenceActivity#addPreferencesFromResource(R.xml.preferences);