gpt4 book ai didi

android - 对话框中的微调器 - NullPointerException

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:55:26 26 4
gpt4 key购买 nike

我想用微调器显示自定义对话框。奇怪的是,当我尝试设置微调器的适配器时出现 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();

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();

最佳答案

您的 Spinner 可能还没有充气。如果您想操纵 View ,请自己对其进行膨胀,然后在膨胀的 View 上使用 setContentView。查看docs关于创建对话框。

更新:

在您的新代码中,更改:

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

到:

spin = (Spinner) layout.findViewById(R.id.spinQ);

关于android - 对话框中的微调器 - NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6365268/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com