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

时间:2014-07-27 13:17:48

标签: java android android-fragments

我正在尝试引用linearLayout,并在片段类的onViewCreated中使用共享首选项,但这会产生3个语法错误,说“无法对非静态方法进行静态引用”。我理解为什么会这样,但我无法找到解决方法。我尝试将静态标识符删除到片段类,但这只是从一个问题引导到下一个问题。并且,我不能将此代码放在onCreate()中,因为我在片段中引用了视图。

带有静态错误的代码行是:

 getApplicationContext()
 findViewById()
 FillInInfo(v);

我可以通过使其成为静态来轻松修复FillInInfo(v),但我仍然发布它以防万一我不必使它静止。

这是片段类:

 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));
    }
}

请记住,此代码块必须保持在一起:

  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);

        }

我的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);

}

1 个答案:

答案 0 :(得分:1)

在片段内部,只有getApplicationContext()不是静态时才能直接访问Fragment。如果是,要获取Context对象,请使用getActivity()

至于从片段布局文件访问您的视图,您必须从findViewById() rootView调用FragmentonViewCreated()回调第一个参数({{1} } v)。