1

i can't seem to figure out why my app/code is crashing in this section. Any help would be appreciated. I think the problem lies on the creation of an AlertDialog in the else if statement.

Basically, this method is called on first launch of the application and asks the user to choose between two options: OCPS and Other. When OCPS is chosen, a SharedPreference is set. When other is selected, an AlertDialog with text box should pop up, allowing the user to input their own local URL, which is then saved to the SharedPreference.

Full code is available here: https://github.com/danielblakes/progressbook/

code follows:

public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    boolean firstrun = getSharedPreferences(
            "com.danielblakes.progressbook", MODE_PRIVATE).getBoolean(
            "firstrun", true);
    if (firstrun) {
        new AlertDialog.Builder(this).setTitle("First Run").show();
        pickDistrict(this);
        getSharedPreferences("com.danielblakes.progressbook", MODE_PRIVATE)
                .edit().putBoolean("firstrun", false).commit();
    }

    else {
        String saved_district = getSharedPreferences(
                "com.danielblakes.progressbook", MODE_PRIVATE).getString(
                "district", null);
        startupWebView(saved_district);
    }
}

public Dialog pickDistrict(final Context context) {
    AlertDialog.Builder districtalert = new AlertDialog.Builder(context);
    districtalert
            .setTitle(R.string.choose_district)
            .setItems(R.array.districts,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int i) {
                            if (i == 0) {
                                String district_site = "https://parentaccess.ocps.net/General/District.aspx?From=Global";
                                startupWebView(district_site);
                                getSharedPreferences(
                                        "com.danielblakes.progressbook",
                                        MODE_PRIVATE)
                                        .edit()
                                        .putString("district",
                                                district_site).commit();
                            } else if (i == 1) {
                                AlertDialog.Builder customdistrict = new AlertDialog.Builder(context);
                                customdistrict
                                        .setTitle(
                                                R.string.custom_district_title)
                                        .setMessage(
                                                R.string.custom_district_message);
                                final EditText input = new EditText(
                                        getParent());
                                customdistrict.setView(input);
                                customdistrict
                                        .setPositiveButton(
                                                "Ok",
                                                new DialogInterface.OnClickListener() {
                                                    public void onClick(
                                                            DialogInterface dialog,
                                                            int which) {
                                                        String custom_url = input
                                                                .getText()
                                                                .toString();
                                                        getSharedPreferences(
                                                                "com.danielblakes.progressbook",
                                                                MODE_PRIVATE)
                                                                .edit()
                                                                .putString(
                                                                        "district",
                                                                        custom_url)
                                                                .commit();
                                                    }
                                                });
                                customdistrict
                                        .setNegativeButton(
                                                "Cancel",
                                                new DialogInterface.OnClickListener() {
                                                    public void onClick(
                                                            DialogInterface dialog,
                                                            int which) {
                                                        return;
                                                    }
                                                }).show();
                            }
                        }
                    }).show();
    return districtalert.create();
}

}

1 Answers1

1

Change

AlertDialog.Builder customdistrict = new AlertDialog.Builder(this);  

to

AlertDialog.Builder customdistrict = new AlertDialog.Builder(context);

also,

final EditText input = new EditText(getParent());

needed to be changed to

final EditText input = new EditText(context);
Hoan Nguyen
  • 18,033
  • 3
  • 50
  • 54
  • When i do this i get the error "Cannot refer to a non-final variable context inside an inner class defined in a different method", and if i change context to a final variable it still crashes in the same way. Plus, the context being passed into the pickDistrict Dialog is just "this". Full code https://github.com/danielblakes/progressbook/ – danielblakes Mar 15 '13 at 01:47
  • 1
    Change public Dialog pickDistrict(final Context context) – Hoan Nguyen Mar 15 '13 at 01:55
  • @HoanNguyen i did that, but it is still crashing. I updated the original post with your suggestion. – danielblakes Mar 15 '13 at 02:10
  • The reason is at http://stackoverflow.com/questions/7846518/why-it-says-that-cannot-refer-to-a-non-final-variable-i-inside-an-inner-class-d – Hoan Nguyen Mar 15 '13 at 02:10
  • I ran your app. This line fails "final EditText input = new EditText(getParent());" Use the context instead of getParent(), it's the same issue as @HoanNguyen mentioned before – cwhsu Mar 15 '13 at 02:28
  • ah! i hadn't even realized i referenced getParent() again there. Thank you so much @HoanNguyen and @cwhsu! – danielblakes Mar 15 '13 at 02:30