This is the code II implemented by googling and reading another stack overflow answers. but any answer is not working for me.
Following code this error - "No view found for id 0x7f09009e
Please help me to implement this
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
//////more code here //////
OnClickListener listner = new OnClickListener() {
#Override
public void onClick(View v) {
Fragment fragment=null;
if(v==rootView.findViewById(R.id.Button)){
fragment = new SortListFragment();
}
FragmentManager manager =getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.fragment_sort, fragment);
transaction.commit();
}
};
ImageButton btn = (ImageButton)rootView.findViewById(R.id.button);
btn.setOnClickListener(listner);
return rootView;
}
ButtonOnClickInterface interface;
public void onCreateView(//parameters){
View view=//inflate the view;
Button button =(Button)view.findViewById(R.id.buttonId);
button.setOnClickListener(new View.OnclickListener{
#Override
public void onClick(View v){
try{
interface.onClick();
}catch(Throwable e){
//may be null
}
}
});
}
#Override
public void onAttach(Context c){
super.onAttach(c);
try{
interface=(ButtonOnClickInterface)c;
}catch(Throwable e){
//not implemented
}
}
And your interface
interface ButtonOnClickInteface{
void onClick();
}
Implement this interface in your activity.
Add this line above on click listner:
rootview = inflater.inflate(R.layout.<your layout xml>, container,
false);
I think you forgot to provide layout file in which you have put buttons and all.
So please provide below line in your code after "super.OnCreateView()"
rootView = inflater.inflate(R.layout.your_layout_file, container, false);
And then check it is working or not, hope this will work
Related
I've been developing an android app which I included the default Navigation-Drawer from Android Studio and so on. In my home fragment, I've implemented CarViews, and then set those cardview(s) OnClickListener to replace the fragment with traditional procedure.
After the fragment replacement and new page comes, I wanted to change the Actionbar title.
So in the onCreateView(...) method, I tried,
((AppCompatActivity)getActivity()).getSupportActionBar().setTitle("B");
It worked. But after pressing the hardware back button to go back to the stacked fragment, the title remains changed & it doesn't change to "Home" again. I've tried other ways. Here's my following codes. Thanks in advance.
public class HomeFragment extends Fragment implements View.OnClickListener {
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_home, container, false);
Objects.requireNonNull(((AppCompatActivity) Objects.requireNonNull(getActivity())).getSupportActionBar()).setTitle("Home");
CardView cardView1 = root.findViewById(R.id.doctor_on);
CardView cardView2 = root.findViewById(R.id.ambulance_e);
CardView cardView3 = root.findViewById(R.id.maintainance_s);
cardView1.setOnClickListener(this);
cardView2.setOnClickListener(this);
cardView3.setOnClickListener(this);
return root;
}
#Override
public void onClick(View v) {
int id = v.getId();
switch (id) {
case R.id.doctor_on:
FragmentTransaction fragmentTransaction = Objects.requireNonNull(getActivity()).getSupportFragmentManager().beginTransaction();
Fragment fragment1 = new doctors();
fragmentTransaction.replace(R.id.container1, fragment1).addToBackStack(getString(R.string.menu_home)).commit();
return;
case R.id.ambulance_e:
//Put Actions
FragmentTransaction fragmentTransaction2 = Objects.requireNonNull(getActivity()).getSupportFragmentManager().beginTransaction();
Fragment fragment2 = new ambulance();
fragmentTransaction2.replace(R.id.container1, fragment2).addToBackStack(getString(R.string.menu_home)).commit();
return;
case R.id.maintainance_s:
//Put Actions
FragmentTransaction fragmentTransaction3 = Objects.requireNonNull(getActivity()).getSupportFragmentManager().beginTransaction();
Fragment fragment3 = new maintanance();
fragmentTransaction3.replace(R.id.container1, fragment3).addToBackStack(getString(R.string.menu_home)).commit();
return;
}
}
#Override
public void onResume() {
super.onResume();
((AppCompatActivity) getActivity()).getSupportActionBar().setTitle("Home");
}
}
To the next fragment(where I change the titlebar and pressed back button):
public class doctors extend Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
((AppCompatActivity)getActivity()).getSupportActionBar().setTitle("B");
View root = inflater.inflate(R.layout.fragment_doctors, container, false);
return root;
}
}
And in the doctors Fragment... Should fix the title error.
#Override
public void onDestroyView() {
super.onDestroyView();
Objects.requireNonNull(((AppCompatActivity) Objects.requireNonNull(getActivity()))
.getSupportActionBar())
.setTitle(getString(R.string.your_title_here));
}
You have to override the method onBackPressed() and write the code:
#Override
public View onBackPressed(){
//Here goes the code that head back to your main fragment
FragmentTransaction fragmentTransaction3 = Objects.requireNonNull(getActivity()).getSupportFragmentManager().beginTransaction();
Fragment fragment3 = new maintanance();
fragmentTransaction3.replace(R.id.container1, fragment3).addToBackStack(getString(R.string.menu_home)).commit();
}
}`
Add below code in Home Fragment...
#Override
public void onHiddenChanged(Boolean hidden) {
super.onHiddenChanged(hidden);
((AppCompatActivity) getActivity()).getSupportActionBar().setTitle("Home");
}
I have a problem. I am trying to change a fragment when a user clicks on it, but it does not work. I do not see the mistake but It works for another fragment. Here is my code :
**Fragment where I try to switch: **
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_profil, container, false);
TextView textViewNom = view.findViewById(R.id.labelNom);
textViewNom.setText(((Main)getActivity()).getNomComplet());
linearScore = (LinearLayout)view.findViewById(R.id.linearScore);
linearScore.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
fragment_score frag_score= new fragment_score();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.add(R.id.fragment_container, frag_score,"scorefrag");
transaction.addToBackStack("scorefrag");
transaction.commit();
}
});
linearReglage = (LinearLayout)view.findViewById(R.id.linearReglage);
linearReglage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
fragment_reglage frag_reglage= new fragment_reglage();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.add(R.id.fragment_container, frag_reglage,"reglagefrag");
transaction.addToBackStack("reglagefrag");
transaction.commit();
}
});
To explain you I do not have problem when I click on linearScore in the view but my application restarts when I try to click on linearReglage.
linearReglage (XML) :
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
tools:context="layout.fragment_reglage">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="oui"
android:textSize="30dp"/>
</ScrollView>
fragment_reglage (java):
public class fragment_reglage extends Fragment {
private View view;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view=inflater.inflate(R.layout.fragment_reglage, container, false);
return view;
}
public void onResume(){
super.onResume();
}
}
I do not see an error I just have a Failed exception ( because of non-0 exit status). But the application restart like if it was basic.
I do know what is this error and if it is link
as #Eselfar in the comment recommended you to see this answer
using the dex solution and trying it on a real device this cache error would be long gone probably! but to me everything seems fine in the code except the cache mechanism you are trying using the backStack along add option
see this for back stack management
I think this code will solve the problem of yours
just change the fragmentTransaction.add to fragmentTransaction.replace because from your code you are trying to navigate between two fragments replacing will keep the cache maintained
do that and test on real device..
here how your code will look like after the change
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_profil, container, false);
TextView textViewNom = view.findViewById(R.id.labelNom);
textViewNom.setText(((Main)getActivity()).getNomComplet());
linearScore = (LinearLayout)view.findViewById(R.id.linearScore);
linearScore.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
fragment_score frag_score= new fragment_score();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, frag_score,"scorefrag");
transaction.addToBackStack("scorefrag");
transaction.commit();
}
});
linearReglage = (LinearLayout)view.findViewById(R.id.linearReglage);
linearReglage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
fragment_reglage frag_reglage= new fragment_reglage();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, frag_reglage,"reglagefrag");
transaction.addToBackStack("reglagefrag");
transaction.commit();
}
});
I have been putting some debugging into my app to try to work out what is happening with the lifecycle.
I have some tabs and each tab content is a different fragment, each time a tab is changed onCreateView is called in the corresponding fragment.
In some of my onCreateViews I am currently mocking up some data and injecting table rows etc and then inflating the view every time. Like in the example below:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d(LOG_TAG, "******************* onCreateView() is being called in the Container Fragment *********************");
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_patient, container, false);
mTabHost = (FragmentTabHost)rootView.findViewById(android.R.id.tabhost);
mTabHost.setup(getActivity(), getChildFragmentManager(), android.R.id.tabcontent);
//Set up tabs
mTabHost.addTab(mTabHost.newTabSpec("home_addresses").setIndicator("Home Addresses"), HomeAddressesFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("postal_addresses").setIndicator("Postal Addresses"), PostalAddressesFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("temp_addresses").setIndicator("Temporary Addresses"), TemporaryAddressesFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("email_other").setIndicator("Email / Other"), TemporaryAddressesFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("tel_fax").setIndicator("Telephone & Fax"), TemporaryAddressesFragment.class, null);
//Set to home addresses tab
mTabHost.setCurrentTab(0);
tempAddressBtn = (Button)rootView.findViewById(R.id.temp_addr_tab_btn);
tempAddressBtn.setTransformationMethod(null);
tempAddressBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tabHandler(v);
}
});
postalAddressBtn = (Button)rootView.findViewById(R.id.postal_addr_tab_btn);
postalAddressBtn.setTransformationMethod(null);
postalAddressBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tabHandler(v);
}
});
homeAddressBtn = (Button)rootView.findViewById(R.id.home_addr_tab_btn);
homeAddressBtn.setTransformationMethod(null);
homeAddressBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tabHandler(v);
}
});
telFaxBtn = (Button)rootView.findViewById(R.id.tel_fax_tab_btn);
telFaxBtn.setTransformationMethod(null);
telFaxBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tabHandler(v);
}
});
emailOtherBtn = (Button)rootView.findViewById(R.id.email_other_tab_btn);
emailOtherBtn.setTransformationMethod(null);
emailOtherBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tabHandler(v);
}
});
return rootView;
}
My question is should I be doing this each time the onCreateView method is called? Is the view already cached somewhere?
I am seeing code in tutorials that looks a bit like:
#Override
protected void onCreate(Bundle savedInstance) {
super.onCreate(savedInstance);
setContentView(R.layout.pager_activity);
if (savedInstance == null) {
PagerFragment frag = PagerFragment.newInstance(buildPagerData());
FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction().add(R.id.layout_fragments, frag, PAGER_TAG).commit();
}
findViewById(R.id.btnFragments).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
changeFragmentVisibility();
}
});
}
I know this Fragment in the example above extends from FragmentActivity and mine just extend from Fragment but it is using the fragment manager and checking if the state is null before deciding to instantiate a new fragment.
Edit: I am guessing as well that as this happens in onCreate and not onCreateView it happens less frequently?
I am wondering if this is something that I should be doing or if its ok to continue with the way I am going?
In my opinion you should manage your tabs from the parent Activity. Each Fragment should be responsible for one part of the UI.
Another advantage of that approach is that each Fragment layout will be a bit easier. And it will be easier to apply different containers if needed (e.g ViewPager).
I was following tutorials on how to make Tabs using fragments.
each tab has this in the java files:
public class rdfri extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
// We have different layouts, and in one of them this
// fragment's containing frame doesn't exist. The fragment
// may still be created from its saved state, but there is
// no reason to try to create its view hierarchy because it
// won't be displayed. Note this is not needed -- we could
// just run the code below, where we would create and return
// the view hierarchy; it would just never be used.
return null;
}
return (LinearLayout)inflater.inflate(R.layout.rdfri, container, false);
}
}
I would like to try and get a imageButton in the fragment. I figured image bottons work with this code:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.rdmon);
ImageButton rainbowbook = (ImageButton) findViewById(R.id.imageButton1);
rainbowbook.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = Intent(rdmon.this, RainbowBookActivity.class);
rdmon.this.startActivity(myIntent);
}
});
}
So how would I go about getting the button code in the fragment code?
Thanks.
Put the button code in the overridden onActivityCreated method in your fragment class.
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ImageButton rainbowbook = (ImageButton) findViewById(R.id.imageButton1);
rainbowbook.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = Intent(rdmon.this, RainbowBookActivity.class);
rdmon.this.startActivity(myIntent);
}
});
}
This assumes of course that your imagebutton is contained in your fragment layout.
i am encapsulating stuff into a fragment at the moment and run into a problem that is hard to google.
Inside my fragment are some buttons with onClick attributes but they are called on the Activity rather the fragment from the android system - this makes encapsulating a bit clumsy. Is there a way to have the reflection stuff from onClick to call on the fragment? The only solution to this I see at the moment is not to use onClick in the xml and set click-listeners inside the fragment via code.
I spoke to some googlers # #adl2011 - they recognize the problem and perhaps there will be a fix of that in the future. Until then - one should use .setOnClick in the Fragment.
The problem is that when layout's are inflated it is still the hosting Activity that is receiving the button clicks, not the individual Fragments.
I prefer using the following solution for handling onClick events. This works for Activity and Fragments as well.
public class StartFragment extends Fragment implements OnClickListener{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_start, container, false);
Button b = (Button) v.findViewById(R.id.StartButton);
b.setOnClickListener(this);
return v;
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.StartButton:
...
break;
}
}
}
Then problem is gone.
It works for me
Add import:
import android.view.View.OnClickListener;
Fragment.java
...
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = (ViewGroup) inflater.inflate(R.layout.fragment, container, false);
Button mButton = (Button) rootView.findViewById(R.id.button);
mButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
}
});
return rootView;
}
ISSUE:
1.In XML onClick attribute will call activity's public method.
2.Fragment's public method not called.
3.Fragment reusability.
SOLUTION:
In Fragment's layout add this to the View.
android:onClick="onFragmentViewClick"
In each activity the fragment may belong..
public void onFragmentViewClick(View v) {
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.container);
if (fragment != null && fragment.isVisible()) {
if (fragment instanceof SomeFragment) {
((SomeFragment) fragment).onViewClicked(v);
}
}
}
In the Fragment include this method..
public void onViewClicked(View v) {
switch (v.getId()) {
case R.id.view_id:
Log.e("onViewClicked", "yehhh!!");
break;
}
}
You could have the listener that is being called in the activity forward the call onto a listener in the fragment. You should have a reference to the fragment inside of the FragmentActivity to pass the call on. You will have to cast to call the method or have your fragment implement an interface you define. I know that isn't the best solution but it will work. You could also use the tag of a button to specify the method name to call if you wanted. Hope this helps a bit.
Try this...
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.activity_ipadditional_users, container, false);
FloatingActionButton button7 = (FloatingActionButton) rootView.findViewById(R.id.fab_ip_additional_user);
button7.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), IPAdditionalUsersEdit.class);
startActivity(intent);
}
});
return rootView;