I created a tabbed activity called UserProfile.java
. As many of you know, this kind of activity comes with a built-in class called PlaceholderFragment
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);
fragment.setArguments(args);
return fragment;
}
@Override
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(
R.layout.fragment_user_profile_data,
container,
false
);
// Here's some code intended to get edittext's values irrelevant to this question...
final Button saveProfile = (Button) profileView.findViewById(R.id.profile_save_data);
saveProfile.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
UpdateProfileForm upf = new UpdateProfileForm(
userName.getText().toString(),
userLastName.getText().toString(),
userID.getText().toString(),
userPhone.getText().toString(),
userEmail.getText().toString(),
userAddress.getText().toString()
);
new UpdateProfile().execute(upf);
view.setVisibility(View.GONE);
}
});
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 fragment_user_profile_data
where I get user's data, a UpdateProfileForm
class to store it and pass it to an AsyncTask called UpdateProfile
used to update the user information in server side when clicking the saveProfile
button.
However, if a try that line
new UpdateProfile().execute(upf);
an
'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!