Android对话框中的Edittext错误不符合逻辑?

Android对话框中的Edittext错误不符合逻辑?,android,android-edittext,sharedpreferences,dialog,Android,Android Edittext,Sharedpreferences,Dialog,当我在活动中按下对话框上的肯定按钮时,我的错误就产生了。最初,Edittext应该将信息保存到字符串中,然后保存到SharedReferences文件中,但是经过很多努力,都没有成功,我删除了大部分代码,并分离出一行仍然给我带来错误的代码 spinnerClass.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { public void onItemSelected(AdapterView&l

当我在活动中按下对话框上的肯定按钮时,我的错误就产生了。最初,Edittext应该将信息保存到字符串中,然后保存到SharedReferences文件中,但是经过很多努力,都没有成功,我删除了大部分代码,并分离出一行仍然给我带来错误的代码

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();
spinnerClass.setOnItemSelectedListener(新的AdapterView.OnItemSelectedListener(){
已选择公共视图(AdapterView父视图、视图、,
内部位置,长id){
//将每个新条目“插入”到arrayadapter,以便在末尾创建
//越新,越接近顶部
如果(id==1)
//最终使用作为元素值来检查数组或SharedPref是否匹配
//“创建新+”或“不创建”<因此不会无故提示
{
build=新建AlertDialog.Builder(MainActivity.this);
LayoutInflater充气机=getLayoutInflater();
//传递null作为父视图,因为它将在对话框布局中运行
build.setView(充气机.充气(R.layout.create_class_对话框,空))
.setTitle(“新类别条目”)
.setPositiveButton(“创建”,新建DialogInterface.OnClickListener()){
@凌驾
public void onClick(DialogInterface对话框,int-id){
字符串farnsworth=inputClass.getText().toString();
为了测试这一行可能会给我一个问题,我硬编码了“Hello world”。它执行时没有错误。现在这个简单的更改正在破坏我的应用程序

/*                         
                              // 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) {
        }
    });
/*
//classesPrefsList=getSharedReferences(类优先,0);
//SharedReferences.Editor=classesPrefsList.edit();
//editor.putString(“ClassList”+(classesList.size()-1),enteredClass);
//commit();
//classesList=GetClasseSarrayListSharedReferences();
//classAdapter.notifyDataSetChanged();
* */
dialog.cancel();
}
})
.setNegativeButton(“取消”,新建DialogInterface.OnClickListener()){
public void onClick(DialogInterface对话框,int-id){
dialog.cancel();
}
});      
AlertDialog alert=build.create();
alert.show();
}
}
未选择公共无效(AdapterView父级){
}
});

如果对话框中有
inputClass
EditText,则使用
AlertDialog
布局视图初始化为:

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...

您在哪里初始化
inputClass
EditText?检查:(在设置任何侦听器之前,我在onCreate中执行此操作。这与我的其他初始化一起工作,并且资源ID也正确匹配,因此也不是这样。如果
inputClass
在对话框中,则需要对话框视图来初始化
inputClass
EditText。请澄清?无法完全理解其含义
inputClass
EditText在对话框或活动中?这很好!希望所有其他代码现在都能工作…请注意解释修复程序工作的原因?实际上并没有解决我的问题,在EditText中输入“asdfasdf”后,调试器仍然显示
enteredClass”“(id=83002579024)
@AlleyOOP:hmmm
enteredClass”“(id=83002579024)
与问题无关。我的答案与您的问题相符,因为您说应用程序在单击“创建”时崩溃Button@AlleyOOP:用最新代码更新问题,然后我会尽力帮助More是的,它确实解决了崩溃问题,但不是最终问题。不用担心,我已经解决了。