与微调在活动开始AlertDialogAlertDialog

2023-09-06 21:51:46 作者:年少就是不服输

我试图用在一个活动的开始微调创造一个AlertDialog。我活动的onCreate()方法中的以下code。

I am trying to create an AlertDialog with a spinner on the start of an activity. I have the following code within the activity's onCreate() method.

  AlertDialog.Builder builder = new AlertDialog.Builder(this);
 AlertDialog alertDialog;
 Context mContext = getApplicationContext();
 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
 View layout = inflater.inflate(R.layout.custom_dialog,
                                (ViewGroup) findViewById(R.id.layout_root));

 Spinner spinner = (Spinner) layout.findViewById(R.id.spinner);
 ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
         this, R.array.num_players_array, android.R.layout.simple_spinner_item);
 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
 spinner.setAdapter(adapter);

 builder = new AlertDialog.Builder(mContext);
 alertDialog = builder.create();
 alertDialog.show();

这力量逢时。我已经成功地创建了一个简单AlertDialog上的活动开始通过以下code:

This force closes every time. I have successfully created a simple AlertDialog on the start of an activity by using the following code:

 AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Stackoverflow!").create().show();

我将不胜AP preciate,如果有人可以点我在正确的方向。

I would greatly appreciate it if someone could point me in the right direction.

推荐答案

您可以尝试 AlertDialog.Builder 取值的setView()的方法来设置你创建的查看 布局作为对话的看法。

You could try AlertDialog.Builders setView() method to set your created View layout as the dialog's view.

builder.setView(layout);

在任何情况下,它可能是有益的发布亚行logcat 的输出,以找出异常崩溃您的应用程序。

In any case, it might be helpful to post the output of adb logcat to find out what exception is crashing your app.