简体   繁体   English

如何绕过片段类中的静态引用?

[英]How to get around the static reference in a fragment class?

I'm trying to reference a linearLayout, and use a sharedpreference in the onViewCreated of a fragment class, yet this creates 3 syntax errors saying "cannnot make a static reference to a non-static method." 我正在尝试引用linearLayout,并在fragment类的onViewCreated中使用sharedpreference,但这会产生3个语法错误,即“无法对非静态方法进行静态引用”。 I understand why this is happening, but I can't figure out a way around it. 我知道为什么会这样,但我想不出办法。 I tried deleting the static identifier to the fragment class, but that just lead from one problem to the next. 我尝试将静态标识符删除到fragment类,但这只是导致一个问题导致下一个问题。 And, I can't put this code in the onCreate() because I'm referencing views in the fragment. 而且,我无法将这段代码放在onCreate()中,因为我是在片段中引用视图。

The lines of code with the static error are: 带有静态错误的代码行是:

 getApplicationContext()
 findViewById()
 FillInInfo(v);

I can fix the FillInInfo(v) one easily by making it static, but I still posted it just in case I don't have to make it static. 我可以通过将其静态化来轻松修复FillInInfo(v),但我还是发布了它,以防万一我不必使其静态化。

Here's the fragment class: 这是片段类:

 public static class PlaceholderFragment extends Fragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    private static final String ARG_SECTION_NUMBER = "section_number";

    /**
     * Returns a new instance of this fragment for the given section number.
     */
    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;
    }

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_manage_day,
                container, false);


        TextView textView = (TextView) rootView
                .findViewById(R.id.section_label);
        textView.setText(Integer.toString(getArguments().getInt(
                ARG_SECTION_NUMBER)));
        return rootView;
    }

    public void onViewCreated(View v, Bundle savedInstanceState) {
        super.onViewCreated(v, savedInstanceState);

        SharedPreferences activitiesFile = getApplicationContext().getSharedPreferences("Activities", 0);
        Set<String> keylist = activitiesFile.getAll().keySet();
        for (String s : keylist) {
            String active = activitiesFile.getString(s, "");
            Button activeName=new Button(getActivity());
            activeName.setText(active);
            activeName.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 
                    LayoutParams.WRAP_CONTENT));
            LinearLayout layout=(LinearLayout) findViewById(R.id.ActivityList);
            activeName.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    FillInInfo(v);
                }
            });
            layout.addView(activeName);

        }

    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        ((ManageDay) activity).onSectionAttached(getArguments().getInt(
                ARG_SECTION_NUMBER));
    }
}

Please keep in the mind that this code block has to stay together: 请记住,此代码块必须保持在一起:

  SharedPreferences activitiesFile = getApplicationContext().getSharedPreferences("Activities", 0);
        Set<String> keylist = activitiesFile.getAll().keySet();
        for (String s : keylist) {
            String active = activitiesFile.getString(s, "");
            Button activeName=new Button(getActivity());
            activeName.setText(active);
            activeName.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 
                    LayoutParams.WRAP_CONTENT));
            LinearLayout layout=(LinearLayout) findViewById(R.id.ActivityList);
            activeName.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    FillInInfo(v);
                }
            });
            layout.addView(activeName);

        }

Code to my FillInInfo() method: 代码到我的FillInInfo()方法:

 public void FillInInfo(View view){
    Intent intent=new Intent(this,ActivityInfo.class);
    Button button=(Button)view;
    String buttonName=button.getText().toString();
    intent.putExtra("Name",buttonName);

    SharedPreferences preferences = getPreferences(MODE_PRIVATE);
    SharedPreferences.Editor editor = preferences.edit();
    editor.putInt("count",count);

    startActivity(intent);

}

When inside a Fragment, you can access getApplicationContext() directly only if the Fragment is not Static. 在片段内部时,仅当Fragment不是静态的时,才可以直接访问getApplicationContext() If it is, for getting a Context object, use getActivity() . 如果是这样,则为了获取Context对象,请使用getActivity()

As for accessing your Views from the fragment layout file, you have to call findViewById() from your Fragment rootView, that is returned on the onViewCreated() callback first parameter ( View v). 至于从片段布局文件访问View,则必须从Fragment rootView调用findViewById() ,该函数在onViewCreated()回调第一个参数( View v)上返回。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM