0

I wanted to have multiple Application Classes just to clean up my code.

So I created 4 classes.

1.A General Class for future references:

public class GlobalVariables extends Application {



}
  1. A Times Class for my time functions:

    public class SetZmanim extends GlobalVariables {
    static double latitude, longitude, elevation;
    static String locationName;
    static TimeZone timeZone;
    static GeoLocation location;
    static ComplexZmanimCalendar czc;
    
    
    public void setZmanimAPI() {
        locationName = "Queens, NY";
        latitude = 40.725058; //Lakewood, NJ
        longitude = -73.815653; //Lakewood, NJ
        elevation = 0; //optional elevation
        timeZone = TimeZone.getTimeZone("America/New_York");
        location = new GeoLocation(locationName, latitude, longitude, elevation, timeZone);
        czc = new ComplexZmanimCalendar(location);
    
    }
    

    }

  2. and 4 are implemented the same way as 2 extending the original class

Here is my Manifest:

    <application
    android:name=".GlobalVariables"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

Error: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.edonfreiner.siddur/com.example.edonfreiner.siddur.MainActivity}: java.lang.ClassCastException: com.example.edonfreiner.siddur.GlobalVariables cannot be cast to com.example.edonfreiner.siddur.SetStyles

Failing part:

SetStyles setStyles;
setStyles = ((SetStyles) this.getApplication());
2
  • Well I don't agree that this cleans up your code (I'd tell a junior to redo this in a heartbeat). And keeping dozens of statics in Application is considered a bad idea these days. But either way, you didn't post the part of the code that's actually failing- there's a cast problem somewhere. Jun 26, 2017 at 15:25
  • Sorry, youre right. just added it. So if i need many variables in other classes, what should I do? and why does it not make my code cleaner? Jun 26, 2017 at 15:37

0

Your Answer

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