AlertDialog创建时的代码中断。我觉得我的Context错了......?

时间:2013-03-15 01:35:34

标签: java android sharedpreferences android-alertdialog onclicklistener

我似乎无法弄清楚为什么我的应用/代码在本节中崩溃了。任何帮助,将不胜感激。我认为问题在于在else if语句中创建AlertDialog。

基本上,首次启动应用程序时会调用此方法,并要求用户在两个选项之间进行选择:OCPS和Other。选择OCPS时,将设置SharedPreference。选择other时,会弹出带有文本框的AlertDialog,允许用户输入自己的本地URL,然后保存到SharedPreference。

此处提供完整代码:https://github.com/danielblakes/progressbook/

代码如下:

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 个答案:

答案 0 :(得分:1)

更改

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

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

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

需要更改为

final EditText input = new EditText(context);