I have updated my Sdk Oreo 8.0 When i come my current fragment to my previous fragment its crash because i am using if (view == null)
if i removed this condition it 'll work fine but have to load again view.
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container,
#Nullable Bundle savedInstanceState) {
if (view == null) {
view = inflater.inflate(R.layout.home_fragement_layout, container, false);
ButterKnife.bind(this, view);
bundle=getArguments();
registrationResponse=(RegistrationResponse)bundle.getSerializable("registerresponse");
setadapter();
hitUserFavouriteOutfitsapi();
swipeRefreshLayout.setOnRefreshListener(this);
}
toolBarTopChangeState =((ToolBarTopChangeState)context);
toolBarTopChangeState.stateChangeToolBarIcon(0);
toolBarTopChangeState.stateChangeToolBarText(getString(R.string.app_name));
return view;
}
Use this code:
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container,
#Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.home_fragement_layout, container, false);
ButterKnife.bind(this, view);
bundle=getArguments();
registrationResponse=(RegistrationResponse)bundle.getSerializable("registerresponse");
setadapter();
hitUserFavouriteOutfitsapi();
swipeRefreshLayout.setOnRefreshListener(this);
toolBarTopChangeState =((ToolBarTopChangeState)context);
toolBarTopChangeState.stateChangeToolBarIcon(0);
toolBarTopChangeState.stateChangeToolBarText(getString(R.string.app_name));
return view;
}
On the lists is a good practice to check if the view is already created and if it's to use it further, but on the activities and fragments you should left the OS to decide when should inflate again the view. You can have a look over android lifecycle to understand better how it's works
it 'll happen because i am adding animation when fragment change and return to back.. This problem facing because of Android 8.0(Oreo) rest os working fine so I removed the animation during fragment change it 'll solved.
Related
I'm starting to learn android app development and currently practicing with simple apps. I'm writing this app that includes a listview and i want to fill it with fake data but, when i use findViewbyId() in my code the app gives me force close BUT when i set the content view to my fragment instead of activity_main i get no force close, but the app shows me an empty list.
This is my code :
package com.example.karen.please;
public class MainActivityFragment extends Fragment {
private ArrayAdapter<String> mForecastAdapter;
public MainActivityFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String[] forecastArray =
{
"sunny",
"cloudy",
"asteroids",
"rainy",
"sunny"
};
List<String> weekforecast = new ArrayList<String>(Arrays.asList(forecastArray));
mForecastAdapter =
new ArrayAdapter<String>(
getActivity(),
R.layout.list_item_forecast,
R.id.list_item_forecast_textview,
weekforecast
);
ListView mylistview = (ListView)container.findViewById(R.id.listview_forecast); //screws upp in this line
mylistview.setAdapter(mForecastAdapter);
return inflater.inflate(R.layout.fragment_main, container, true);
}}
Any help would be appreciated.
You're trying to find a view before you inflate your layout... Try inflating at the top of your method, like this:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View myView = inflater.inflate(R.layout.fragment_main, container, true);
<all the other init code you have>
return myView;
}
First you need inflate your layout...
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
...
ListView mylistview = (ListView)rootView.findViewById(R.id.listview_forecast);
...
Before using findViewById you should Create a View variable, use the inflate.. line, use findViewById on the View variable you created, and then return that.
This is what your code should look like:
package com.example.karen.please;
public class MainActivityFragment extends Fragment {
private ArrayAdapter<String> mForecastAdapter;
public MainActivityFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Instead of inflate and return instantly, store it in a variable,
// Then return at the end of the method.
View view = inflater.inflate(R.layout.fragment_main, container, true);
[...]
// Here, use findViewById on the View you have just inflated.
ListView mylistview = (ListView) view.findViewById(R.id.listview_forecast); //screws upp in this line
mylistview.setAdapter(mForecastAdapter);
// Return the view you created on the beginning.
return view;
}}
You should start with a proper tutorial not chaotically adding code that you don't understand. Start reading here:
Creating and Using Fragments
After you understood how it goes then you will see that first you need to inflate the layout that contains the stuff you want to find by id and then by using the view(that defines the layout) you can find the desired view(listview, etc).
ListView my listview = (ListView).findViewById(R.id.listview_forcast);
I think this is how it should be, no container in the code
I try to set up a grid view like in this tutorial : http://developer.android.com/guide/topics/ui/layout/gridview.html
But I want a fragment and setContentView in a fragment is not working. What I have to change to make it works ?
Thanks.
Override the onCreateView() method in your fragment like so:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedState) {
return inflater.inflate(R.layout.fragment_layout, container, false);
}
I have a view in a fragment. This fragment is within a FrameLayout. I want to animate this view moving outside the fragment borders. However, the view always get clipped when crossing the border. I have tried by setting android:clipChildren="false" and android:clipToPadding="false" to everything, but I can't get it to work.
Is this even possible to do?
I had the similar problem with fragments and put android:clipChildren="false" and android:clipToPadding="false" on all the levels in my hierarchy, but it was still not working. The trick that worked for me was to add this override in my fragment code:
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ViewGroup vg = (ViewGroup) view;
vg.setClipChildren(false);
vg.setClipToPadding(false);
}
Hope this helps...
P.S. the credit should go to this answer
frangulyan You're missing the other bit of code from your credited answer. The below solution worked for me.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
LinearLayout rootView = (LinearLayout) inflater.inflate(R.layout.fragment_main, container, false);
//This can be done in XML
rootView.setClipChildren(false);
rootView.setClipToPadding(false);
return rootView;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//This is the NoSaveStateFrameLayout - force it to not clip
FrameLayout frameLayout = (FrameLayout) getView();
frameLayout.setClipChildren(false);
frameLayout.setClipToPadding(false);
}
The answers already given by #frangulyan and #Barrie Galitzky were both on the right track, but I had to add this loop to setClipping false for all parents in the hierarchy before it would work properly
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ViewGroup vg = (ViewGroup) view;
while(vg != null) {
vg.setClipChildren(false);
vg.setClipToPadding(false);
vg = vg.getParent() instanceof ViewGroup ? (ViewGroup) vg.getParent() : null;
}
}
I know this is a bit old, and correct answers have been given, but I wanted to add that you can do the same thing as frangulyan does inside onCreateView using the ViewGroup. I.e.:
#Override
public View onCreateView(LayoutInflater, ViewGroup container, Bundle savedInstanceState) {
container.setClipChildren(false);
container.setClipToPadding(false);
// Whatever other setup you like
return View;
}
Can anyone explain why my View element (ListView) is null with the following code:
public class NewsFragment extends Fragment {
#InjectView(R.id.news_listView) ListView lv;
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.news_layout, container, false);
ButterKnife.inject(this, view);
return view;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
if (lv == null) {
UIHelper.showAlert("null?");
}
}
}
Am I doing something wrong or is there something wrong with the library, because I pasted the example code to my app to get it working, but it's not working here... Any help is much appreciated!
Have you setup your IDE?
IDE CONFIGURATION
Some IDEs require additional configuration in order to enable
annotation processing.
IntelliJ IDEA — If your project uses an external configuration (like a
Maven pom.xml) then annotation processing should just work. If not,
try manual configuration.
Eclipse — Set up manual configuration
Usually this means that the annotation processor didn't do its work. That might be due to misconfiguration or random problems with the IDE. In my experience, every now and then I had to clean the project and build everything again.
This work for me:
...
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
ButterKnife.bind(this, view);
return view;
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
...
I am in trouble when trying to get the baseView(inflated in onCreateView and returned) in fragment, because I receive unexpected NoSaveStateFrameLayout.
I don't know why, can anyone explain?
Code:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
LogUtil.d(TAG, "onCreateView ...");
View v = inflater.inflate(R.layout.gen_blank, container, false);
return v;
}
When I run this code in all life cycles:
LogUtil.v(TAG, "getview = "+getView());
It logs the same msg: getview = android.support.v4.app.NoSaveStateFrameLayout#41afdb70
The support library inserts an additional view into the view hierarchy. According to a comment in NoSaveStateFrameLayout.java: "Pre-Honeycomb versions of the platform don't have View.setSaveFromParentEnabled(), so instead we insert this between the view and its parent."
So you will need to either check for this with something like:
View myView = (NoSaveStateFrameLayout)getView();
if (myView != null) {
myView = myView.getChildAt(0);
}
Or keep track of the view returned by onCreateView in an instance variable.