1

这是我的 SharedPreference 类:

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;
}

}

我的功能类:

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
    }

这是我的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)

我收到上述错误。我在调用这些SharedPreference值时犯了一些错误。我无法弄清楚。以前我收到此错误

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

SharedPreferences我通过调用清除了上述错误OnCreate。如何清除Logcat上面显示的错误。我错在哪里?

我在另一个活动的 onclick 期间调用 MyFunctionClass 中的方法。如下所示

HoraWatch活动:

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();
    }
};

更快的回复将帮助我很多。谢谢。

4

3 回答 3

2

好的,发现问题。问题在于这个变量, appPrefsuser_informations你的Function class. 那时它是空的。

您所做的是,您已经在 onCreate() 中启动了 appPrefs,但是当您执行此类操作时,public Functions_class func_cls = new Functions_class();您的 onCreate 不会被调用,这意味着它直接进入方法并返回 null 给您。您必须对现有代码进行一些修改。

试试这个,

1)首先将您的当前活动上下文传递给您的方法,

2)在方法中取消注释这一行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
    }
于 2012-08-10T09:38:07.633 回答
1

您正在手动实例化活动类 Functions_class。如果您启动活动,框架将处理此问题。即使 Functions_class 没有被用作活动,您也必须手动调用 Functions_class 的 oncreate。那就是初始化 appPrefs 的地方。user_informations 函数中的 appPrefs 为空。初始化它。

于 2012-08-10T09:31:47.237 回答
1

我认为你的方法是错误的,因为你正在调用一个 Activity 方法(iefunc_cls.user_informations(mYear, mMonth, mDay);),它不是可见 Activity (HoraWatchActivity.this) 的焦点......并且该函数什么也不做从共享偏好中获取价值。数据检索应在 NonActivity 类中完成。appPrefs 为 null 未初始化。

于 2012-08-10T09:33:14.833 回答