Android 将视图添加到AlertDialog时出现了什么错误?
代码是,我想在对话框中添加一个视图,但似乎有一些问题Android 将视图添加到AlertDialog时出现了什么错误?,android,view,dialog,android-alertdialog,Android,View,Dialog,Android Alertdialog,代码是,我想在对话框中添加一个视图,但似乎有一些问题 LayoutInflater flater = LayoutInflater.from(context); final View view = flater.inflate(R.layout.login, null); Spinner spinner=(Spinner)view.findViewById(R.id.safequestion_spinner); ArrayAdap
LayoutInflater flater = LayoutInflater.from(context);
final View view = flater.inflate(R.layout.login, null);
Spinner spinner=(Spinner)view.findViewById(R.id.safequestion_spinner);
ArrayAdapter<String> adapter=new ArrayAdapter<String>(context,android.R.layout.simple_dropdown_item_1line,safeQuestion);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
questionid=""+i;
if(i!=0)
((EditText)view.findViewById(R.id.answear_edittext)).setVisibility(0);
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
questionid=""+0;
}
});
new AlertDialog.Builder(context).setIcon(R.drawable.hi).setTitle(R.string.userlogin).setView(view)
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
((Activity) context).finish();
}
}).setPositiveButton(R.string.login, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//进行登陆验证
name = ((EditText) view.findViewById(R.id.username_edittext)).getText().toString();
password = ((EditText) view.findViewById(R.id.password_edittext)).getText().toString();
if (questionid == "0")
answear = "";
else {
answear = ((EditText) view.findViewById(R.id.answear_edittext)).getText().toString();
}
User user = new User(name, password, questionid, answear);
login(user);
if (isUserAvailable == false)
Tools.checkUser(context);
else {
LoggedInUser = user;
editor.putString("name",name);
editor.putString("password",password);
editor.putString("questionid",questionid);
editor.putString("answear",answear);
editor.commit();
context.startActivity(new Intent(context,MainActivity.class));
((Activity)context).finish();
}
}
}).create().show();
LayoutInflater flater=LayoutInflater.from(上下文);
最终视图=更平。充气(R.layout.login,null);
微调器微调器=(微调器)视图.findViewById(R.id.safequestion\u微调器);
ArrayAdapter=新的ArrayAdapter(上下文,android.R.layout.simple\u下拉列表\u item\u 1line,安全问题);
旋转器。设置适配器(适配器);
spinner.setOnItemSelectedListener(新的AdapterView.OnItemSelectedListener(){
@凌驾
已选择公共视图(AdapterView AdapterView、View视图、int i、long l){
问题id=”“+i;
如果(i!=0)
((EditText)view.findViewById(R.id.answear_EditText)).setVisibility(0);
}
@凌驾
未选择公共无效(AdapterView AdapterView){
问题ID=”“+0;
}
});
新建AlertDialog.Builder(上下文).setIcon(R.drawable.hi).setTitle(R.string.userlogin).setView(视图)
.setNegativeButton(R.string.cancel,新建DialogInterface.OnClickListener(){
@凌驾
公共void onClick(DialogInterface,inti){
((活动)上下文).finish();
}
}).setPositiveButton(R.string.login,新的DialogInterface.OnClickListener(){
@凌驾
公共void onClick(DialogInterface,inti){
//进行登陆验证
name=((EditText)view.findViewById(R.id.username_EditText)).getText().toString();
password=((EditText)view.findViewById(R.id.password_EditText)).getText().toString();
如果(问题ID=“0”)
answear=“”;
否则{
answear=((EditText)view.findViewById(R.id.answear_EditText)).getText().toString();
}
用户=新用户(姓名、密码、问题ID、密码);
登录(用户);
if(isUserAvailable==false)
工具。检查用户(上下文);
否则{
LoggedInUser=用户;
编辑器.putString(“name”,name);
编辑器.putString(“密码”,password);
编辑器.putString(“questionid”,questionid);
编辑:putString(“answear”,answear);
commit();
新意图(context,MainActivity.class));
((活动)上下文).finish();
}
}
}).create().show();
错误的信息是:
06-10 22:07:32.774 8502-8502/com.example.HiPda E/AndroidRuntime﹕ FATAL EXCEPTION: main
android.view.InflateException: Binary XML file line #123: Error inflating class <unknown>
at android.view.LayoutInflater.createView(LayoutInflater.java:613)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:409)
at com.android.internal.app.AlertController.installContent(AlertController.java:240)
at android.app.AlertDialog.onCreate(AlertDialog.java:336)
at android.app.Dialog.dispatchOnCreate(Dialog.java:379)
at android.app.Dialog.show(Dialog.java:261)
06-10 22:07:32.774 8502-8502/com.example.HiPda E/AndroidRuntime﹕ 致命异常:主
android.view.InflateException:二进制XML文件行#123:膨胀类时出错
在android.view.LayoutInflater.createView中(LayoutInflater.java:613)
在com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)上
位于android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
位于android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
位于android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
位于android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
位于android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
在android.view.LayoutInflater.充气(LayoutInflater.java:489)
在android.view.LayoutInflater.inflate(LayoutInflater.java:396)
在android.view.LayoutInflater.充气(LayoutInflater.java:352)
位于com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:409)
位于com.android.internal.app.AlertController.installContent(AlertController.java:240)
在android.app.AlertDialog.onCreate上(AlertDialog.java:336)
在android.app.Dialog.dispatchOnCreate(Dialog.java:379)上
在android.app.Dialog.show上(Dialog.java:261)
当我使用LayoutInflater创建视图时,似乎出现了问题?
你能帮我找出问题所在吗
布局如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="20dp">
<LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content"
>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="@string/username"
/>
<EditText android:layout_width="fill_parent" android:layout_height="wrap_content"
android:id="@+id/username_edittext"
/>
</LinearLayout>
<LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="@string/password"
/>
<EditText android:layout_width="fill_parent" android:layout_height="wrap_content"
android:id="@+id/password_edittext"
android:password="true"
/>
</LinearLayout>
<Spinner android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_marginRight="100dp"
android:id="@+id/safequestion_spinner"
android:spinnerMode="dropdown"
></Spinner>
<EditText android:layout_width="fill_parent" android:layout_height="wrap_content"
android:id="@+id/answear_edittext"
android:visibility="invisible"
/>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:id="@+id/textview"
android:visibility="invisible"/>
你能发布版面吗?我发布了版面,你能再检查一下吗?感谢您尝试过
LayoutInflater flator=getActivity().getLayoutInflater()代码>而不是LayoutInflater flater=LayoutInflater.from(上下文)代码>它似乎有错误…如果为布局输入的代码是完整文件,则它不是格式良好的xml。外部<代码>