Abhinav Arora
Abhinav Arora

Reputation: 537

openOrCreateDatabase() undefined in the fragment class

I am writing a code in which I place an fragment within an activity. Now in the OnCreate() function of the fragment class, I am not having error in the following code

public class HomePage_Fragment extends Fragment {

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

    SQLiteDatabase db = openOrCreateDatabase("DATABASE",android.content.Context.MODE_PRIVATE ,null);

}

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


    return inflater.inflate(R.layout.fragment_home_page,container,false);


}

}

ERROR:The method openOrCreateDatabase(String, int, null) is undefined for the type HomePage_Fragment

The code is very simple and I have used the same commands many times, so I am sure the syntax is correct.

Thanks in advance

Upvotes: 0

Views: 8345

Answers (1)

kalyan pvs
kalyan pvs

Reputation: 14590

openOrCreateDatabase(name, mode, factory) method is in Context class

Change this line

SQLiteDatabase db = openOrCreateDatabase("DATABASE",android.content.Context.MODE_PRIVATE ,null);

into

SQLiteDatabase db = getActivity().openOrCreateDatabase("DATABASE",android.content.Context.MODE_PRIVATE ,null);

Upvotes: 6

Related Questions