0

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


}

}

4

1 回答 1

0

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

有时

表明你没有注意线程安全。

我觉得现在说得通。

您已经创建了一个 static Calendar,它被初始化为 null 作为编码。

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

于 2013-08-12T17:06:30.703 回答