从 PlaceHolderFragment 执行 AsyncTask:“'com.example.Activity.this' 无法从静态上下文中引用”

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

我创建了一个名为 UserProfile.java 的选项卡 activity。正如你们许多人所知,这种 activity 带有一个名为 PlaceholderFragment 的内置 class,这基本上就是魔法发生的地方。我的看起来如下:

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 class 用于存储它并将其传递给名为 UpdateProfile用于在点击saveProfile按钮时更新服务器端的用户信息。

但是,如果尝试那行

new UpdateProfile().execute(upf);

一个

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

错误将由 Android Studio 显示。我被告知要么从 PlaceholderFragment class 中删除 "static" 属性 要么使我的 AsyncTask 静态化,但它们都不起作用。

我被困在这个问题上,如有任何帮助,我们将不胜感激!

您不能从静态方法调用 MyActivity.this。 这是一个解决方法。

像这样在 Activity/Fragment 中声明一个变量:

private static Activity activity;

然后在你的 onCreate():

activity = this;
// OR
activity = getActivity(); // if you are in fragment

然后,在您的 UpdateProfile() AsyncTask 方法中,这样调用它:

activity.someMethod();

从片段中删除静态关键字。

public static class PlaceholderFragment extends Fragment 

并在单独的 class 中移动 UpdateProfile AsyncTask。现在它看起来像是另一个片段的内部 class。

所以,基本上我所做的是错误的,因为我使用单个片段 (PlaceholderFragment) 来承担三个不同的职责,正如 中提到的@cricket_007。

我这样做是因为我发现只有 TabbedActivity tutorial 使用它。

基本上,在使用 TabbedActivity 时,您不需要 PlaceholderFragment,因此您可以轻松摆脱它。现在,在 SectionsPagerAdapter class 中生成的 activity 的 getItem 方法中,替换以下行

return PlaceholderFragment.newInstance(position + 1);

位置参数的 switch 语句和 return 适当片段的实例。即:

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;
        default:
            return null;
    }
}

最后,将 AsyncTask 和表单 class 声明到 Fragment class 中,并在 Fragment 的 onCreateView 方法中设置 EditTexts 的值和按钮的功能,然后对其进行膨胀和 return它。

我希望这个答案能有所帮助,因为缺少关于此 Activity 的样本。无论如何感谢您的回答!