Show spinner in AlertDialog

I want to show the spinner in my alertDialog. I have the following code, but it just gives me a black screen. Logcat does not report any errors.

MyActivity.java

AlertDialog.Builder builder; AlertDialog alertDialog; Context mContext = getApplicationContext(); LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE); View layout = inflater.inflate(R.layout.spinner,null); String array_spinner[]; array_spinner=new String[5]; array_spinner[0]="US"; array_spinner[1]="Japan"; array_spinner[2]="China"; array_spinner[3]="India"; array_spinner[4]="Vietnam"; Spinner s = (Spinner) layout.findViewById(R.id.Spinner01); ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item, array_spinner); s.setAdapter(adapter); builder = new AlertDialog.Builder(mContext); builder.setView(layout); alertDialog = builder.create(); 

main.xml

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> </LinearLayout> 

Spinner.xml

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Spinner android:id="@+id/Spinner01" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> 

I am really new, thanks for the help! :)

edit: when I added alertDialog.show (); his power is closing, logcat says:

 11-18 18:14:09.886: D/AndroidRuntime(21856): Shutting down VM 11-18 18:14:09.886: W/dalvikvm(21856): threadid=1: thread exiting with uncaught exception (group=0x401b8888) 11-18 18:14:09.909: E/AndroidRuntime(21856): FATAL EXCEPTION: main 11-18 18:14:09.909: E/AndroidRuntime(21856): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.SpinnerExample/com.example.SpinnerExample.SpinnerExampleActivity}: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application 11-18 18:14:09.909: E/AndroidRuntime(21856): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2668) 11-18 18:14:09.909: E/AndroidRuntime(21856): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2684) 11-18 18:14:09.909: E/AndroidRuntime(21856): at android.app.ActivityThread.access$2300(ActivityThread.java:126) 11-18 18:14:09.909: E/AndroidRuntime(21856): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2038) 11-18 18:14:09.909: E/AndroidRuntime(21856): at android.os.Handler.dispatchMessage(Handler.java:99) 11-18 18:14:09.909: E/AndroidRuntime(21856): at android.os.Looper.loop(Looper.java:123) 11-18 18:14:09.909: E/AndroidRuntime(21856): at android.app.ActivityThread.main(ActivityThread.java:4632) 11-18 18:14:09.909: E/AndroidRuntime(21856): at java.lang.reflect.Method.invokeNative(Native Method) 11-18 18:14:09.909: E/AndroidRuntime(21856): at java.lang.reflect.Method.invoke(Method.java:521) 11-18 18:14:09.909: E/AndroidRuntime(21856): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:871) 11-18 18:14:09.909: E/AndroidRuntime(21856): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629) 11-18 18:14:09.909: E/AndroidRuntime(21856): at dalvik.system.NativeStart.main(Native Method) 11-18 18:14:09.909: E/AndroidRuntime(21856): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application 11-18 18:14:09.909: E/AndroidRuntime(21856): at android.view.ViewRoot.setView(ViewRoot.java:509) 11-18 18:14:09.909: E/AndroidRuntime(21856): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:207) 11-18 18:14:09.909: E/AndroidRuntime(21856): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:121) 11-18 18:14:09.909: E/AndroidRuntime(21856): at android.app.Dialog.show(Dialog.java:259) 11-18 18:14:09.909: E/AndroidRuntime(21856): at com.example.SpinnerExample.SpinnerExampleActivity.onCreate(SpinnerExampleActivity.java:45) 11-18 18:14:09.909: E/AndroidRuntime(21856): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 11-18 18:14:09.909: E/AndroidRuntime(21856): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2632) 11-18 18:14:09.909: E/AndroidRuntime(21856): ... 11 more 
+4
source share
5 answers

You need to use alertDialog.show() to display the dialog. You may also get an error due to the mContext you use there.

Often this does not help in the dialogue. Instead, try using ActivityName.this as mContext .

+4
source
 alertDialog.show() 

to view the dialog box.

+3
source
 Unable to start activity ComponentInfo{com.example.SpinnerExample/com.example.SpinnerExample.SpinnerExampleActivity} 

this means that in the package name: com.example.SpinnerExample

and

activity name: com.example.SpinnerExample.SpinnerExampleActivity.

to remove the qualified name and set ActivityName .SpinnerExample

0
source

This often happens if you give new Dialog(context); wrong context. Use this when building a dialog:

 final Dialog dialog = new Dialog(YourClass.this); dialog.setContentView(R.layout.your_xml_layout); dialog.setTitle("Dialog Title"); dialog.show(); 

Remember to change YourClass to the class name and path to the XML layout, respectively.

0
source

first, your mContext should be equal to your mContext = this activity on your onCreate, and Spinner should be declared in your onCreate if this solution should work for your problem.

0
source

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


All Articles