Set the counter to the user dialog

I get a NullPointerException when trying to create a Spinner in a dialog box and cannot debug it because the code looks solid. I wonder if anyone else has any ideas. Any help is 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 an exception in the adapter.setDropDownViewResource file (android.R.layout.simple_spinner_dropdown_item); I changed the context to MyClass.this and the exception moved to the next line, which bothers me. I am wondering if there is an adapter that has a null value, but I call everything the same as before, until it is in the dialog box.

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> 

The remaining amount of data has been edited so that it does not take up too much space.

+1
source share
1 answer

I managed to fix this problem. It was very subtle, and I'm sure I was 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 part of the code so that the dialog was initialized in front of the counter, but this was not a problem. I added a dialogue. in Spinner spinner = (Spinner) dialog.findViewById (R.id.pm_server); and it did the trick. Hope this helps others.

+11
source

Source: https://habr.com/ru/post/920967/


All Articles