[英]How to get around the static reference in a fragment class?
我正在嘗試引用linearLayout,並在fragment類的onViewCreated中使用sharedpreference,但這會產生3個語法錯誤,即“無法對非靜態方法進行靜態引用”。 我知道為什么會這樣,但我想不出辦法。 我嘗試將靜態標識符刪除到fragment類,但這只是導致一個問題導致下一個問題。 而且,我無法將這段代碼放在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);
}
在片段內部時,僅當Fragment
不是靜態的時,才可以直接訪問getApplicationContext()
。 如果是這樣,則為了獲取Context
對象,請使用getActivity()
。
至於從片段布局文件訪問View,則必須從Fragment
rootView調用findViewById()
,該函數在onViewCreated()
回調第一個參數( View
v)上返回。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.