gpt4 book ai didi

android - 在 DialogFragment IllegalStateException 中膨胀 RecyclerView

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:19:59 25 4
gpt4 key购买 nike

我需要在对话框中放置回收器 View ,所以我这样做:

public class RecyclerTemperatureFragment extends DialogFragment
{
String[] items = {getString(R.string.kt),getString(R.string.mph),getString(R.string.kmh)};
RecyclerView recyclerView;
RecyclerTemperatureAdapter adapter;

public static RecyclerTemperatureFragment newInstance()
{
Bundle args = new Bundle();

RecyclerTemperatureFragment fragment = new RecyclerTemperatureFragment();
fragment.setArguments(args);
return fragment;
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
Dialog dialog = new Dialog(getActivity());
return dialog;
}

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

recyclerView = (RecyclerView)rootView.findViewById(R.id.recTemperature);
adapter = new RecyclerTemperatureAdapter(getActivity(),items);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.addItemDecoration(new SimpleDividerItemDecoration(getActivity()));
recyclerView.setAdapter(adapter);

getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);

return rootView;
}
}

在我的按钮的 OnClickListener 事件中,我这样做:

  public class TrueAirSpeedFragment extends Fragment
{
TextView casTW, paTW, satTW, ins, clc,casUM,paUM,satUM;
ImageView calculate;
EditText casET, paET, satET;

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
//this works
}
private void setUpCASUM(final TextView textview)
{
RecyclerTemperatureFragment dialog = RecyclerTemperatureFragment.newInstance();
FragmentManager manager = getFragmentManager();
dialog.show(manager,"DTAG");
}

但是当我启动它时出现这个错误:

 java.lang.IllegalStateException: Fragment RecyclerTemperatureFragment{b4d74cd} not attached to Activity
at android.app.Fragment.getResources(Fragment.java:819)
at android.app.Fragment.getString(Fragment.java:841)

我该如何解决这个问题?我遵循了网络上的教程,但在视频中这段代码有效,但在我的项目中却崩溃了。

谢谢

最佳答案

这一行可能会出错,因为要获取资源,您需要上下文:

String[] items = {getString(R.string.kt),getString(R.string.mph),getString(R.string.kmh)};

尝试在 onCreate 中启动项目

关于android - 在 DialogFragment IllegalStateException 中膨胀 RecyclerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40424143/

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