我需要把回收視圖在一個對話框,所以我這樣做:在DialogFragment充氣RecyclerView IllegalStateException異常
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)
我該如何解決這個問題?我在網上跟着一個教程,但在視頻中這個代碼可以工作,但在我的項目中它崩潰了。
謝謝
我把它放在onCreateView上,不是很好用......謝謝! – ste9206