1

This is my SharedPreference class :

public class MySharedPrefs {
private static final String APP_SHARED_PREFS = "com.astroved.Horawatch";
private SharedPreferences appSharedPrefs;
private Editor prefsEditor;

public MySharedPrefs(Context context)
{
    this.appSharedPrefs = context.getSharedPreferences(APP_SHARED_PREFS, Activity.MODE_PRIVATE);
    this.prefsEditor = appSharedPrefs.edit();
}

public String getPrefsValue(String value) {
    return appSharedPrefs.getString(value, "");
}

public void savePrefsValue(String key , String Value) {
    prefsEditor.putString(key, Value);
    prefsEditor.commit();
}

public Boolean checkKey(String Key)
{
    if(appSharedPrefs.contains(Key))
        return true;    
    else
        return false;
}

}

MyFunctionClass :

public class Functions_class extends Activity{


AstroVedTime tz,lat,lon;
TimeZone tz1;
protected MySharedPrefs appPrefs;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);{
        appPrefs = new MySharedPrefs(Functions_class.this);
    }
    }

public void user_informations(int myear,int mMonth, int mDay){

    int year = myear,month = mMonth+1,day = mDay;
    Moment mn = new Moment(year,month,day,user_device_time());
    //appPrefs = new MySharedPrefs(Functions_class.this);

    if(appPrefs.checkKey("City_name1")){
        TimeZone.setDefault(TimeZone.getTimeZone(appPrefs.getPrefsValue("selected_time_zone")));
        Log.v("LOG_TAG"+"here the error is : ", appPrefs.getPrefsValue("selected_lat")+"");
        Log.v("LOG_TAG", appPrefs.getPrefsValue("selected_longi")+"");
        tz1 = TimeZone.getDefault();
        lat = new AstroVedTime(Double.parseDouble(appPrefs.getPrefsValue("selected_lat")));// Latitude
        lon = new AstroVedTime(Double.parseDouble(appPrefs.getPrefsValue("selected_longi")));// Longitude
    }

This is my logcat :

 08-10 14:07:14.947: E/AndroidRuntime(475): FATAL EXCEPTION: Thread-13
08-10 14:07:14.947: E/AndroidRuntime(475): java.lang.NullPointerException
08-10 14:07:14.947: E/AndroidRuntime(475):  at com.astroved.horawatch.Functions_class.user_informations(Functions_class.java:46)
08-10 14:07:14.947: E/AndroidRuntime(475):  at com.astroved.horawatch.HoraWatchActivity$13$1.run(HoraWatchActivity.java:1106)
08-10 14:07:16.628: E/WindowManager(475): Activity com.astroved.horawatch.HoraWatchActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@407f1950 that was originally added here
08-10 14:07:16.628: E/WindowManager(475): android.view.WindowLeaked: Activity com.astroved.horawatch.HoraWatchActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@407f1950 that was originally added here
08-10 14:07:16.628: E/WindowManager(475):   at android.view.ViewRoot.<init>(ViewRoot.java:258)
08-10 14:07:16.628: E/WindowManager(475):   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
08-10 14:07:16.628: E/WindowManager(475):   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
08-10 14:07:16.628: E/WindowManager(475):   at android.view.Window$LocalWindowManager.addView(Window.java:424)
08-10 14:07:16.628: E/WindowManager(475):   at android.app.Dialog.show(Dialog.java:241)
08-10 14:07:16.628: E/WindowManager(475):   at com.astroved.horawatch.HoraWatchActivity$13.onClick(HoraWatchActivity.java:1103)
08-10 14:07:16.628: E/WindowManager(475):   at android.view.View.performClick(View.java:2485)
08-10 14:07:16.628: E/WindowManager(475):   at android.view.View$PerformClick.run(View.java:9080)
08-10 14:07:16.628: E/WindowManager(475):   at android.os.Handler.handleCallback(Handler.java:587)
08-10 14:07:16.628: E/WindowManager(475):   at android.os.Handler.dispatchMessage(Handler.java:92)
08-10 14:07:16.628: E/WindowManager(475):   at android.os.Looper.loop(Looper.java:123)
08-10 14:07:16.628: E/WindowManager(475):   at android.app.ActivityThread.main(ActivityThread.java:3683)
08-10 14:07:16.628: E/WindowManager(475):   at java.lang.reflect.Method.invokeNative(Native Method)
08-10 14:07:16.628: E/WindowManager(475):   at java.lang.reflect.Method.invoke(Method.java:507)
08-10 14:07:16.628: E/WindowManager(475):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-10 14:07:16.628: E/WindowManager(475):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-10 14:07:16.628: E/WindowManager(475):   at dalvik.system.NativeStart.main(Native Method)

I am getting the above error. I am doing some mistake in calling the SharedPreference values. I am not able to figure it out. Previously I get this error

at android.content.contextwrapper.getsharedpreferences contextwrapper.java 146

I cleared the above error by calling SharedPreferences in OnCreate. How to clear the Logcat error shown above.Where I am wrong?

I am calling the method in the MyFunctionClass during the onclick of my Another activity.Its shown below

HoraWatchActivity :

public Functions_class func_cls = new Functions_class();
 private OnClickListener show_panchang_page = new OnClickListener(){

    public void onClick(View v) { 

        pbarDialog = new ProgressDialog( HoraWatchActivity.this );
        pbarDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        pbarDialog.setMessage("Loading Panchang...");
        pbarDialog.show();
               Thread t = new Thread(){
                   public void run(){
                       func_cls.user_informations(mYear, mMonth, mDay); 
                       Message myMessage=new Message();
                       Bundle resBundle = new Bundle();
                       resBundle.putString("status", "SUCCESS"); 
                       myMessage.obj=resBundle;
                       handler.sendMessage(myMessage);
                   }
               };
              t.start();
    }
};

Quicker response will helps me a Lot.Thanks.

9
  • 1
    I think the error is with HoraWatchActivity.java . Not with he one you have posted here. Commented Aug 10, 2012 at 9:11
  • @AndroSelva: In HoraWatchActivity I just called the method in the MyFunctionClass. The Error is showing in the if(appPrefs.checkKey("City_name1")).... So I think the error is retriving the SharedPreference value. Am I Right? Commented Aug 10, 2012 at 9:17
  • Could you please post that part too? It would be helpful to sort things. Commented Aug 10, 2012 at 9:19
  • Yup. I have updated in my question Commented Aug 10, 2012 at 9:22
  • is func_cls initialized? Commented Aug 10, 2012 at 9:25

3 Answers 3

2

Ok found the problem. The problem is with this variable, appPrefs in the user_informations of your Function class. It is null at that point.

What you are doing is, you have initiated the appPrefs in onCreate(), but when you do something like this, public Functions_class func_cls = new Functions_class(); your onCreate is not called, which means it directly enters the method and returns null to you. You have to do some modifications to your existing code.

Try this,

1)Pass your Current Activity context to your method first,

2)uncomment this line inside the method appPrefs = new MySharedPrefs(context);

public void user_informations(int myear,int mMonth, int mDay,Context context){

    int year = myear,month = mMonth+1,day = mDay;
    Moment mn = new Moment(year,month,day,user_device_time());
    appPrefs = new MySharedPrefs(context);

    if(appPrefs.checkKey("City_name1")){
        TimeZone.setDefault(TimeZone.getTimeZone(appPrefs.getPrefsValue("selected_time_zone")));
        Log.v("LOG_TAG"+"here the error is : ", appPrefs.getPrefsValue("selected_lat")+"");
        Log.v("LOG_TAG", appPrefs.getPrefsValue("selected_longi")+"");
        tz1 = TimeZone.getDefault();
        lat = new AstroVedTime(Double.parseDouble(appPrefs.getPrefsValue("selected_lat")));// Latitude
        lon = new AstroVedTime(Double.parseDouble(appPrefs.getPrefsValue("selected_longi")));// Longitude
    }
Sign up to request clarification or add additional context in comments.

1 Comment

HMmm. Again ThankYou.. So I was not passing the Context. I got It :)
1

You are manually instantiating the activity class Functions_class. The framework will take care of this if you start the activity. Even if Functions_class is not being used as an activity then you will have to manually call oncreate of Functions_class. Thats where appPrefs is being initialized. appPrefs is null in user_informations function. Initialize it.

2 Comments

I have initialize tha appPrefs in the user_information.During tat time only i Got the at android.content.contextwrapper.getsharedpreferences contextwrapper.java 146 error. So I initialize in OnCreate.I am calling the user_informations function from another function directly.This will not initialize the appPrefs in the Oncreate of the Func_class. How to initiate this?
U r Correct.I got It. thnks :)
1

I think your approach is wrong as you are calling a method of Activity (i.e.func_cls.user_informations(mYear, mMonth, mDay);) which is not in focus from the visible Activity (HoraWatchActivity.this ) .. and that function is doing nothing but getting value from shared preferences . Data retrieval should be done in NonActivity class .appPrefs is null not initialized.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.