1

I have gone through this link. But the given solution does not solve my problem. All well experienced persons suggested to use getActivty().getApplicationContext(). But I'm using a normal class instead of activity. I'm getting all the UI values and initialized to the objects which is in my fragment. So from there I tried to save it in my database. I'm using normal class for database which is not extending anything.

I'm getting two errors when I pass the values from my main fragment to database class:

  1. The constructor DatePickerDialog(TopRatedFragment, DatePickerDialog.OnDateSetListener, int, int, int) is undefined
  2. create method getApplicationContext()

Please give me some idea to solve this problem.

Here is my Fragment class code.

public class TopRatedFragment extends Fragment {
    public EditText text1,text2,text3,text4;
    Button date,save;
    TextView textdate;
    DatePicker datepicker;
    ListView expenselist;
    private int myear,mmonth,mday;
    ArrayList<String> DayExpenseList = new ArrayList<String>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.activity_main, container,
            false);
        text1 = (EditText) rootView.findViewById(R.id.editText1);
        text2=(EditText) rootView.findViewById(R.id.editText2);
        text3=(EditText) rootView.findViewById(R.id.editText3);
        text4=(EditText) rootView.findViewById(R.id.editText4);
        textdate = (TextView) rootView.findViewById(R.id.textView6);
        save = (Button) rootView.findViewById(R.id.button1);
        date=(Button) rootView.findViewById(R.id.datepicker);
        expenselist = (ListView) rootView.findViewById(R.id.listView1);
        date.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                DatePickerDialog dpd = new DatePickerDialog(TopRatedFragment.this, mDateSetListener, myear, mmonth, mday);
                dpd.show();
            }
        });
        final Calendar c = Calendar.getInstance();
        myear = c.get(Calendar.YEAR);
        mmonth = c.get(Calendar.MONTH);
        mday = c.get(Calendar.DAY_OF_MONTH);
        save.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                String entertainment = text1.getText().toString();
                String food = text2.getText().toString();
                String medicine = text3.getText().toString();
                String travel = text4.getText().toString();
                DBHelper helper = new DBHelper(getApplicationContext());
                helper.insertValues(entertainment, food, medicine, travel);
                DayExpenseList = helper.getValues();
                setAdapter();
            }
        });
        return rootView;
    }

    private void setAdapter()
    {
        expenselist.setAdapter(new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1,DayExpenseList));
    }

    private void updateDisplay() {
        // TODO Auto-generated method stub
        textdate.setText(new StringBuilder()
            .append(mday).append("-").append(mmonth+1).append("-")
            .append(myear));
    }

    private DatePickerDialog.OnDateSetListener mDateSetListener = new 
        DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear,
                    int dayOfMonth) {
                myear=year;
                mmonth=monthOfYear;
                mday=dayOfMonth;
                updateDisplay();
            }
        };
}
Community
  • 1
  • 1
Sridhar
  • 668
  • 1
  • 11
  • 22
  • 1
    Inside `Fragment`, Use `getActivity()` instead to get the `Context`. – Andrew T. Jul 22 '14 at 06:38
  • If you're getting the errors you mentioned in just the code you've posted, then that's irrelevant. The constructors just need a Context passed to them; it doesn't matter if they're Activities or Fragments or not. – Mike M. Jul 22 '14 at 06:45
  • Why your getting the views? because views are inflated from `View rootView = inflater.inflate(R.layout.activity_main, container,false);`, and Fragments binds with the activity context by default from which activity they are initiated. Second: Why DatePickerDialog gives exception? because date picker requires the context and your passing it the fragment instance, you can retrieve the context/activity anytime in a fragment by `getActivity()`. Hope it will resolve your query, and the exception as well. – jitain sharma Jul 22 '14 at 06:46
  • Yes.I understood. The error has gone. May i know why i'am facing this error: constructor DatePickerDialog is undefined. – Sridhar Jul 22 '14 at 06:49
  • 1
    Use `getActivity()` instead of `TopRatedFragment.this`. – Mike M. Jul 22 '14 at 06:54

3 Answers3

1

getApplicationContext() is only available in Context and all classes that are extending it (including Activity and its variant). However, Fragment doesn't extend it, and hence, doesn't have that method.

Instead, Fragment depends on the Activity's context, which can be accessed by calling getActivity().

The error says:

  1. The constructor DatePickerDialog(TopRatedFragment, DatePickerDialog.OnDateSetListener, int, int, int) is undefined
  2. create method getApplicationContext()

The first one is because DatePickerDialog expects Context for the first argument, where you put Fragment which is not a subclass of Context.

The second one is already explained above.

Change the code to

DatePickerDialog dpd = new DatePickerDialog(getActivity(), mDateSetListener,
                               myear, mmonth, mday);

and

DBHelper helper = new DBHelper(getActivity());

should fix the errors.

Andrew T.
  • 4,701
  • 8
  • 43
  • 62
0

In fragment you use getActivity() not getApplicationContext()

0

In Fragment use activity's context,so change

    DBHelper helper = new DBHelper(getApplicationContext());

to

    DBHelper helper = new DBHelper(container.getContext());

and use ViewGroup container as final

Or Use getActivity() to get activity context.

Giru Bhai
  • 14,370
  • 5
  • 46
  • 74