0

So the idea is to allow the user to click a button (or textview), display a datepicker, select a date, save the date to a database(on the TODO list) and display the selected date via a toast to the user (or on the textview if i take that route). All this is on a fragment of a tabbed activity

For now I've just used a button and when I click it the app simply crashes. The catlog shows the following

08-19 09:50:57.422 21472-21472/com.example.alibasmaci.maralapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.alibasmaci.maralapplication, PID: 21472
java.lang.NullPointerException
    at com.example.alibasmaci.maralapplication.OneWayFragment.showDialogOnButtonClick(OneWayFragment.java:49)
    at com.example.alibasmaci.maralapplication.OneWayFragment.onCreateView(OneWayFragment.java:93)

Now the java class in question is here:

        package com.example.alibasmaci.maralapplication;


    import android.app.DatePickerDialog;
    import android.app.Dialog;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.AutoCompleteTextView;
    import android.widget.Button;
    import android.widget.DatePicker;
    import android.widget.EditText;
    import android.widget.Spinner;
    import android.widget.Toast;


    /**
     * A simple {@link Fragment} subclass.
     */

    public class OneWayFragment extends Fragment {
    Spinner spinDeparture;
    Spinner spinDestination;
    Button btnDeparture;
    EditText etDepartureDate;
    int year_x, month_x, day_x;
    static final int DIALOG_ID = 0;


    String[] cities = {
            "Ottawa",
            "Montreal",
            "Toronto"
    };

    public static OneWayFragment newInstance() {
        OneWayFragment fragment = new OneWayFragment();
        return fragment;
    }

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


    public void showDialogOnButtonClick(){
        btnDeparture = (Button)getActivity().findViewById(R.id.btnDepartureDate);
        btnDeparture.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        getActivity().showDialog(DIALOG_ID);
                    }
                }
        );
    }

    protected Dialog onCreateDialog(int id){
        if (id == DIALOG_ID)
            return new DatePickerDialog(getActivity(),dPickerListener, year_x, month_x, day_x);
        return null;
    }

        private DatePickerDialog.OnDateSetListener dPickerListener = new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker datePicker, int year, int monthOfYear, int dayOfMonth) {
                year_x = year;
                month_x = monthOfYear;
                day_x = dayOfMonth;
                Toast.makeText(getActivity(), year_x+"/"+month_x+"/"+day_x,Toast.LENGTH_LONG).show();
            }
        };

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

        // Spinner to display departure points
        spinDeparture = (Spinner) rootView.findViewById(R.id.spinnerDeparture);
        ArrayAdapter adapterDeparture = new ArrayAdapter(getActivity(),android.R.layout.simple_spinner_item,cities);
        spinDeparture.setAdapter(adapterDeparture);

        // Spinner to display destinations
        spinDestination = (Spinner) rootView.findViewById(R.id.spinnerDestination);
        ArrayAdapter adapterDestination = new ArrayAdapter(getActivity(),android.R.layout.simple_spinner_item,cities);
        spinDeparture.setAdapter(adapterDestination);

        // Show date picker when departure button is clicked


        showDialogOnButtonClick();

        return rootView;


    }


}

The line to which the error is referring to is here:

 public void showDialogOnButtonClick() {

Any thoughts on why this is crashing?

Sergey Glotov
  • 20,200
  • 11
  • 84
  • 98
Ila Icamsab
  • 51
  • 1
  • 1
  • 5
  • Is your fragment attached to any activity? And please post the exact line of crash. – sumandas Aug 19 '16 at 14:32
  • Yes before I added the code relating to the datepicker the tabbed activity was able to swipe between two different fragments. The exact line according to the logcat is line 49 which I have shown previously. It also shows and error where the method is called on line 93 // Show date picker when departure button is clicked showDialogOnButtonClick(); – Ila Icamsab Aug 19 '16 at 14:40
  • Offtop: why is your fragment setting click listener for button in activity? It's quite strange. – Sergey Glotov Aug 19 '16 at 14:41
  • The tutorial I referred to was using the date picker in an activity, I was trying to modify it to be used in a fragment, and since I am not good at this I am sure there are many strange mistakes – Ila Icamsab Aug 19 '16 at 14:43
  • 1
    `getActivity()` returns null in `onCreateView()` method. Answers here: http://stackoverflow.com/a/6225044/436938 and here http://stackoverflow.com/a/31237093/436938 and many more – Sergey Glotov Aug 19 '16 at 14:49

1 Answers1

0

Try this:

Move this : btnDeparture = (Button)rootView.findViewById(R.id.btnDepartureDate);

to onCreateView() { ...} after the inflate

and for onClick try this:

 public void showDialogOnButtonClick(){
        btnDeparture.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        view.getContext().showDialog(DIALOG_ID);
                    }
                }
        );
    }

Try it.

sumandas
  • 555
  • 7
  • 20
  • cannot resolve method 'showDialog(int) error appears – Ila Icamsab Aug 19 '16 at 15:25
  • Ahh, I think you mixed Fragment having dialog and DialogFragment. Try this link, http://stackoverflow.com/questions/12818342/dialog-oncreatedialogint-dialogid/12818534#12818534. it would say, you have to have a showDialog defined. And if not mistaken I don't see it defined in your code. – sumandas Aug 19 '16 at 15:32