Android calendar.get(calendar.Year)上的某些时间出现NullPointerException

Android calendar.get(calendar.Year)上的某些时间出现NullPointerException,android,calendar,nullpointerexception,Android,Calendar,Nullpointerexception,Google play console表示,在某些设备上(或有时),我的应用程序会崩溃NullPointerException。以下是我的应用程序代码: Double jd = DateTimeUtils.jdCalc( MainActivity.calenda.get(Calendar.YEAR), <== here is 465 row MainActivity.calenda.get(Calendar.MONTH)+1

Google play console表示,在某些设备上(或有时),我的应用程序会崩溃NullPointerException。以下是我的应用程序代码:

    Double jd  = DateTimeUtils.jdCalc(
        MainActivity.calenda.get(Calendar.YEAR),            <== here is 465 row 
        MainActivity.calenda.get(Calendar.MONTH)+1,
        MainActivity.calenda.get(Calendar.DAY_OF_MONTH),
        0, 
        0, 
        0,
        timeZoneHrs);
你知道为什么MainActivity.calenda.get(Calendar.YEAR)有时为空吗

更新:MainActivity类

public class MainActivity extends Dash {

MyLocation myLocation;
public static Calendar calenda;
public static Calendar day_today;
public static double            julDat; 
public static IabHelper mHelper;
public static boolean use_24 = false;
public static boolean use_c = false;
public static boolean use_km = false;
public static boolean first_su = true;

/* (non-Javadoc)
 * @see android.app.Activity#onPause()
 */
@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    myLocation.cancelLocTimer();

}


static SharedPreferences prefs;
SharedPreferences preferences;
public  static final String     PREFS_NAME = "SolunarForecast";
public  static boolean  gps_on = true;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    day_today = Calendar.getInstance();
    day_today.add(Calendar.DAY_OF_MONTH, 1);
    calenda = Calendar.getInstance();
    julDat      = DateTimeUtils.nowJulDat();

    prefs = PreferenceManager.getDefaultSharedPreferences(this);
    use_24 = prefs.getBoolean("time_format_24", false);
    use_c = prefs.getBoolean("units_format_c", false);
    use_km = prefs.getBoolean("units_format_km", false);
    first_su = prefs.getBoolean("first_sunday", true);
    Dash.SLat = Double.parseDouble(prefs.getString("Last_lat", "51.57"));
    Dash.SLon = Double.parseDouble(prefs.getString("Last_lon", "0"));   
    String base64EncodedPublicKey = "";
    mHelper = new IabHelper(this, base64EncodedPublicKey);
    mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
        public void onIabSetupFinished(IabResult result) {
            if (!result.isSuccess()) {
                gp_enabled = false;
                return;
            }
            gp_enabled = true;
            String[] moreSkus = {"fh.solunar.pro"};
            mHelper.queryInventoryAsync(true, Arrays.asList(moreSkus), mGotInventoryListener);
        }
    });             
    LocationResult locationResult = new LocationResult(){
        @Override
        public void gotLocation(Location location){

            try{
            Dash.SLat = location.getLatitude();
            Dash.SLon = location.getLongitude();
            Dash.YesLocation = true;
            prefs = getSharedPreferences(MainActivity.PREFS_NAME, 0);
            Editor ed = prefs.edit();
            ed.putString("Last_lat", location.getLatitude()+"");
            ed.putString("Last_lon", location.getLongitude()+"");
            ed.commit();
            }
            catch(Exception ex){
                Dash.YesLocation = true; 
                //trace("Your current location is temporarily not available.");
            }

        }
    };
    myLocation = new MyLocation();
    gps_on = myLocation.getLocation(this, locationResult);
    if (!gps_on) {
        trace("Please enable Location Services in your Android settings");
        Dash.YesLocation = true;
    }


    AlertDialog.Builder builder = new AlertDialog.Builder(this)
    .setPositiveButton("Rate it!", null)
    .setNegativeButton("No, thanks", null)
    .setNeutralButton("Remind me later", null);
    LayoutInflater inflater = this.getLayoutInflater();

    builder.setView(inflater.inflate(R.layout.ratedialog, null));

new AppRate(this)
    .setShowIfAppHasCrashed(false)
    .setCustomDialog(builder)
    .setMinLaunchesUntilPrompt(5)
    .init();       


}

}表示
MainActivity.calenda
为空。如何在代码中初始化该引用

有时

表明您没有注意线程安全

我认为现在有道理了

您已经创建了一个静态的
日历
,它被初始化为空


在对
onCreate
方法的调用中,仅将该实例设置为非空引用。如果在调用该方法之前执行其他操作,则引用仍然为空。

在MainActivity:public static calenda;calenda=Calendar.getInstance();看起来不对。为什么是静态的?编写的语法不可编译。公共静态-我在许多活动中使用这个变量“calenda”。它在许多设备中都能正常工作。但是有些人会出错。发布整个MainActivity类;你会有更好的运气。我们能得到一些“calenda”和get方法的代码吗?
public class MainActivity extends Dash {

MyLocation myLocation;
public static Calendar calenda;
public static Calendar day_today;
public static double            julDat; 
public static IabHelper mHelper;
public static boolean use_24 = false;
public static boolean use_c = false;
public static boolean use_km = false;
public static boolean first_su = true;

/* (non-Javadoc)
 * @see android.app.Activity#onPause()
 */
@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    myLocation.cancelLocTimer();

}


static SharedPreferences prefs;
SharedPreferences preferences;
public  static final String     PREFS_NAME = "SolunarForecast";
public  static boolean  gps_on = true;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    day_today = Calendar.getInstance();
    day_today.add(Calendar.DAY_OF_MONTH, 1);
    calenda = Calendar.getInstance();
    julDat      = DateTimeUtils.nowJulDat();

    prefs = PreferenceManager.getDefaultSharedPreferences(this);
    use_24 = prefs.getBoolean("time_format_24", false);
    use_c = prefs.getBoolean("units_format_c", false);
    use_km = prefs.getBoolean("units_format_km", false);
    first_su = prefs.getBoolean("first_sunday", true);
    Dash.SLat = Double.parseDouble(prefs.getString("Last_lat", "51.57"));
    Dash.SLon = Double.parseDouble(prefs.getString("Last_lon", "0"));   
    String base64EncodedPublicKey = "";
    mHelper = new IabHelper(this, base64EncodedPublicKey);
    mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
        public void onIabSetupFinished(IabResult result) {
            if (!result.isSuccess()) {
                gp_enabled = false;
                return;
            }
            gp_enabled = true;
            String[] moreSkus = {"fh.solunar.pro"};
            mHelper.queryInventoryAsync(true, Arrays.asList(moreSkus), mGotInventoryListener);
        }
    });             
    LocationResult locationResult = new LocationResult(){
        @Override
        public void gotLocation(Location location){

            try{
            Dash.SLat = location.getLatitude();
            Dash.SLon = location.getLongitude();
            Dash.YesLocation = true;
            prefs = getSharedPreferences(MainActivity.PREFS_NAME, 0);
            Editor ed = prefs.edit();
            ed.putString("Last_lat", location.getLatitude()+"");
            ed.putString("Last_lon", location.getLongitude()+"");
            ed.commit();
            }
            catch(Exception ex){
                Dash.YesLocation = true; 
                //trace("Your current location is temporarily not available.");
            }

        }
    };
    myLocation = new MyLocation();
    gps_on = myLocation.getLocation(this, locationResult);
    if (!gps_on) {
        trace("Please enable Location Services in your Android settings");
        Dash.YesLocation = true;
    }


    AlertDialog.Builder builder = new AlertDialog.Builder(this)
    .setPositiveButton("Rate it!", null)
    .setNegativeButton("No, thanks", null)
    .setNeutralButton("Remind me later", null);
    LayoutInflater inflater = this.getLayoutInflater();

    builder.setView(inflater.inflate(R.layout.ratedialog, null));

new AppRate(this)
    .setShowIfAppHasCrashed(false)
    .setCustomDialog(builder)
    .setMinLaunchesUntilPrompt(5)
    .init();       


}