Using Edittext in Fragment Activities outside the onCreateView method

724 views Asked by At

As the title says, I need to get the value of an editText contained in a fragment activity, being part of a ViewPager; I've tried everything so far, and I've understood that the xml items can be accessed just by passing the current view of the activity after inflating it, and that's what i've done.. But i need to have the values contained in the edittexts after the user fills it, specifically after a button 'save' is pressed; during the execution of the program i get this error when i try to get the text contained: "java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference" Sorry for the silly question, i'm new to android programming and i appreciate all the help i can get Here's the code of the fragment activity:

public class Monday extends android.support.v4.app.Fragment{
EditText[] subjects = new EditText[6];
EditText[] classes = new EditText[6];

private OnFragmentInteractionListener mListener;

    public Monday() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        ScrollView scrollView = (ScrollView)inflater.inflate(R.layout.fragment_monday, container, false);
        //some code

        subjects[0] = (EditText) scrollView.findViewById(R.id.editTextMateria1);
        subjects[1] = (EditText) scrollView.findViewById(R.id.editTextMateria2);
        subjects[2] = (EditText) scrollView.findViewById(R.id.editTextMateria3);
        subjects[3] = (EditText) scrollView.findViewById(R.id.editTextMateria4);
        subjects[4] = (EditText) scrollView.findViewById(R.id.editTextMateria5);
        subjects[5] = (EditText) scrollView.findViewById(R.id.editTextMateria6);


        classes[0] = (EditText) scrollView.findViewById(R.id.editTextClasse1);
        classes[1] = (EditText) scrollView.findViewById(R.id.editTextClasse2);
        classes[2] = (EditText) scrollView.findViewById(R.id.editTextClasse3);
        classes[3] = (EditText) scrollView.findViewById(R.id.editTextClasse4);
        classes[4] = (EditText) scrollView.findViewById(R.id.editTextClasse5);
        classes[5] = (EditText) scrollView.findViewById(R.id.editTextClasse6);
        return scrollView;
        //return inflater.inflate(R.layout.fragment_monday, container, false);
    }

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        public void onFragmentInteraction(Uri uri);
    }

    public String[] calculateDay(){


        String s = "";
        for(int i=1;i<subjects.length;i++){
            s += subjects[i].getText().toString()+"-"+classes[i].getText().toString()+";";
        }
        String[] monday = new String[6];

        monday = s.split(";");
        return monday;

    }
}

That's the part that generates the error:

   s += subjects[i].getText().toString()+"-"+classes[i].getText().toString()+";";

Here's the code of the 'mainactivity' that generates the ViewPager(on which the button pressed save is handled):

public class CustomTimetables extends FragmentActivity implements NoticeDialogFragment.NoticeDialogListener{

    public static String classname = "";
    ViewPager viewPager = null;

    private MyAdapter2 mAdapter;

    ViewPager Tab;
    MyAdapter2 TabAdapter;
    ActionBar actionBar;
    PagerSlidingTabStrip tabs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
        setContentView(R.layout.activity_custom_timetables);

        tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
        Tab = (ViewPager) findViewById(R.id.pager);
        TabAdapter = new MyAdapter2(getSupportFragmentManager());

        Tab.setAdapter(TabAdapter);
        tabs.setViewPager(Tab);
        Toast.makeText(  getApplicationContext(),
                "Inserisci l'orario nelle caselle, lasciale vuote se le lezioni terminano prima ",
                Toast.LENGTH_LONG
        ).show();



    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_custom_timetables, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();

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

        return super.onOptionsItemSelected(item);
    }


    public void onSaveTimetable(View v){
        DialogFragment dialog = new NoticeDialogFragment();
        dialog.show(this.getSupportFragmentManager(), "insert_class");
    }

    public void onDialogPositiveClick(DialogFragment dialog) {
        String timetable = "Orario;Lunedi;Martedi;Mercoledi;Giovedi;Venerdi;Sabato;\n08:00;";


        String[] monday = new String[6];
        Monday m = new Monday();
        monday  = m.calculateDay();

        for(int i = 0; i<6; i++){
            timetable += monday[i]+";";



            switch(i){
                case 0: timetable +="\n";break;
                case 1 : timetable +="\n09:00;";break;
                case 2 : timetable +="\n10:00;";break;
                case 3 : timetable +="\n11:05;";break;
                case 4 : timetable +="\n12:00;";break;
                case 5 : timetable +="\n12:50;";break;
            }

        }

    }

    @Override
    public void onDialogNegativeClick(DialogFragment dialog) {

    }
}

class MyAdapter2 extends FragmentStatePagerAdapter
{

    public MyAdapter2(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int i) {
        Fragment fragment = null;
        if(i==0){
            fragment = new Monday();
        }
        if(i==1){
            fragment = new Tuesday();
        }
        if(i==2){
            fragment = new Wednesday();
        }
        if(i==3){
            fragment = new Thursday();
        }
        if(i==4){
            fragment = new Friday();
        }
        if(i==5){
            fragment = new Saturday();
        }
        return fragment;
    }

    @Override
    public int getCount() {
        return 6;
    }

    @Override
    public CharSequence getPageTitle(int position) {

        if(position == 0){
            return "Lunedi";
        }
        if(position == 1){
            return "Martedi";
        }
        if(position == 2){
            return "Mercoledi";
        }
        if(position == 3){
            return "Giovedi";
        }
        if(position == 4){
            return "Venerdi";
        }
        if(position == 5){
            return "Sabato";
        }
        return null;
    }


}

Here's the logcat:

    06-09 19:29:16.172  24414-24443/com.progettostage.nick__000.timetablesapp D/OpenGLRenderer﹕ Use EGL_SWAP_BEHAVIOR_PRESERVED: true
06-09 19:29:16.187  24414-24414/com.progettostage.nick__000.timetablesapp D/Atlas﹕ Validating map...
06-09 19:29:16.250  24414-24443/com.progettostage.nick__000.timetablesapp I/Adreno-EGL﹕ <qeglDrvAPI_eglInitialize:379>: QUALCOMM Build: 01/15/15, ab0075f, Id3510ff6dc
06-09 19:29:16.252  24414-24443/com.progettostage.nick__000.timetablesapp I/OpenGLRenderer﹕ Initialized EGL, version 1.4
06-09 19:29:16.290  24414-24443/com.progettostage.nick__000.timetablesapp D/OpenGLRenderer﹕ Enabling debug mode 0
06-09 19:32:20.725  24414-24443/com.progettostage.nick__000.timetablesapp V/RenderScript﹕ Application requested CPU execution
06-09 19:32:20.738  24414-24443/com.progettostage.nick__000.timetablesapp V/RenderScript﹕ 0xb83ba0b8 Launching thread(s), CPUs 4
06-09 19:32:38.415  24414-24414/com.progettostage.nick__000.timetablesapp D/AndroidRuntime﹕ Shutting down VM
06-09 19:32:38.422  24414-24414/com.progettostage.nick__000.timetablesapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.progettostage.nick__000.timetablesapp, PID: 24414
    java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
            at com.progettostage.nick__000.timetablesapp.Monday.calculateDay(Monday.java:111)
            at com.progettostage.nick__000.timetablesapp.CustomTimetables.onDialogPositiveClick(CustomTimetables.java:111)
            at com.progettostage.nick__000.timetablesapp.NoticeDialogFragment$2.onClick(NoticeDialogFragment.java:33)
            at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:162)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5254)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

And this is simply the class that handles the dialog, i dont think it has anything to do with it, but better safe than sorry..

public class NoticeDialogFragment extends DialogFragment {


    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        // Get the layout inflater
        LayoutInflater inflater = getActivity().getLayoutInflater();
        final View view = inflater.inflate(R.layout.dialog_classinput, null);
        // Inflate and set the layout for the dialog
        // Pass null as the parent view because its going in the dialog layout
        builder.setView(view)
                // Add action buttons
                .setTitle("Inserisci nome classe")
                .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        EditText et = (EditText)view.findViewById(R.id.classname22);
                        CustomTimetables.classname = et.getText().toString();
                        // Send the positive button event back to the host activity
                        mListener.onDialogPositiveClick(NoticeDialogFragment.this);
                    }
                })
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.dismiss();
                    }
                });
        return builder.create();
    }


    public interface NoticeDialogListener {
        public void onDialogPositiveClick(DialogFragment dialog);
        public void onDialogNegativeClick(DialogFragment dialog);
    }


    NoticeDialogListener mListener;


    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        // Verify that the host activity implements the callback interface
        try {
            // Instantiate the NoticeDialogListener so we can send events to the host
            mListener = (NoticeDialogListener) activity;
        } catch (ClassCastException e) {
            // The activity doesn't implement the interface, throw exception
            throw new ClassCastException(activity.toString()
                    + " must implement NoticeDialogListener");
        }
    }

}

Here's the Monday.xml as well(had to cut off some parts in the end cause it was too long, but it's simple, it goes on like this till editTextMateria6 and editTextClasse6):

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">
<RelativeLayout  android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context=".Monday"

        >


    <TextView
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="58dp"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="8:00-9:00"
        android:id="@+id/PrimaOra"
        android:textSize="25dp"

        android:layout_gravity="center_horizontal|bottom"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Materia"
        android:id="@+id/materia1"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="115dp" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:ems="10"
        android:id="@+id/editTextMateria1"
        android:layout_marginTop="160dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Classe"
        android:id="@+id/classe1"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="225dp" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:text=""
        android:ems="10"
        android:id="@+id/editTextClasse1"
        android:layout_marginTop="270dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />
-----------------------------------------------------

    <TextView
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="58dp"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="9:00-10:00"
        android:id="@+id/SecondaOra"
        android:textSize="25dp"
        android:layout_gravity="center_horizontal|bottom"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="325dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Materia"
        android:id="@+id/materia2"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="390dp" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:text=""
        android:ems="10"
        android:id="@+id/editTextMateria2"
        android:layout_marginTop="435dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Classe"
        android:id="@+id/classe2"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="500dp" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:text=""
        android:ems="10"
        android:id="@+id/editTextClasse2"
        android:layout_marginTop="545dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"/>
        -----------------------------------------

    <TextView
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="58dp"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="10:00-10:50"
        android:id="@+id/TerzaOra"
        android:textSize="25dp"
        android:layout_gravity="center_horizontal|bottom"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="600dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Materia"
        android:id="@+id/materia3"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="665dp" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:text=""
        android:ems="10"
        android:id="@+id/editTextMateria3"
        android:layout_marginTop="710dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Classe"
        android:id="@+id/classe3"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="775dp" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:text=""
        android:ems="10"
        android:id="@+id/editTextClasse3"
        android:layout_marginTop="825dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"/>
    -----------------------------------------
    <TextView
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="58dp"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="11:05-12:00"
        android:id="@+id/QuartaOra"
        android:textSize="25dp"
        android:layout_gravity="center_horizontal|bottom"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="875dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Materia"
        android:id="@+id/materia4"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="940dp" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:text=""
        android:ems="10"
        android:id="@+id/editTextMateria4"
        android:layout_marginTop="985dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Classe"
        android:id="@+id/classe4"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="1050dp" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:text=""
        android:ems="10"
        android:id="@+id/editTextClasse4"
        android:layout_marginTop="1095dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"/>
    ------------------------------------------------
    <TextView
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="58dp"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="12:00-12:50"
        android:id="@+id/QuintaOra"
        android:textSize="25dp"
        android:layout_gravity="center_horizontal|bottom"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="1150dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Materia"
        android:id="@+id/materia5"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="1215dp" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:text=""
        android:ems="10"
        android:id="@+id/editTextMateria5"
        android:layout_marginTop="1260dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Classe"
        android:id="@+id/classe5"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="1325dp" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:text=""
        android:ems="10"
        android:id="@+id/editTextClasse5"
        android:layout_marginTop="1380dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"/>
    ----------------------------------------------
    <TextView
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="58dp"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="12:50-13:40"
        android:id="@+id/SestaOra"
        android:textSize="25dp"
        android:layout_gravity="center_horizontal|bottom"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="1435dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Materia"
        android:id="@+id/materia6"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="1500dp" />



    <Button
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:width="400dp"
        android:height="70dp"
        android:onClick="onSaveTimetable"
        android:text="Salva"
        android:layout_marginTop="1700dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        />


</RelativeLayout>
</ScrollView>

UPDATE Okay I've added a few lines so maybe it is more clear what the problem is..

  String sub1 = subjects[0].getText().toString();
            String classr1 = classes[0].getText().toString();
            for(int i=0;i<subjects.length;i++){
                s += subjects[i].getText().toString()+"-"+classes[i].getText().toString()+";";
            }

If I set it like this, I get the same exact error right at the sub1 line, because in a method like this one i'm not able to pass the view or inflate that, as I did on oncreateview, where it works just fine whatever operation I do.. The problem is that I need those edittext after the user had put something inside them.. I hope i've been a little clearer, I appreciate all the help I'm still getting and I'd like to point out that I've tried every single solution down here, even tho I didn't reply to them all

4

There are 4 answers

1
Anton Kovalyov On

This is wrong usage:

 Monday m = new Monday();
 monday  = m.calculateDay();

Try this:

Monday m = mAdapter.getItem(viewPager.getCurrentItem());
monday = m.calculateDay();
1
siva On

Array index starts from 0. Please make sure you use correct index values when accessing arrays.

2
Jayesh Elamgodil On

You can maybe try using the :

getSupportFragmentManager().findFragmentById 

to get hold of the already initiated Monday fragment in your onDialogPositiveClick. You can then use this fragment to invoke the calculateDay method. An id can be set in the xml file for the fragment. Your current implementation to create an instance and invoke calculateDay is incorrect.

In the xml layout of your Activity where I am assuming you've added the fragment tag, add an id attribute. Then in your onDialogPositiveClick, call

Monday monday = (Monday) getSupportFragmentManager().
    findFragmentById("that fragment id here");

Now, use this monday variable to invoke the calculateDay method.

Some official documentation: http://developer.android.com/training/basics/fragments/communicating.html#Deliver

1
EpicPandaForce On

It has been said before, but I'll say it again.

  for(int i=1;i<7;i++){
        s += subjects[i].getText().toString()+"-"+classes[i].getText().toString()+";";

Should be

    for(int i = 0; i < subjects.length; i++){
        s += subjects[i].getText().toString()+"-"+classes[i].getText().toString()+";";

EDIT: To be honest, I'd just write the code like this instead

public enum Days {
    MONDAY("Lunedi") { //should be string resource
        public Fragment createFragment() {
            return new Monday();
        }
    },
    TUESDAY("Martedi") {
        public Fragment createFragment() {
            return new Tuesday();
        }
    },
    WEDNESDAY("Mercoledi") {
        public Fragment createFragment() {
            return new Wednesday();
        }
    },
    THURSDAY("Giovedi") {
        public Fragment createFragment() {
            return new Thursday();
        }
    },
    FRIDAY("Venerdi") {
        public Fragment createFragment() {
            return new Friday();
        }
    },
    SATURDAY("Sabato") {
        public Fragment createFragment() {
            return new Saturday();
        }
    };

    private String name;

    private Days(String name) {
        this.name = name;
    }

    public String getName() { return name; }

    public abstract Fragment createFragment();
}

class MyAdapter2 extends FragmentStatePagerAdapter
{

    public MyAdapter2(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int i) {
        Fragment fragment = null;
        Days[] days = Days.values();
        if(i < days.length) {
            fragment = days[i].createFragment();
        }
        return fragment;
    }

    @Override
    public int getCount() {
        return Days.values().length;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        Days[] days = Days.values();
        if(position < days.length) {
            return days[position].getName(); 
        } else {
            return null; 
        }
    }    
}

And to be fair, and most importantly, subjects[5] = (EditText) scrollView.findViewById(R.id.editTextMateria6); is null because editTextMateria6 does not exist in fragment_monday.xml, and neither does editTextClasse6.