gpt4 book ai didi

java - 无法对基本类型 int 调用 findViewById(int)

转载 作者:行者123 更新时间:2023-12-02 05:33:08 24 4
gpt4 key购买 nike

我想从 Android 应用程序中的微调器中获取选定的值。

我尝试如下操作,因为我的微调器位于弹出对话框中

final Spinner problems = (Spinner) layout.input_dialog.findViewById((R.id.spinner_problems));
problems.setOnItemSelectedListener(new OnItemSelectedListener(){

@Override
public void onItemSelected(AdapterView<?> adapter, View view1, int pos, long id)
{
spin_problem_val = adapter.getItemAtPosition(pos).toString();
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub

}
});

声明final Spinner issues = (Spinner)layout.input_dialog.findViewById((R.id.spinner_problems));给我错误Cannot调用原始类型int上的findViewById(int)

为什么会这样?

如果我像这样尝试final Spinner issues = (Spinner) findViewById((R.id.spinner_problems));

我会得到NullPointerException

input_dialog 是我的 input_dialog.xml 对话框弹出窗口,我的微调器位于

<Spinner
android:id="@+id/spinner_problems"
android:entries="@array/problems"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>

最佳答案

请确保在设置布局之后运行此代码。当您在加载布局之前尝试访问 View 时,通常会发生此错误。

请检查此代码是否在您的 onCreateView 函数设置布局之后运行。

关于java - 无法对基本类型 int 调用 findViewById(int),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25322068/

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