【问题标题】:Edittext in Android Dialogbox error is illogical?Android对话框错误中的Edittext不合逻辑?
【发布时间】:2014-04-01 02:16:25
【问题描述】:

当我在我的活动中按下对话框上的肯定按钮时,我的错误就出现了。最初 Edittext 应该将信息保存到 String 中,然后保存到 SharedPreferences 文件中,但经过很多努力无济于事,我修改了大部分代码并隔离了一行仍然给我一个错误。

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();

为了测试可能给我带来问题的这一行,我硬编码了“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) {
        }
    });

【问题讨论】:

  • 你在哪里初始化inputClassEditText?
  • 检查了:(我在设置任何侦听器之前在我的 onCreate 中执行此操作。它与我的其他初始化一起工作,并且资源 ID 匹配正确,所以也不是。
  • 如果 inputClass 在 Dialog 中,那么您将需要 Dialog View 来初始化 inputClass EditText。
  • 请澄清一下?不太明白其中的含义
  • inputClass EditText 在 Dialog 或 Activity 内?

标签: android android-edittext sharedpreferences dialog


【解决方案1】:

如果inputClass EditText 在Dialog 内部,则使用AlertDialog layout View 初始化为:

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

【讨论】:

  • 这很棒!希望所有其他代码现在应该可以工作...介意解释为什么修复工作吗?
  • 实际上并没有解决我的问题,在edittext中输入“asdfasdf”后,调试器仍然显示enteredClass "" (id=830025799024)
  • @AlleyOOP :嗯enteredClass "" (id=830025799024) 与问题无关。我的回答是针对您的问题,因为您说应用程序在单击创建按钮时崩溃
  • @AlleyOOP:用最新代码更新问题,然后我会尽力提供更多帮助
  • 是的,它确实解决了崩溃问题,但不是最终问题。不用担心,我已经解决了。
猜你喜欢
  • 2012-02-10
  • 1970-01-01
  • 2019-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-14
  • 1970-01-01
相关资源
最近更新 更多