Skip to content

Commit

Permalink
Fix crash from trying to access MainActivity.preferences (null pointer
Browse files Browse the repository at this point in the history
to MainActivity)
  • Loading branch information
caseydavenport committed Mar 11, 2016
1 parent 0a3fa96 commit 37bb196
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 12 deletions.
1 change: 1 addition & 0 deletions AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<uses-permission android:name="android.permission.WRITE_CALENDAR"/>

<application
android:name="com.biermacht.brews.utils.BiermachtApplication"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
Expand Down
4 changes: 3 additions & 1 deletion src/com/biermacht/brews/frontend/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ public class MainActivity extends DriveActivity {
public static DatabaseInterface databaseInterface;
public static IngredientHandler ingredientHandler;
public static Boolean usedBefore;
public static SharedPreferences preferences;

// Shared preferences.
private static SharedPreferences preferences;

// Static drawer list items
private static String DRAWER_RECIPES = "Recipes";
Expand Down
24 changes: 24 additions & 0 deletions src/com/biermacht/brews/utils/BiermachtApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.biermacht.brews.utils;

import android.app.Application;
import android.content.Context;


public class BiermachtApplication extends Application {

private static Application app;

public static Application getApplication() {
return app;
}

public static Context getContext() {
return getApplication().getApplicationContext();
}

@Override
public void onCreate() {
super.onCreate();
app = this;
}
}
6 changes: 3 additions & 3 deletions src/com/biermacht/brews/utils/DriveActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void onResult(DriveApi.DriveContentsResult result) {
writer.write(new RecipeXmlWriter(getApplicationContext()).getXmlText(recipes));
writer.close();
} catch (IOException e) {
Log.e("MainActivity", e.getMessage());
Log.e("DriveActivity", e.getMessage());
}

// Determine the default title for the file.
Expand Down Expand Up @@ -178,7 +178,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
handleConnectToDrive(resultCode, data);
break;
case Constants.REQUEST_DRIVE_FILE_OPEN:
Log.d("MainActivity", "Result from Google Drive file access: " + resultCode);
Log.d("DriveActivity", "Result from Google Drive file access: " + resultCode);
handleDriveFileOpen(resultCode, data);
break;
case Constants.REQUEST_DRIVE_FILE_CREATE:
Expand Down Expand Up @@ -247,7 +247,7 @@ public void onConnectionSuspended(int i) {

@Override
public void onConnectionFailed(ConnectionResult result) {
Log.d("MainActivity", "Google API Connection failed");
Log.d("DriveActivity", "Google API Connection failed");
if (result.hasResolution()) {
try {
result.startResolutionForResult(this, Constants.REQUEST_CONNECT_TO_DRIVE);
Expand Down
21 changes: 13 additions & 8 deletions src/com/biermacht/brews/utils/Units.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.biermacht.brews.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import com.biermacht.brews.frontend.MainActivity;
Expand Down Expand Up @@ -60,6 +62,9 @@ public class Units {
public static final String POUNDS_FORMAL = "Pounds";
public static final String UNITS_FORMAL = "Units";

// SharedPreferences to use.
private static SharedPreferences preferences = BiermachtApplication.getContext().getSharedPreferences(Constants.PREFERENCES, Context.MODE_PRIVATE);

public static String toAbbreviation(String s) {
if (s.toLowerCase().equals(TEASPOONS_FORMAL.toLowerCase())) {
return TEASPOONS;
Expand Down Expand Up @@ -365,7 +370,7 @@ public static double gramsToOunces(double g) {

// Methods to return the units for each measurement system
public static String getUnitSystem() {
if (MainActivity.preferences.getString(Constants.PREF_MEAS_SYSTEM, Units.IMPERIAL).equals
if (preferences.getString(Constants.PREF_MEAS_SYSTEM, Units.IMPERIAL).equals
(IMPERIAL)) {
return IMPERIAL;
}
Expand All @@ -375,7 +380,7 @@ public static String getUnitSystem() {
}

public static String getHopUnits() {
if (MainActivity.preferences.getString(Constants.PREF_MEAS_SYSTEM, Units.IMPERIAL).equals
if (preferences.getString(Constants.PREF_MEAS_SYSTEM, Units.IMPERIAL).equals
(IMPERIAL)) {
return OUNCES;
}
Expand All @@ -385,7 +390,7 @@ public static String getHopUnits() {
}

public static String getFermentableUnits() {
if (MainActivity.preferences.getString(Constants.PREF_MEAS_SYSTEM, Units.IMPERIAL).equals
if (preferences.getString(Constants.PREF_MEAS_SYSTEM, Units.IMPERIAL).equals
(IMPERIAL)) {
return POUNDS;
}
Expand All @@ -395,7 +400,7 @@ public static String getFermentableUnits() {
}

public static String getWaterToGrainUnits() {
if (MainActivity.preferences.getString(Constants.PREF_MEAS_SYSTEM, Units.IMPERIAL).equals
if (preferences.getString(Constants.PREF_MEAS_SYSTEM, Units.IMPERIAL).equals
(IMPERIAL)) {
return QUARTS_PER_POUND;
}
Expand All @@ -405,7 +410,7 @@ public static String getWaterToGrainUnits() {
}

public static String getStrikeVolumeUnits() {
if (MainActivity.preferences.getString(Constants.PREF_MEAS_SYSTEM, Units.IMPERIAL).equals
if (preferences.getString(Constants.PREF_MEAS_SYSTEM, Units.IMPERIAL).equals
(IMPERIAL)) {
return QUARTS;
}
Expand All @@ -415,7 +420,7 @@ public static String getStrikeVolumeUnits() {
}

public static String getTemperatureUnits() {
if (MainActivity.preferences.getString(Constants.PREF_MEAS_SYSTEM, Units.IMPERIAL).equals
if (preferences.getString(Constants.PREF_MEAS_SYSTEM, Units.IMPERIAL).equals
(IMPERIAL)) {
return FAHRENHEIT;
}
Expand All @@ -425,7 +430,7 @@ public static String getTemperatureUnits() {
}

public static String getVolumeUnits() {
if (MainActivity.preferences.getString(Constants.PREF_MEAS_SYSTEM, Units.IMPERIAL).equals(IMPERIAL)) {
if (preferences.getString(Constants.PREF_MEAS_SYSTEM, Units.IMPERIAL).equals(IMPERIAL)) {
return GALLONS;
}
else {
Expand All @@ -434,7 +439,7 @@ public static String getVolumeUnits() {
}

public static String getWeightUnits() {
if (MainActivity.preferences.getString(Constants.PREF_MEAS_SYSTEM, Units.IMPERIAL).equals
if (preferences.getString(Constants.PREF_MEAS_SYSTEM, Units.IMPERIAL).equals
(IMPERIAL)) {
return POUNDS;
}
Expand Down

0 comments on commit 37bb196

Please sign in to comment.