Java Question

Executing AsyncTask from PlaceHolderFragment: "'com.example.Activity.this' cannot be referenced from a static context"

I created a tabbed activity called
. As many of you know, this kind of activity comes with a built-in class called
which is basically where the magic happens. Mine looks as follows:

public static class PlaceholderFragment extends Fragment {

private static final String ARG_SECTION_NUMBER = "section_number";

public PlaceholderFragment() {

public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
return fragment;

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

// Some code related to database queries irrelevant to this question...

if (getArguments().getInt(ARG_SECTION_NUMBER) == 1) {
View profileView = inflater.inflate(

// Here's some code intended to get edittext's values irrelevant to this question...

final Button saveProfile = (Button) profileView.findViewById(;

saveProfile.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {

UpdateProfileForm upf = new UpdateProfileForm(

new UpdateProfile().execute(upf);
return profileView;

} else if (getArguments().getInt(ARG_SECTION_NUMBER) == 2) {
View profileView = inflater.inflate(R.layout.another_fragment, container, false);
return profileView;
} else {
View profileView = inflater.inflate(R.layout.fragment_user_profile_data, container, false);
return profileView;

As you can see, I have a form fragment called
where I get user's data, a
class to store it and pass it to an AsyncTask called
used to update the user information in server side when clicking the

However, if a try that line

new UpdateProfile().execute(upf);


'com.example.UserProfile.this' cannot be referenced from a static context

error will be shown by Android Studio. I was told either to take the "static" property off from the PlaceholderFragment class or to make my AsyncTask static, but neither of them work.

I'm stuck in this so any help would be appreciated!

Answer Source

So, basically what I was doing was wrong since I was using a single fragment (PlaceholderFragment) for three separate responsibilities, as @cricket_007 mentioned in his comment to my question.

I did this because the only TabbedActivity tutorial I found uses it.

Basically, when working with a TabbedActivity, you don't need the PlaceholderFragment so you can easily get rid of it. Now, in the getItem method of the SectionsPagerAdapter class generated within the activity, replace the following line

return PlaceholderFragment.newInstance(position + 1);

with a switch statement for the position argument and return an instance of the appropriate fragment. i.e.:

public Fragment getItem(int position) {
    switch (position) {
        case 0:
            UserProfileData tab1 = new UserProfileData();
            return tab1;
        case 1:
            UserProfileCollections tab2 = new UserProfileCollections();
            return tab2;
        case 2:
            UserProfileBalance tab3 = new UserProfileBalance();
            return tab3;
            return null;

Finally, declare the AsyncTask and the form class into the Fragment class and set the EditTexts' values and button's functionality within the Fragment's onCreateView method, then inflate it and return it.

I hope this answer helps given the lack of samples about this Activity. Thank you for your answers anyways!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download