我是否可以知道该行中下一个活动的意图?(案例3)它在开关的OncheckedChanged内部。
我遇到了错误
Home.this cannot be referenced from static context
我的活动如下:
......
.....
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = new View(this.getActivity());
ARG_NUMBER = getArguments().getInt(ARG_SECTION_NUMBER, 0);
switch (ARG_NUMBER) {
case 1:
rootView = inflater.inflate(R.layout.fragment_home, container, false);
break;
case 2:
rootView = inflater.inflate(R.layout.fragment_stamp, container, false);
break;
case 3:
rootView = inflater.inflate(R.layout.fragment_setting, container, false);
//to be optimize
Switch passcode_switch = (Switch) rootView.findViewById(R.id.switch1);
passcode_switch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
if(isChecked){
Log.d("Switch State=", "" + "Is checked");
Toast.makeText(getActivity(), "Generated!", Toast.LENGTH_SHORT).show();
final Intent setup_passcode = new Intent(Home.this, Passcode.class);
startActivity(setup_passcode);
}else{
Log.d("Switch State=", "" + "NOT checked");
}
}
});
break;
}
return rootView;
....
....
我不能打电话给这条线的意图:
final Intent setup_passcode = new Intent(Home.this, Passcode.class);
startActivity(setup_passcode);
改变
final Intent setup_passcode = new Intent(Home.this, Passcode.class);
到
final Intent setup_passcode = new Intent(getActivity(), Passcode.class);
使用getActivity()
中获取上下文Fragment
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句