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
source share