【发布时间】: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 来初始化inputClassEditText。 -
请澄清一下?不太明白其中的含义
-
inputClassEditText 在 Dialog 或 Activity 内?
标签: android android-edittext sharedpreferences dialog