Reputation: 2607
In activity, in the Toolbar, I have a button that needs to call a method from a fragment and update the list in that fragment. Now it has an error. Calling in activity
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.menu_sort:
ListFragment listFragment = new ListFragment();
listFragment.sortByPopularity();
break;
}
return super.onOptionsItemSelected(item);
}
Fragment code. I have found an error when Activity not attached. But nothing with context
public class ListFragment extends Fragment implements ListAdapter.ItemClickListener {
/**
* Needed
*/
RecyclerView recyclerView;
View view;
List<BasePojo.Result> list;
ListAdapter listAdapter;
public ListFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
/**
* Main Initialization
*/
view = inflater.inflate(R.layout.fragment_list, container, false);
recyclerView = view.findViewById(R.id.recycler_list_detailed);
recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 2));
list = new ArrayList<>();
listAdapter = new ListAdapter(list, setOnItemClickCallback());
recyclerView.setAdapter(listAdapter);
RetrofitClient.getApiService().getPhotosList(getString(R.string.api_key)).enqueue(new Callback<BasePojo>() {
@Override
public void onResponse(Call<BasePojo> call, Response<BasePojo> response) {
BasePojo basePojo = response.body();
list.addAll(basePojo.getResults());
recyclerView.getAdapter().notifyDataSetChanged();
}
@Override
public void onFailure(Call<BasePojo> call, Throwable t) {
Log.d("tag", "Response failed" + t.toString());
}
});
return view;
}
@Override
public void onItemClick(View view, int position) {
Log.v("in on click", "value " + position);
}
private OnItemClickListener.OnItemClickCallback setOnItemClickCallback() {
OnItemClickListener.OnItemClickCallback onItemClickCallback = new OnItemClickListener.OnItemClickCallback() {
@Override
public void onItemClicked(View view, int position) {
BasePojo.Result itemClicked = list.get(position);
Bundle bundle = new Bundle();
bundle.putString("title", itemClicked.getOriginalTitle());
bundle.putString("overview", itemClicked.getOverview());
bundle.putString("release_date", itemClicked.getReleaseDate());
bundle.putString("vote_average", itemClicked.getVoteAverage().toString());
bundle.putString("poster_path", itemClicked.getPosterPath());
DetailedFragment detailedFragment = new DetailedFragment();
detailedFragment.setArguments(bundle);
FragmentManager manager = getActivity().getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.main_frame_list, detailedFragment);
Log.d("tag", "title is 111 " + bundle.get("title"));
transaction.commit();
}
};
return onItemClickCallback;
}
@Override
public void onAttachFragment(Fragment childFragment) {
super.onAttachFragment(childFragment);
}
public void sortByPopularity() {
RetrofitClient.getApiService().getPopularList(getString(R.string.api_key)).enqueue(new Callback<BasePojo>() {
@Override
public void onResponse(Call<BasePojo> call, Response<BasePojo> response) {
BasePojo basePojo = response.body();
list.addAll(basePojo.getResults());
recyclerView.getAdapter().notifyDataSetChanged();
}
@Override
public void onFailure(Call<BasePojo> call, Throwable t) {
Log.d("tag", "Response failed" + t.toString());
}
}); }
}
And here is an error
05-09 12:48:26.915 5775-5775/com.borisruzanov.popularmovies E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.borisruzanov.popularmovies, PID: 5775
java.lang.IllegalStateException: Fragment ListFragment{6dbd6de} not attached to a context.
at android.support.v4.app.Fragment.requireContext(Fragment.java:614)
at android.support.v4.app.Fragment.getResources(Fragment.java:678)
at android.support.v4.app.Fragment.getString(Fragment.java:700)
at com.borisruzanov.popularmovies.ListFragment.sortByPopularity(ListFragment.java:110)
at com.borisruzanov.popularmovies.MainActivity.onOptionsItemSelected(MainActivity.java:47)
at android.app.Activity.onMenuItemSelected(Activity.java:3204)
at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:407)
at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:195)
at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:108)
at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:108)
at android.support.v7.app.ToolbarActionBar$2.onMenuItemClick(ToolbarActionBar.java:63)
at android.support.v7.widget.Toolbar$1.onMenuItemClick(Toolbar.java:203)
at android.support.v7.widget.ActionMenuView$MenuBuilderCallback.onMenuItemSelected(ActionMenuView.java:780)
at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:822)
at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:171)
at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:973)
at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:963)
at android.support.v7.widget.ActionMenuView.invokeItem(ActionMenuView.java:624)
at android.support.v7.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:150)
at android.view.View.performClick(View.java:5610)
at android.view.View$PerformClick.run(View.java:22265)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
Thank you very much for your time and help. If my question looks not well, please make a note and I will learn how to ask questions better.
Upvotes: 106
Views: 177214
Reputation: 11
if (getActivity() != null){
textkol_PC.setBackground(
requireActivity().getResources()
.getDrawable(R.drawable.background_close, null)
);
}
Upvotes: 1
Reputation: 599
before creating and swapping fragments check savedInstanceState and return
if (savedInstanceState != null) {
return;
}
source: https://tech.auct.eu/solved-fragment-not-attached-to-a-context/
Upvotes: 0
Reputation: 658
In my case I had this problem when setting a textview's color using textview.setColor(ContextCompat.getColor(context,ColorRes(int)))
Using binding.root.context
as a context solved the problem for me.
Upvotes: 1
Reputation: 301
AVOID using getActivity(), getContext() USE activity.getresources().getString("string") instead INITIALIZE activity instance in TWO places:
if(context instance of Activity) this.activity = (activity)context;
if(context instance of Activity) this.activity = (activity)context;
Upvotes: 1
Reputation: 417
For everybody who still have this error:
private Context mContext;
@Override
public void onAttach(Context context) {
super.onAttach(activity);
mContext = context;
}
And do not add mContext = null
in onDetach
method 'cause you will still get this error.
Upvotes: 5
Reputation: 116
As Tam Huynh said, this crash happens when we our fragment is not attached. I had the same problem that your (but with a bottomSheet) and now it works fine.
We can receive this kind of error from two reasons:
requireContext()
can crash directly if the context is nullgetString(R.string.xxx_xxx_xxx)
from fragment will crash if the fragment is detached (because we will need the context and the context is null).For me, with that piece of code we can check if our fragment is attached or not, and with that I solve the crash.
fun checkIfFragmentAttached(operation: Context.() -> Unit) {
if (isAdded && context != null) {
operation(requireContext())
}
}
More info => https://weidianhuang.medium.com/android-fragment-not-attached-to-a-context-24d00fac4f3d
Upvotes: 2
Reputation: 215
For kotlin developers
lifecycleScope.launchWhenResumed {
// do your work here
}
Upvotes: 4
Reputation: 28845
If a fragment is not shown (not added) or is removed, it's context == null
. In this case getting resources will lead to this exception. getString(R.string.some_string)
requires context
and crashes.
You can check whether the fragment exists so:
if (isAdded) {
// Print getString(R.string.some_string).
}
But you might need to print the string even when the fragment was released, for instance, in LogCat, analytics or send a request to a server. In this case you need an application context to obtain a string resource.
class MyApplication : Application() {
override fun onCreate() {
super.onCreate()
instance = this
}
companion object {
lateinit var instance: MyApplication private set
}
}
object Strings {
fun get(@StringRes stringRes: Int, vararg formatArgs: Any = emptyArray()): String {
return instance.getString(stringRes, *formatArgs)
}
}
Then set MyApplication
in AndroidManifest and use: Strings.get(R.string.some_string)
.
Upvotes: 9
Reputation: 10504
This assertion can occur anywhere you have a requireContext()
call to get access to the Android context from your Fragment. Review the call site carefully, before you use requireContext()
. I only use requireContext()
when I'm certain that the fragment is going to be attached to the Activity at the time or the use case is so essential that it is better to crash with this assertion than any other course of action.
If for any reason the fragment could happen to be unattached and you can handle it yourself at the call site by avoiding or early returning, then the better idea is to null check the return from getContext()
and only then proceed forward.
Typical Kotlin code for the null check looks like this:
fun myFragmentFunction(){
val context = getContext() ?: return // early return using Elvis operator
context.whatever() // guaranteed non-null context at this point
}
Upvotes: 3
Reputation: 370
In this scenario validate if you don't have any class level properties which are dependent on context as the fragment is not committed it won't have the context and we might end up with this exception.
Upvotes: 0
Reputation: 2179
If you are using CountDownTimer, you may get that error cause of detaching the fragment before finishing the timer. If you are performing ui changes in onFinish callback, you should check the context that it is null or not like below;
timer = object : CountDownTimer(startTimeInMillis, 1000) {
override fun onTick(millisUntilFinished: Long) {
}
override fun onFinish() {
context?.let {
//perform ui changes here
}
}
}
timer?.start()
or you should cancel the timer before detaching fragment like below;
override fun onDestroy() {
super.onDestroy()
timer?.cancel()
}
Upvotes: 16
Reputation: 4074
Kotlin : Use Lazy Initialisation
override val contentMessage by lazy {
getString(R.string.message)
}
Upvotes: 3
Reputation: 3804
Kotlin:
My problem happened with getString()
Changing it to context.getString()
solved it
Upvotes: 21
Reputation: 3564
In my case, this problem occurred when I was calling getString()
changing this calls to getActivity().getString()
solved the problem.
Upvotes: 124
Reputation: 31
I know this is an old post, but I just figured out what you could do. It is true that creating a fragment instance is not enough, It needs to be attached to Activity through a transaction. However, you can initially add both fragments and detach them from fragment manager. That way, they are both 'alive' in fragmentManager and you can call attach and detach on those fragments later as you wish.
i.e
.add(container, fragment1).detach(fragment1).add(container, fragment2).commit();
.
.
.
.
.
ft.detach(fragment2)
ft.attach(fragment1
Upvotes: 2
Reputation: 2487
Create a fragment instance is not enough.
It needs to be attached to Activity through a transaction:
getFragmentManager()
.beginTransaction()
.replace(R.id.container_layout, fragment)
.commit();
After a successful commit, onAttach
method in the fragment is called, the view is created and then you can interact with its views.
In your case, create the fragment instance and attach it in activity onCreate
, then call sortByPopularity
later in a click event.
Read more about fragment life cycle: https://developer.android.com/guide/components/fragments
Upvotes: 25
Reputation: 648
Using commit()
can not solve the problem, we should try to find the solution in the source code of Fragment.
So, consider from the error stack you provided, the requireContext()
in Fragment was:
public final Context requireContext() {
Context context = getContext();
if (context == null) {
throw new IllegalStateException("Fragment " + this + " not attached to a context.");
}
return context;
}
This means the system will check the Context
from getContext()
, if it's null, the exception will be thrown.
So, to avoid this problem, we can check the result of getContext()
before do our business.
Upvotes: 32