NullPointerException in Fragments onAttach for findViewById - android

So I have a curious problem I'm not able to solve right now.
RoomGamesFragment
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
roomActivity = (RoomActivity) activity;
gameTabContainerView = (LinearLayout) roomActivity.findViewById(R.id.game_tab_container); // findViewById returns null
// NullPointerException
gameTabContainerView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
This works totally fine when opening the activity which contains the fragment. But here is the problem: If I leave the activity open, put the app in the background (by clicking the home button), use other apps and then open my app again after some time, I get the NullPointerException, because findViewById returns null now.
How can I prevent this? Is the Activity removed from the stack, which leads to the Exception? I know I could just check for null, but I need the onClickListener, even when I return to the app after it has been in the background.

You should not perform this code in onAttach() but rather in onActivityCreated(). This is because the "View" is not yet created. onAttach() is above the onCreateView() in the Fragment lifecycle.
For more info : http://developer.android.com/guide/components/fragments.html#Creating

Related

java.lang.IllegalStateException: Fragment not attached to Activity in Android

I am working on an application in which I have multiple Fragments inside my Activity but the problem is that sometimes on "BackPress" my application got crashed and it shows me error i.e. "java.lang.IllegalStateException: Fragment not attached to Activity in Android" . And my logcat redirect me to Toast i.e.
Code
catch (Exception e) {
Toast.makeText(getActivity(), R.string.some_error_occured, Toast.LENGTH_LONG).show();
e.printStackTrace();
}
Have I done something wrong with Toast?
if you have a viewpager in your fragments then you need to add in your viewpager adapter.
#Override
public Parcelable saveState() {
return null;
}
Check back stack count and remove all active fragments then call parent class's onBackPressed() method.
override fun onBackPressed() {
supportFragmentManager.beginTransaction().remove(fragment)
super.onBackPressed()
}
hey check if you are attached to activity or not then make context related calls like getString which you are doing in Toast. so move your code inside
isAdded() : Return true if the fragment is currently added to its
activity.
if (isAdded()){
//your code goes here
} else {
//handle the case
}
docs
It's crashing because when you are pressing back button that time your activity is not attached to the view and if you want to show toast message then you need an instance of that activity.
Try this, to check fragment is attached to the activity
Activity activity = getActivity();
if(activity! = null && isAdded){
Toast.makeText(getActivity, "Show message", Toast.LENGTH_SHORT).show();
}
The answer is very simple.
Your fragment is not getting proper context refrence you should do like this it will never force stop.
Take reference of your activity in which fragments are integrated.
For example, your fragment is lying under MainActivity so you should code like this
MainActivity mainactivity;
#Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
// use this mainactivity object instead of getActivity() or getContext() or requireContext() or requireActivity()
mainactivity = (MainActivity) getActivity();
}
#D Developer
I guarantee, your app will work smoothly without any single error.

When is fragment finally attached to activity?

I have a main fragment with a viewpager inside it. This viewpager has 2 pages (list fragments). When I start the activty, the main fragment is shown and I also show the first paged fragment. This paged fragment displays data from a db using AsyncTask.
In the main fragment I have:
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
onPageSelected(0);
}
#Override
public void onPageSelected(int position) {
Fragment fragment = (Fragment) pagerAdapter.instantiateItem(viewPager, position);
if (fragment instanceof IPagedFragment) {
((IPagedFragment) fragment).onShown(getActivity());
}
}
And the interface is:
public interface IPagedFragment {
void onShown(FragmentActivity activity);
}
The first issue I have is that I have to pass the activity as a parameter because when onShown gets called, the activity is still null.
Furthermore, the paged fragments use progressbar logic similar to the LoginActivity sample. I also get the following exception:
IllegalStateException: Fragment PagedFragment1{4201f758} not attached to Activity
at android.support.v4.app.Fragment.getResources(Fragment.java:620)
So what is the correct stage to start retrieving data from db once the paged fragment is fully available to the UI?
Issues like yours is the reason some developers are starting to question if fragments are really that good or useful.
Also "the correct" is debatable as you can do it in a variety of places and different developers will give you different answers, But let me try to supply you some useful info.
The attach/detach callbacks:
public void onAttach(Activity activity);
public void onDetach();
between those two methods any call to getActivity() will return the non-null activity the fragments is connected to. You can override them and use a private boolean isAttached to keep track of that call.
Also useful is the:
public void onActivityCreated (Bundle savedInstanceState)
this method is called AFTER the Activity.onCreate method. That is very important if you rely on some initialisation that happened there.
Also it's important to remember that on the moment the fragment transaction happens, the Fragment.onCreate happens after the Activity.onCreate and during rotation it happens before it.
As a general rule of thumb I use the Fragment.onStart() / Fragment.onStop() for getting/listening to data. On those calls, all the UI have been created, the fragment is attached to the activity and those callbacks don't get called if there's a dialog/popup (pause/resume does)
From the documentation:
public void onActivityCreated (Bundle savedInstanceState)
[...] tells the fragment when it is fully associated with the new activity instance.
source: http://developer.android.com/reference/android/app/Fragment.html#onActivityCreated(android.os.Bundle)
To get the reference of your activity, create a local object of fragmentActivity and get your activity reference as shown below.
private FragmentActivity fragmentActivity;
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
fragmentActivity=activity;
}

Flurry context in a Fragment

I need to use Flurry within a Fragment in my Android app.
I insert the following code in onStart():
#Override
public void onStart() {
super.onStart();
//Log.i("About get activity","About get activity "+getActivity().hashCode());
FlurryAgent.onStartSession(getActivity(), "WXXXXXXXX");
}
and in on stop:
#Override
public void onStop() {
FlurryAgent.onEndSession(getActivity());
super.onStop();
}
Is this code correct? Do I pass the context as getActivity(), this or something else?
That's correct, you could also use:
getActivity().getApplicationContext();
which is the context for the entire application and not specific to that particular Activity.
As a side note, if it happens to you to get some weird crashes, specially when you press quickly the back button removing all the fragment in your back stack, it may be that getActivity() is returning null.

Android: NullPointerException in dialog when rotating

Sorry if this been asked before but I couldn't find an answer to my specific case. Also sorry that I'm new and a little stupid.
Problem:
I'm showing a dialog from a fragment and passing along a context in my constructor method because I need a context in my dialog to register for broadcastrecievers etc.
DialogFragment fragmentDialog = MyDialog.myConstructor(getActivity());
fragmentDialog.show(getFragmentManager(), "dialog");
Then in MyDialog class I store the context in a instance variable.
The problem arises when rotating the device and I get a nullPointerException when I try to use the context again in the dialog.
Can this be solved in some easy way?
If the device is rotated the Activity will be destroyed and recreated. So the Context you passed to your Fragment points on the Activity which was destroyed.
You could use setRetainInstance(true) in your Fragment. This way your Fragment will survive the recreation of the Activity.
To solve the NPE you have to pass the Context to the Fragment, if the Activity is recreated. Then the Context belongs to the new Activity.
In fact, without this update every line of code which points on the Activity like getActivity() or getFragmentManager() will lead in a NPE.
You get the NullPointerException because activites are destroyed and recreated when rotating the screen.
The SO post below gives more info...
https://stackoverflow.com/a/1673374/
Please be careful with the order of events if you rotate a FragmentActivity, because this can also be a source of NullPointerExceptions.
This is not documentated:
When the FragmentActivity is created the first time,
public class MyActivity extends FragmentActivity implements
MyFragment.OnFragmentInteractionListener {
private int var1;
private int var2;
#Override
protected void onCreate(Bundle savedInstanceState) {
//before
var1 = 3;
super.onCreate(Bundle savedInstanceState)
//after
var2 = 5;
}
//Interface Methods
public int getVar1() { return var1; }
public int getVar2() { return var2; }
}
both of the [before] and [after] code will be run before the fragments are attached and created. So, if you get the vars in the onCreate() call of the Fragment you get both vars. But when you rotate your device, the Activity is recreated from the savedInstanceState in the super call. Now, the fragments are reattached and created anew in this call! That means, this time the Methods of the Listener Interface are called before your [after] code. So, if you pass the Context of the activity to the fragment and get Information through the Interface like it is shown in: https://developer.android.com/training/basics/fragments/communicating.html
you get a NullPointerException for var2 because the interface methods are called from the fragments onCreate() onAttach() ... functions before the [after] code in the Activity's onCreate() is executed! So, take care that you set your Information the InterfaceFunctions are accessing before the super call.
Depending on what you're doing in your initialization you could consider creating a new class that extends Application and moving your initialization code into an overwridden onCreate method within that class.
public class MyApplicationClass extends Application {
#Override
public void onCreate() {
super.onCreate();
// TODO Put your application initialization code here.
}
}
And you are not stupid, even experts need help from time to time.

Activity group does not refresh activities when back button is clicked

I have a tabhost on my application and I'm using an Activity group which handles 3 activities inside.
Example:
ActivityGroup Handles
A -> B -> C
When i start this activities i'm using the flag Intent.FLAG_ACTIVITY_CLEAR_TOP.
My problem is when the user goes from A->B->C and press back button, my B activity shows up, but it does not resume or reload or refresh. It has the same state as before.
For example if the user goes again to C, C is refreshed, but when from C goes back.... B is not.
On B I have implementend methods such as onResume, onStart, onReestart and debugging it the main thread never goes in there...
And i need to refresh B because C can make changes that change the content displayed on B.
I have googleled this for 3 days and I couldn't found a solution..
I had this problem too.
I was using ActivityGroup code based on this blog post.
When I pressed the back button the pervious View would load fine, but the activity associated with it would not fire the onResume().
I was using an extended activity with on overridden and public onResume().
I found this blog post, so tried casting the view as my extended activity and called onResume().
Bingo.
Edit.... here's some more detail...
public class YFIMenuListActivity extends ListActivity {
....
#Override
public void onResume() {
super.onResume();
}
....
}
onResume() is normally protected, but I override it and make it public so that my ActivityGroup can call it.
I only have extended list activities in this activity group (I was just playing around). If you have different activities, each will have to override onResume() and I guess you'd have to look at the type of context you got back from v.getContext() before casting and calling it.
My ActivityGroup looks something like this:
public class BrowseGroup extends ActivityGroup {
.....
#Override
protected void onResume() {
super.onResume();
// call current activity's onResume()
View v = history.get(history.size()-1);
YFIMenuListActivity currentActivity = (YFIMenuListActivity)v.getContext();
currentActivity.onResume();
}
....
}
I've managed to implement an expanded version of cousin_itt's approach.
In both of my activities being used within the activity group I changed onResume from :
protected void onResume()
to
public void onResume()
I then wrote the following onResume function in my ActivityGroup to manually fire off onResumes:
#Override
protected void onResume() {
super.onResume();
View v = history.get(history.size()-1);
MainPeopleView currentActivity = null;
try {
currentActivity = (MainPeopleView)v.getContext();
currentActivity.onResume();
}
catch ( ClassCastException e ) {
Log.e(TAG, e.toString());
}
ProfileView otherActivity = null;
try {
otherActivity = (ProfileView)v.getContext();
otherActivity.onResume();
}
catch ( ClassCastException e ) {
Log.e(TAG, e.toString());
}
}
I have to say, this feels like the worst android hack I've ever written. I'm never using activitygroup again.
((ReportActivity)getLocalActivityManager().getActivity("ReportActivity")).onResume();
ReportActivity is that name you want to back Activity
ps: v.getContext();
only return the ActivityGroup ,it can't invoke child Activity onResume
I have found that onFocusChanged(boolean hasFocus) is great for situations like ActivityGroup. This will fire, even if onResume() does not. I use it for a few of my apps that have TabHosts and ActivityGroups. Here you can force the refresh and insure that it always gets fired when your Activity regains the focus.
I hope you have write your refresh data code in this method onResume().

Categories

Resources