Question

My error is brought about as soon as I press the positive button on the dialogue box in my activity. Originally the Edittext was supposed to save the information into a String and the subsequently into a SharedPreferences file, but after much effort to no avail, I rem'ed most of the code and isolated one line that still gave me an error.

spinnerClass.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> parent, View view,
                int pos, long id) {
            // have each new entry be "insert"ed to arrayadapter so create is at end
            // the newer, the closer to the top
            if (id==1)
                // eventually uses as element value to check array or SharedPrefs if matches
                // "Create New+" or not < so doesn't prompt for no reason
            {
                build = new AlertDialog.Builder(MainActivity.this);
                LayoutInflater inflater = getLayoutInflater();

                // Pass null as the parent view because its going in the dialog layout
                build.setView(inflater.inflate(R.layout.create_class_dialog, null))
                       .setTitle("New Class Entry")
                       .setPositiveButton("Create", new DialogInterface.OnClickListener() {
                           @Override
                           public void onClick(DialogInterface dialog, int id) {
                               String farnsworth = inputClass.getText().toString();

to test this ONE line that could have given me an issue, I hardcoded "Hello world." which executed without an error. Now this simple change is crashing my app!

/*                         
                              // classesPrefsList = getSharedPreferences(CLASSES_PREFS, 0);
                              //SharedPreferences.Editor editor = classesPrefsList.edit();
                               //editor.putString("ClassList"+(classesList.size()-1), enteredClass);
                               //editor.commit();
                               //classesList = getClassesArrayListSharedPreferences();
                              // classAdapter.notifyDataSetChanged();
                               * */
                               dialog.cancel();
                           }
                       })
                       .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                           public void onClick(DialogInterface dialog, int id) {
                               dialog.cancel();
                           }
                       });      
                AlertDialog alert = build.create();
                alert.show();
            }
        }
        public void onNothingSelected(AdapterView<?> parent) {
        }
    });
Was it helpful?

Solution

If inputClass EditText is inside Dialog then use AlertDialog layout View to initialize as:

build = new AlertDialog.Builder(MainActivity.this);
LayoutInflater inflater =  LayoutInflater.from(MainActivity.this);
View alertview = inflater.inflate(R.layout.create_class_dialog, null);

/// initialize EditText here..
inputClass=(EditText)alertview.findViewById(R.id.edittextid);
build.setView(alertview)
.setTitle("New Class Entry")
....your code here...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top