在我的活动中按下对话框上的“正”按钮后,我的错误就会出现。最初Edittext应该将信息保存到String中,然后将其保存到SharedPreferences文件中,但经过多次努力无济于事,我重新编写了大部分代码,并隔离了仍然给我一个错误的一行。 / p>
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();
测试这一行可能给我一个问题,我硬编码&#34; Hello world。&#34;执行没有错误。现在这个简单的改变让我的应用程序崩溃了!
/*
// 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) {
}
});
答案 0 :(得分:1)
如果inputClass
EditText位于Dialog内,则使用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...