Reputation: 4050
i have this fragment:
public class Fragment_PV extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//option 2
return inflater.inflate(R.layout.fragment_pv, container, false);
//option 1
}
//option 3
}
Now i want to do a fairly significant amount of math when somebody pushes a button in the layout that goes with this fragment. As it stands, the layout loads fine but of course nothing happens.
When i put the code where option 1 is, eclipse says that the code is "unreachable"
When I put the code where option 2 is, there are no compile errors but the app crashes when the fragment is loaded saying it encounters an error in the oncreateview method
When I put the code where option 3 is, it tells me that the parentheses {} are set wrong and it should be remodeled that it looks like option 1 ...
help please :)
p.s. the code i was testing to put in,just to see if it was working is this:
Toast errormsg = Toast.makeText(getActivity(), "Error!", 5000);
errormsg.setGravity(Gravity.CENTER, 0, 0);
errormsg.show();
Upvotes: 0
Views: 1985
Reputation: 16393
Put your code in onActivityCreated
.
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Your code here
Toast errormsg = Toast.makeText(getActivity(), "Error!", 5000);
errormsg.setGravity(Gravity.CENTER, 0, 0);
errormsg.show();
}
Upvotes: 3