Ryden Sun
Ryden Sun

Reputation: 75

Fragment wasn't displayed and OnCreateView method wasn't called

Recently I am learning how to use fragments in my app and I follow a tutorial that contains a simple demo, however, after I ran it, the fragment is not displayed, the screen is whole white and I debugged it found that the OnCreateView() method was not called.
Here are my codes: activity_crime:

<FrameLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:id="@+id/fragmentContainer"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".CrimeActivity">

</FrameLayout>

fragment_crime XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/crime_title_label"/>


    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/crime_title"
        android:hint="@string/crime_title_hint"
        style="?android:listSeparatorTextViewStyle"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/crime_details_label"
        style="?android:listSeparatorTextViewStyle"/>
    <Button
        android:id="@+id/crime_date"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        />
    <CheckBox
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
         android:id="@+id/crime_solved"
        android:layout_marginLeft="16dp"
         android:layout_marginRight="16dp"
        android:text="@string/crime_solved_label"
        />

</LinearLayout>

CrimeActivity.java

package com.example.ryden.criminalintent;

import android.support.v4.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

public class CrimeActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_crime);

       FragmentManager fm = getSupportFragmentManager();

        CrimeFragment fragment = new CrimeFragment();
       fm.beginTransaction().add(R.id.fragmentContainer,fragment).commit();
        fm.executePendingTransactions();
        Log.v("test","add");


    }






    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
      //  if (id == R.id.action_settings) {
       //     return true;
      //  }

        return super.onOptionsItemSelected(item);
    }
}

CrimeFragment.java

package com.example.ryden.criminalintent;


import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.support.v4.app.FragmentActivity;
import android.widget.EditText;
import android.support.v4.app.Fragment;


import java.text.SimpleDateFormat;

/**
 * Created by ryden on 12/23/2015.
 */
public class CrimeFragment extends Fragment{
        private Crime mCrime;
        private EditText mTitleField;
        private Button mDateButton;
        private CheckBox mSolvedCheckBox;

        @Override
    public void onCreate(Bundle savedInstanceState){
            super.onCreate(savedInstanceState);
            mCrime = new Crime();
            Log.v("1","create");
        }

    public View OnCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState){
        Log.v("222","oncreateview");
        View v = inflater.inflate(R.layout.fragment_crime, parent, false);
        Log.v("inflater?","inflater?");
        mTitleField = (EditText)v.findViewById(R.id.crime_title);
        mTitleField.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            mCrime.setmTitle(charSequence.toString());
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        SimpleDateFormat dateFormat = new SimpleDateFormat("yy/mm/dd HH:mm");
        mDateButton = (Button)v.findViewById(R.id.crime_date);
        mDateButton.setText(dateFormat.format(mCrime.getmDate().toString()));
        mDateButton.setEnabled(false);
        mSolvedCheckBox = (CheckBox)v.findViewById(R.id.crime_solved);
        mSolvedCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mCrime.setmSolved(isChecked);
            }
        });
        return  v;
    }

}

Upvotes: 0

Views: 79

Answers (3)

Michele La Ferla
Michele La Ferla

Reputation: 6884

to debug this, set the onCreateView method to be overridden. This makes sure that the onCreateView method is called correctly and that the casing of the method is correct. Hence the code should be as follows:

@Override
public View OnCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState){
        Log.v("222","oncreateview");
        View v = inflater.inflate(R.layout.fragment_crime, parent, false);
        Log.v("inflater?","inflater?");
        mTitleField = (EditText)v.findViewById(R.id.crime_title);
        mTitleField.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            mCrime.setmTitle(charSequence.toString());
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        SimpleDateFormat dateFormat = new SimpleDateFormat("yy/mm/dd HH:mm");
        mDateButton = (Button)v.findViewById(R.id.crime_date);
        mDateButton.setText(dateFormat.format(mCrime.getmDate().toString()));
        mDateButton.setEnabled(false);
        mSolvedCheckBox = (CheckBox)v.findViewById(R.id.crime_solved);
        mSolvedCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mCrime.setmSolved(isChecked);
            }
        });
        return  v;
    }

Take note that this should be done in the fragment only. Once you do that change, the fragment should be displayed.

Upvotes: 1

Tony Augustine
Tony Augustine

Reputation: 180

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,  Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.fragment_tab_1, null);

    return v;
}

Upvotes: 4

Zeeshan Shabbir
Zeeshan Shabbir

Reputation: 7114

Problem is with signature. It should start with lowercase. replace this in your fragment

 @override
    public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState){
            Log.v("222","oncreateview");
            View v = inflater.inflate(R.layout.fragment_crime, parent, false);
            Log.v("inflater?","inflater?");
            mTitleField = (EditText)v.findViewById(R.id.crime_title);
            mTitleField.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                }

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                mCrime.setmTitle(charSequence.toString());
                }

                @Override
                public void afterTextChanged(Editable editable) {

                }
            });

Upvotes: 1

Related Questions