我创建了一个名为的选项卡式活动UserProfile.java
。你们很多人都知道,这种活动带有一个名为的内置类PlaceholderFragment
,它基本上就是魔法发生的地方。我的看起来如下:
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;
}
}
}
正如你所看到的,我有一个表单片段,称为fragment_user_profile_data
我获取用户数据的地方,一个UpdateProfileForm
用于存储数据并将其传递给 AsyncTask 的类,用于在单击按钮UpdateProfile
时更新服务器端的用户信息。saveProfile
但是,如果尝试该行
new UpdateProfile().execute(upf);
一个
'com.example.UserProfile.this' 不能从静态上下文中引用
Android Studio 将显示错误。有人告诉我要么从 PlaceholderFragment 类中删除“静态”属性,要么让我的 AsyncTask 静态,但它们都不起作用。
我被困在这所以任何帮助将不胜感激!