与微调在活动开始AlertDialog [英] AlertDialog with spinner on start of activity
问题描述
我试图用在一个活动的开始微调创造一个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.Builder
s 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.
这篇关于与微调在活动开始AlertDialog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!