简体   繁体   中英

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." 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. And, I can't put this code in the onCreate() because I'm referencing views in the fragment.

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.

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:

 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. If it is, for getting a Context object, use 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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