gpt4 book ai didi

java - 如何绕过 fragment 类中的静态引用?

转载 作者:行者123 更新时间:2023-12-01 12:42:22 27 4
gpt4 key购买 nike

我试图引用线性布局,并在 fragment 类的 onViewCreated 中使用共享首选项,但这会产生 3 个语法错误,指出“无法对非静态方法进行静态引用”。我明白为什么会发生这种情况,但我无法找到解决方法。我尝试删除 fragment 类的静态标识符,但这只会导致一个问题又一个问题。而且,我无法将此代码放入 onCreate() 中,因为我正在引用 fragment 中的 View 。

出现静态错误的代码行是:

 getApplicationContext()
findViewById()
FillInInfo(v);

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

这是 fragment 类:

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

}

最佳答案

在 Fragment 内部时,仅当 Fragment 不是静态时,您才可以直接访问 getApplicationContext()。如果是,要获取 Context 对象,请使用 getActivity()

至于从 fragment 布局文件访问 View ,您必须从 Fragment rootView 调用 findViewById(),该 rootView 在 onViewCreated( ) 回调第一个参数 (View v)。

关于java - 如何绕过 fragment 类中的静态引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24981344/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com