当前位置: 动力学知识库 > 问答 > 编程问答 >

android - Spinner in Custom Dialog giving force close

问题描述:

The Alert Dialog being shown nicely, when i click the spinner( spinner is in the Alert Dialog) it gives force close, kindly help

private void showDialogue(Activity a) {

try {

// Inflating a layout for Alert Dialog

LayoutInflater layoutInflater = (LayoutInflater) getApplicationContext()

.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View view = layoutInflater.inflate(R.layout.prop_dialogue,null);

view.findViewById(R.id.dialogue_btn_cancel).setOnClickListener(

Properties.this);

// Getting reference of Spinner

Spinner spin = (Spinner) view.findViewById(R.id.dialogue_spinner_count);

setImage((ImageView) view.findViewById(R.id.dialogue_prop_icon),

getImageBitmap(position));

setName((TextView) view.findViewById(R.id.dialogue_prop_name));

// Adapter for Spinner

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(

Properties.this, R.array.prop_count,

android.R.layout.simple_spinner_item);

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

spin.setAdapter(adapter );

spin.setOnItemSelectedListener(Properties.this);

Builder b = new AlertDialog.Builder(this).setTitle("Properties")

.setNeutralButton("Submit",

new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog,

int which) {

}

});

b.setView(view);

b.show();

} catch (Exception e) {

// TODO: handle exception

}

}

Error Shown in the Log

06-06 12:09:22.971: E/AndroidRuntime(992): FATAL EXCEPTION: main

06-06 12:09:22.971: E/AndroidRuntime(992): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application

06-06 12:09:22.971: E/AndroidRuntime(992): at android.view.ViewRoot.setView(ViewRoot.java:509)

06-06 12:09:22.971: E/AndroidRuntime(992): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)

06-06 12:09:22.971: E/AndroidRuntime(992): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)

06-06 12:09:22.971: E/AndroidRuntime(992): at android.app.Dialog.show(Dialog.java:241)

06-06 12:09:22.971: E/AndroidRuntime(992): at android.app.AlertDialog$Builder.show(AlertDialog.java:802)

06-06 12:09:22.971: E/AndroidRuntime(992): at android.widget.Spinner.performClick(Spinner.java:257)

06-06 12:09:22.971: E/AndroidRuntime(992): at android.view.View$PerformClick.run(View.java:8816)

06-06 12:09:22.971: E/AndroidRuntime(992): at android.os.Handler.handleCallback(Handler.java:587)

06-06 12:09:22.971: E/AndroidRuntime(992): at android.os.Handler.dispatchMessage(Handler.java:92)

06-06 12:09:22.971: E/AndroidRuntime(992): at android.os.Looper.loop(Looper.java:123)

06-06 12:09:22.971: E/AndroidRuntime(992): at android.app.ActivityThread.main(ActivityThread.java:4627)

06-06 12:09:22.971: E/AndroidRuntime(992): at java.lang.reflect.Method.invokeNative(Native Method)

06-06 12:09:22.971: E/AndroidRuntime(992): at java.lang.reflect.Method.invoke(Method.java:521)

06-06 12:09:22.971: E/AndroidRuntime(992): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)

06-06 12:09:22.971: E/AndroidRuntime(992): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)

06-06 12:09:22.971: E/AndroidRuntime(992): at dalvik.system.NativeStart.main(Native Method)

网友答案:
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;
   }
分享给朋友:
您可能感兴趣的文章:
随机阅读: