I was trying to go to another page using button
, but it always fail.
Here is my First Class with its XML:
public class Fin
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.frame, new MySchedule()).commit();
MySchedule is the name of my java class.
public class OneWayFragment extends Fragment {
ImageView img_search;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.one_way_fragment, container, false);
img_search = (ImageView) view.findViewById(R.id.search);
img_search.setOnClickListener(new View.OnClickListener() {
@Override`enter code here`
public void onClick(View v) {
Intent displayFlights = new Intent(getActivity(), SelectFlight.class);
startActivity(displayFlights);
}
});
Try this code once-
public class FindPeopleFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home,
container, false);
Button button = (Button) rootView.findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
updateDetail();
}
});
return rootView;
}
public void updateDetail() {
Intent intent = new Intent(getActivity(), MainActivityList.class);
startActivity(intent);
}
}
And as suggested by Raghunandan remove below code from your fragment_home.xml
-
android:onClick="goToAttract"
Hope this code will help
public class ThisFragment extends Fragment {
public Button button = null;
Intent intent;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.yourlayout, container, false);
intent = new Intent(getActivity(), GoToThisActivity.class);
button = (Button) rootView.findViewById(R.id.theButtonid);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(intent);
}
});
return rootView;
}
You can use this code, make sure you change "ThisFragment" as your fragment name, "yourlayout" as the layout name, "GoToThisActivity" change it to which activity do you want and then "theButtonid" change it with your button id you used.
If you can get a View
in that fragment, you can access the context from there:
view.getContext().startActivity(intent);
use this
public void goToAttract(View v)
{
Intent intent = new Intent(getActivity(), MainActivityList.class);
startActivity(intent);
}
be sure you've registered MainActivityList
in you Manifest