Search code examples
androidsetcontentview

setContentView trouble on android


I need your help and please don't be angry with me!

My first training project was creating a new android layout and loaded in my onCreate method

There is the code:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.new_layout);

    if (savedInstanceState == null) {
        getFragmentManager().beginTransaction()
                .add(R.id.textView1, new PlaceholderFragment())
                .add(R.id.editText1, new PlaceholderFragment())
                .add(R.id.button1, new PlaceholderFragment())
                .commit();
    }
}

The thing is if I comment the If statement the app runs perfectly fine but If I remove the comments I cant run the APP!!


public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.new_layout, container, false);
        return rootView;
    }
}

there is my layout

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout 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:orientation="vertical"
    tools:ignore="HardcodedText" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/textview" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPersonName" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>

Solution

  • 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;
        }
    }