自定义对话框中设置微调 [英] Set spinner within custom dialog

查看:117
本文介绍了自定义对话框中设置微调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到一个NullPointerException异常试图将对话框中创建一个微调似乎并不能因为code相固态进行调试。不知道其他人有任何想法。任何帮助是极大的AP preciated。

I'm getting a NullPointerException while attempting to create a Spinner within a dialog and can't seem to debug it because the code looks solid. Wonder if anyone else has any idea. Any help is greatly appreciated.

    protected Dialog onCreateDialog(int id)
    {
        Dialog dialog;
        switch(id) {
        case DIALOG_SEND_PM:
            Spinner spinner = (Spinner)findViewById(R.id.pm_server);
            ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.server_array, android.R.layout.simple_spinner_item);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinner.setAdapter(adapter);
            spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());

            dialog = new Dialog(PM.this);
            dialog.setContentView(R.layout.send_pm_dialog);
            dialog.setTitle(R.string.send_pm);
            pmMessage = (EditText) dialog.findViewById(R.id.send_pm_box);
            Button sendPm = (Button) dialog.findViewById(R.id.send_pm_button);
            sendPm.setOnClickListener(PM.this);
            break;
        default:
            dialog = null;
   }

我得到的异常在adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);我改变的背景下MyClass.this和异常向下移动到下一行,这混淆了我。我想知道,如果它是具有一个空值的适配器,但我所说的一切都以同样的方式我之前,而不是在一个对话框。

I get the exception at adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); I changed the context to MyClass.this and the exception moved down to the next line, which confuses me. I'm wondering if it is the adapter having a null value but I call everything the same way I have before while not in a dialog.

相关XML数据:

<LinearLayout>
    <TextView/>

    <LinearLayout>

       <TextView/>
      <EditText/>
      <TextView/>
      <Spinner
        android:id="@+id/pm_server"
        android:layout_height="fill_parent"
        android:layout_width="wrap_content"
        android:background="@drawable/yblueborder"
        android:textColor="#ABABAB"/>
    </LinearLayout>

    <Button/>
</LinearLayout>

编辑出的数据的其余部分,因此不会占用太多的空间。

Edited out the rest of the data so it wouldn't take up too much space.

推荐答案

我设法解决这个问题。这是非常微妙的,我是pretty的肯定,我真的很幸运。这里的工作code:

I have managed to fix the issue. It was very subtle and I'm pretty sure I got lucky. Here's the working code:

protected Dialog onCreateDialog(int id)
    {
        Dialog dialog;
        switch(id) {
        case DIALOG_SEND_PM:
            dialog = new Dialog(PM.this);
            dialog.setContentView(R.layout.send_pm_dialog);
            dialog.setTitle(R.string.send_pm);
            pmMessage = (EditText) dialog.findViewById(R.id.send_pm_box);
            Button sendPm = (Button) dialog.findViewById(R.id.send_pm_button);
            sendPm.setOnClickListener(PM.this);

            Spinner spinner = (Spinner)dialog.findViewById(R.id.pm_server);
            ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.server_array, android.R.layout.simple_spinner_item);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinner.setAdapter(adapter);
            spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
            break;
        default:
            dialog = null;
        }
        return dialog;
    }

我有些感动的code左右,这样的微调之前被初始化的对话框中,但这不是问题。我加了对话。在微调微调=(微调)dialog.findViewById(R.id.pm_server);而且做的伎俩。希望这可以帮助别人。

I moved some of the code around so that the dialog was initialized before the spinner, but that was not the issue. I added dialog. in Spinner spinner = (Spinner)dialog.findViewById(R.id.pm_server); and that did the trick. Hope this helps others.

这篇关于自定义对话框中设置微调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆