在calendar.get(Calendar.Year)上有时会出现NullPointerException

时间:2013-08-12 17:00:34

标签: android calendar nullpointerexception

谷歌播放控制台说,在某些设备上(或有时),我的应用程序崩溃了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);

以下是Google Play控制台的堆栈跟踪:

java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:299)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
at java.util.concurrent.FutureTask.run(FutureTask.java:239)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:864)
Caused by: java.lang.NullPointerException
at com.antonnikitin.solunarforecast.F1Activity.CalcForecast(F1Activity.java:465)
at com.antonnikitin.solunarforecast.F1Activity.access$0(F1Activity.java:454)
at com.antonnikitin.solunarforecast.F1Activity$Atask.doInBackground(F1Activity.java:851)
at com.antonnikitin.solunarforecast.F1Activity$Atask.doInBackground(F1Activity.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask.run(FutureTask.java:234)
... 4 more

知道为什么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();       


}

}

1 个答案:

答案 0 :(得分:0)

这意味着MainActivity.calenda为空。如何在代码中初始化该引用?

  

有时

表示您没有注意线程安全性。

我认为现在有道理。

您已经创建了一个静态Calendar,它在编码时初始化为null。

您只能在调用onCreate方法时将该实例设置为非空引用。如果在调用该方法之前执行任何其他操作,则引用仍为空。