Android 对话框中的微调器-NullPointerException

Android 对话框中的微调器-NullPointerException,android,dialog,spinner,Android,Dialog,Spinner,我想用微调器显示自定义对话框。奇怪的是,当我试图设置微调器的适配器时,我得到了NullPointerException Dialog dialog = new Dialog(this.getApplicationContext()); dialog.setContentView(R.layout.dialog_spinner); ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this, an

我想用微调器显示自定义对话框。奇怪的是,当我试图设置微调器的适配器时,我得到了NullPointerException

Dialog dialog = new Dialog(this.getApplicationContext());
dialog.setContentView(R.layout.dialog_spinner);

ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, new String[] {"0","1","2"});

spin = (Spinner)dialog.findViewById(R.id.spinQ);
//What am I doing wrong here?
spin.setAdapter(spinnerAdapter);

dialog.setTitle("Questions");
dialog.show();
Dialog Dialog=新建对话框(this.getApplicationContext());
setContentView(R.layout.dialog\u微调器);
ArrayAdapter spinnerAdapter=新的ArrayAdapter(这个,android.R.layout.simple_spinner_项,新字符串[]{“0”、“1”、“2”});
spin=(微调器)dialog.findviewbyd(R.id.spinQ);
//我做错了什么?
spin.setAdapter(spinnerAdapter);
对话框。设置标题(“问题”);
dialog.show();
xml布局代码:

<?xml version="1.0" encoding="utf-8"?>  

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:paddingLeft="10dip"
>

<Spinner 
android:id="@+id/spinQ" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content"
/> 


</LinearLayout>

更新:

        AlertDialog alertDialog;


        LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.dialog_spinner,
                                       (ViewGroup) findViewById(R.id.root));

        ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this,
              android.R.layout.simple_spinner_item, new String[] {"0","1","2"});
        spin = (Spinner) findViewById(R.id.spinQ);
        //I get the error in the following line:
        spin.setAdapter(spinnerAdapter);

        builder = new AlertDialog.Builder(mContext);
        builder.setView(layout);
        alertDialog = builder.create();
        alertDialog.show();
AlertDialog-AlertDialog;
LayoutFlater充气器=(LayoutFlater)getApplicationContext().getSystemService(布局充气器服务);
视图布局=充气机。充气(R.layout.dialog\u微调器,
(ViewGroup)findViewById(R.id.root));
ArrayAdapter spinnerAdapter=新的ArrayAdapter(此,
android.R.layout.simple_微调器_项,新字符串[]{“0”、“1”、“2”});
spin=(微调器)findviewbyd(R.id.spinQ);
//我在以下行中得到错误:
spin.setAdapter(spinnerAdapter);
builder=新建AlertDialog.builder(mContext);
builder.setView(布局);
alertDialog=builder.create();
alertDialog.show();

您的
微调器可能尚未充气。如果要操纵视图,请自行充气,然后在充气的
视图上使用
setContentView
。请参见有关创建对话框的说明

更新:

        AlertDialog alertDialog;


        LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.dialog_spinner,
                                       (ViewGroup) findViewById(R.id.root));

        ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this,
              android.R.layout.simple_spinner_item, new String[] {"0","1","2"});
        spin = (Spinner) findViewById(R.id.spinQ);
        //I get the error in the following line:
        spin.setAdapter(spinnerAdapter);

        builder = new AlertDialog.Builder(mContext);
        builder.setView(layout);
        alertDialog = builder.create();
        alertDialog.show();
在新代码中,更改:

spin = (Spinner) findViewById(R.id.spinQ);
致:


这个教程怎么样?对话的工作方式不同于活动,尤其是在其生命周期方面。本教程使用了视图已膨胀的活动onCreate中的
微调器。您可能可以执行类似的操作,并在微调器的一个回调(可能是onAttachedToWindow?)中创建/设置适配器,但我不能说我对这些太熟悉。我在您的链接中尝试了这个示例,使用LayoutInflater的示例(最后一个示例),但在相同的位置“spin.setAdapter(…)”仍然会出现相同的错误你是说你放大了视图,你得到了
null
返回?可能会更新您的帖子,并用膨胀的代码追加新代码。好的,这解决了nullpointerexception的问题,但现在我发现alertDialog.show()出现错误“令牌null不适用于应用程序”。。如何显示此警报对话框?