It seems that you created a new layout named new_layout
. Inside the new layout, you added some views and you try - with the condition in comments - to add a Fragment
in your activity. The FragmentManager
try to add the new fragment inside R.id.container as follows:
.add(R.id.container, new PlaceholderFragment()) // add(view id, fragment class)
Then, you might be a NullPointerException
because the activity cannot find this specific id into your layout.
The id container
doesn't exist inside new_layout
file.
Solution
If you want to use the Fragment Class, I'd suggest you to keep the activity_main
layout for:
setContentView(R.layout.activity_main);
which contains a FrameLayout
with the right id. This view will receive and display the fragment and you can have your custom layout inside it as follows:
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() { }
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// call the custom layout here
View rootView = inflater.inflate(R.layout.new_layout, container, false);
// update, control, the views of the layout here
TextView text = (TextView) rootView.findViewById(R.id.textView1);
text.setText("This is inside fragment");
// same for other..
return rootView;
}
}