I want to show a spinner in my alertDialog. I have the following code but it just give me a black screen. Logcat doesn't say 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'm really new here, thanks a lot for any help! :)
edit: when I added alertDialog.show(); it force closes, 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