1

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

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

完整代码可在此处获得: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();
}

}

4

1 回答 1

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);
于 2013-03-15T01:41:08.493 回答