将视图添加到AlertDialog时出错?

时间:2014-06-10 13:44:28

标签: android spinner android-spinner

我创建了一个新对话框,允许用户输入他们的姓名和密码进行登录。 我在这里添加了我的片段:

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

添加了Xml代码:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent">
<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"
        ></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"/>

错误的信息是:

06-10 21:47:41.963 6725-6725 / com.example.HiPda E / AndroidRuntime:FATAL EXCEPTION:main     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.inflate(LayoutInflater.java:489)             在android.view.LayoutInflater.inflate(LayoutInflater.java:396)             在android.view.LayoutInflater.inflate(LayoutInflater.java:352)             在com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:409)             在com.android.internal.app.AlertController.installContent(AlertController.java:240)

我不知道发生了什么事?你能帮助我吗?谢谢!

1 个答案:

答案 0 :(得分:0)

onItemClickListener不能用于微调框小部件,因此请改用spinner.setOnItemSelectedListener。

spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
    // your code here
}

@Override
public void onNothingSelected(AdapterView<?> parentView) {
    // your code here
}

});