将视图添加到AlertDialog时出现的错误是什么?

时间:2014-06-10 14:24:46

标签: android view dialog alertdialog

代码是,我想在对话框中添加一个视图,但我似乎有一些问题?

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

错误的信息是:

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)

当我使用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"/>

0 个答案:

没有答案