getView returning null when fragment has been created from an activity
Move your method call to be executed during onCreateView
, and use the view you are inflating for reference instead of getView()
. See the fragment lifecycle for more information: https://developer.android.com/guide/components/fragments.html#Creating
and the documentation of getView()
that explains why it returns null before onCreateView(LayoutInflater, ViewGroup, Bundle)
returns:
getView()
Get the root view for the fragment's layout (the one returned by onCreateView(LayoutInflater, ViewGroup, Bundle)), if provided.
https://developer.android.com/reference/android/app/Fragment.html#getView()
fragment.getview() always return null when called in an activity
As mentioned by Visamanath you dont add your fragment to activity, you just create it.
To add fragment to activity you can do it by implement it as a View in xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<fragment android:name="com.example.android.fragments.NewBooksFrag"
android:id="@+id/new_books_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Or by adding this dynamically in the code:
NewBooksFrag frag = new NewBooksFrag();
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, frag ).commit();
For more deatils please visit official guide
getView() returns null in second activity
I'm calling getView().findViewByID(R.id.theTextViewIWant), but it is always returning null, and the IDE says it cannot resolve getView()
I recommend to you to read getView()
method documentation, but there's a small explanation
Get a View that displays the data at the specified position in the data set.
So if you want to find a view that's not on a data set, for instance in your example TextView
then you want to use findViewById(int)
Finds a view that was identified by the android:id XML attribute that was processed in onCreate(Bundle).
As it says in the documentation I recommend you to put it on onCreate()
method, instead of onStart()
.
Then you have to remove the getView()
and do it like :
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.yourLayoutFromActivity2);
TextView textViewBox = (TextView) findViewById(R.id.textView2);
}
Note : if you put your stuff in onStart()
it means that every-time the app come from foreground will execute all of you have inside of it. I also recommend to you to take a look at Android life cycle
Android Get View In Fragment Returns Null
The simplest way, not the safest:
You can access hosting activity in fragment B by
HostActivity activity =(HostActivity) getActivity();
activity.callOtherFragment();
In that activity, you can access fragment A, by
public void callOtherFragment() {
YourFragment A = (YourFragment)getFragmentManager().findFragmentById(R.id.fragmentA);
A.showSomeStuff();
}
then implement your method in fragment A:
public void showSomeStuff() {
shadowLine.setVisibility(View.VISIBLE);
}
Fragment getView() always returning null for Fragments created by a FragmentStatePagerAdapter
So I need to get the current fragment imageview but I can't because
fragment.getView() always is null, the activity associated with the
fragment is null too and I can't figure out why is it.
That is happening because you're expecting _iva.getItem(index);
to return the Fragment
that the ViewPager
uses for the page corresponding to the specified index. That will not happen as the ViewPager
has already called the getItem
method to get the fragments it needs and after your call the getItem
method you get a new ImageViewURLFragment
instance. This new instance isn't tied to the Activity
(getActivity()
returns null
) and its view wasn't created.
As you use a FragmentStatePagerAdapter
try the code below to get the currently visible Fragment
:
if (item.getItemId()==R.id.saveToSD) {
int index = _vp.getCurrentItem();
Fragment fragment = _vp.getAdapter().instantiateItem(_vp, index);
//...
Fragment.getView() always return null
If I were you, I would use the fragments' onCreateView()
to bind the views, then let the parent Activity
know about the views through an Interface in onActivityCreated()
.
Your interface could look like
public interface ViewInterface {
void onLinearLayoutCreated(LinearLayout layout);
void onRelativeLayoutCreated(RelativeLayout layout);
}
and then in each fragment
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewGroup layout = (ViewGroup) inflater.inflate(R.layout.fragment_layout, inflater, false);
mLinearLayout = layout.findViewById(R.id.linear_layout);
...
return layout;
}
...
public void onActivityCreated (Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
try {
ViewInterface callback = (ViewInterface) getActivity();
callback.onLinearLayoutCreated(mLinearLayout);
} catch (ClassCastException e) {
Log.e("ERROR", getActivity().getName()+" must implement ViewInterface");
}
...
}
and then in your parent Activity
that implements ViewInterface
void onLinearLayoutCreated(LinearLayout layout) {
//do something with LinearLayout
...
}
getView() returns null
Depending on the implementation of your PagerAdapter, it is possible that a Fragment no longer in view will have their View
destroyed in order to free up resources. As such, there's no need to update the View
because it will be created again in onCreateView()
with the updated information. getView()
will return a view in between calls to onCreateView()
and onDestroyView()
. Outside of that, the Fragment
can still exist in memory, but not attached to any View
, thus getView()
will return null
.
So basically,
View fragmentView = getView();
if(fragmentView != null) {
// we are in view or at least exist. Update the elements.
}
// Else don't worry about it. Just update the data.
Related Topics
Can't Create Handler Inside Thread Which Has Not Called Looper.Prepare()
How to Handle Cookies in Httpurlconnection Using Cookiemanager
Didn't Find Class "Com.Google.Firebase.Provider.Firebaseinitprovider"
Android: Using Findviewbyid() with a String/In a Loop
Selecting Multiple Items in Listview
Cannot Get Folderid That I Just Created on Google Drive
Android 4.3 Bluetooth Low Energy Unstable
What's "Tools:Context" in Android Layout Files
How to Get the APK of an Installed App Without Root Access
Listview Scroll to the End of the List After Updating the List
Jetpack Compose - Order of Modifiers
Does Alarm Manager Persist Even After Reboot
How to Start Android Service on Installation
Using Asynctask to Load Images in Listview
Using Gson to Parse a JSON with Dynamic "Key" and "Value" in Android
Adjustpan Not Preventing Keyboard from Covering Edittext