gpt4 book ai didi

java - ArrayAdapter 中的 createFromResources() 不能应用于微调器

转载 作者:行者123 更新时间:2023-11-30 01:03:53 26 4
gpt4 key购买 nike

我在微调器上创建 ArrayAdapter 时遇到问题。我不知道如何向你们解释,但人们说一张图片能表达 1000 个单词。所以我提供了我的问题的图片。请帮助我,我真的不知道该怎么做。

this is my error

这是我的类(class)

public class SettingFragment extends Fragment{

private View rootView;
Spinner spinner;
ArrayAdapter<CharSequence> adapter;

public SettingFragment(){
//required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_setting, container, false);

spinner = (Spinner)rootView.findViewById(R.id.spinner);
adapter = ArrayAdapter.createFromResource(this, R.array.select_font_size, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);

return rootView;
}

这是我的 string.xml

<string-array name="select_font_size">
<item>Small</item>
<item>Default</item>
<item>Large</item>
<item>Larger</item>
</string-array>

这是我的xml代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">


<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/spinner"
android:layout_gravity="center_horizontal" />

最佳答案

我建议将适配器的创建移动到 onActivityCreated 回调中,当您确定通过调用 getActivity() 时您将获得一个非空实例 Activity 的:

public class SettingFragment extends Fragment{

private View rootView;
Spinner spinner;
ArrayAdapter<CharSequence> adapter;

public SettingFragment(){
//required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_setting, container, false);
spinner = (Spinner)rootView.findViewById(R.id.spinner);

return rootView;
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
adapter = ArrayAdapter.createFromResource(getActivity(), R.array.select_font_size, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
}
}

关于java - ArrayAdapter 中的 createFromResources() 不能应用于微调器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39087053/

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