Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
708 views
in Technique[技术] by (71.8m points)

android - Set spinner within custom dialog

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;
   }

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.

Relevant XML data:

<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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;
    }

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...