0

我创建了一个名为的选项卡式活动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 静态,但它们都不起作用。

我被困在这所以任何帮助将不胜感激!

4

3 回答 3

1

所以,基本上我所做的事情是错误的,因为我使用一个片段 ( PlaceholderFragment) 来承担三个单独的职责,正如@cricket_007 在他对我的问题的评论中提到的那样。

我这样做是因为我发现的唯一TabbedActivity 教程使用了它。

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

return PlaceholderFragment.newInstance(position + 1);

使用位置参数的 switch 语句并返回适当片段的实例。IE:

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 和表单类声明到 Fragment 类中,并在 Fragment 的onCreateView方法中设置 EditTexts 的值和按钮的功能,然后将其膨胀并返回。

鉴于缺乏有关此活动的样本,我希望此答案有所帮助。无论如何,谢谢您的回答!

于 2016-10-22T22:30:48.240 回答
0

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

Activity在/中声明一个变量,Fragment如下所示:

private static Activity activity;

然后在你的onCreate()

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

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

activity.someMethod();
于 2016-10-20T20:13:21.833 回答
0

从 Fragment 中删除 static 关键字。

public static class PlaceholderFragment extends Fragment 

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

于 2016-10-20T20:16:35.097 回答