Show spinner in AlertDialog

2019-05-06 21:59发布

问题:

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

回答1:

You need to use alertDialog.show() to show the dialog.Also you might get error because of mContext you use there.

Often that doesn't help in dialog.Instead,try using ActivityName.this as mContext.



回答2:

alertDialog.show()

inorder to view the dialog.



回答3:

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

this means in menifest package name : com.example.SpinnerExample

and

activity name :com.example.SpinnerExample.SpinnerExampleActivity .

so remove qualified name and set ActivityName .SpinnerExample



回答4:

This often happens if you give new Dialog(context); a wrong Context. Use this when building the 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 with the class name and the XML layout path accordingly.



回答5:

first your mContext have to be equals at your activity mContext = this on your onCreate, and the Spinner must be declared in your onCreate, if this solution should work for your problem