如何修复PreferenceActivity中的空指针?

时间:2016-01-15 20:34:48

标签: android 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.access$800(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
        // http://stackoverflow.com/questions/6822319/what-to-use-instead-of-addpreferencesfromresource-in-a-preferenceactivity
        //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>

之前有效(我不能在什么时间之前指定)。我只想在我的设置中有一个项目'重置',这将删除该应用程序的数据库。方法findPreference被删除(暗示它是一些过时的代码,没有任何线索如何“修复”它。)

如何修复此空指针异常?

2 个答案:

答案 0 :(得分:3)

之后

<img scr="/Users/grasbergerm/Projects/djangoshop/djangoshop/media/5_hat_1_2_b8a44Lz.jpg">

添加行

{% for item in items %}
    <img scr="{{item.image.url}}">
    <br>
    {{ item.name }}<br>
    {{ item.author }}<br>
    ${{ item.price }}<br>
{% endfor %}

在对首选项执行任何操作之前,需要调用addPreferencesFromResource。

答案 1 :(得分:0)

您的onCreate()函数中没有addPreferencesFromResource,因此此时未加载首选项xml。

使用PreferenceActivity#addPreferencesFromResource(R.xml.preferences);

来源:http://rominirani.com/android-preferences-tutorial/