android - How to fix null pointer in PreferenceActivity? -


in android error

caused by: java.lang.nullpointerexception: attempt invoke virtual method 'void android.preference.preference.setonpreferenceclicklistener(android.preference.preference$onpreferenceclicklistener)' on null object reference     @ com.impyiablue.stoxx.usersettingactivity.oncreate(usersettingactivity.java:32)     @ android.app.activity.performcreate(activity.java:5977)     @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1105)     @ android.app.activitythread.performlaunchactivity(activitythread.java:2258)     @ android.app.activitythread.handlelaunchactivity(activitythread.java:2365)      @ android.app.activitythread.access$800(activitythread.java:148)      @ android.app.activitythread$h.handlemessage(activitythread.java:1283)      @ android.os.handler.dispatchmessage(handler.java:102)      @ android.os.looper.loop(looper.java:135)      @ android.app.activitythread.main(activitythread.java:5272)      @ java.lang.reflect.method.invoke(native method)      ...                                                                            @ com.android.internal.os.zygoteinit.main(zygoteinit.java:704)  

in piece of code:

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) {              ... 

and preferences.xml file looks follows:

<?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 username"></edittextpreference>     <checkboxpreference android:title="application updates"         android:defaultvalue="false"         android:summary="this option if selected allow application check latest versions."         android:key="applicationupdates" />      <preference         android:key="reset"         android:title="reset database"         android:summary="this remove every entry in database"          /> </preferencescreen> 

it worked before (i cannot specify before time). want have item 'reset' in settings delete database of app. method findpreference striked through (implying outdated code, without clue how 'fix' it).

how can fix null pointer exception?

after

super.oncreate(savedinstancestate); 

add line

addpreferencesfromresource(r.xml.preferences); 

addpreferencesfromresource needs called before preference.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -