我正在尝试在活动开始时使用微调器创建一个AlertDialog。我在activity的onCreate()方法中有以下代码。
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:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Stackoverflow!").create().show();
如果有人能指出我正确的方向,我将不胜感激。
答案 0 :(得分:0)
您可以尝试使用AlertDialog.Builder
setView()
方法将创建的View
layout
设置为对话框的视图。
builder.setView(layout);
在任何情况下,发布adb logcat
的输出以找出导致您的应用崩溃的异常可能会有所帮助。