Android 在OncheckedChanged中使用意图
我可以知道我该如何称呼该行下一个活动的意图吗?(案例3) 它是在一个开关的一次检查更改内 我发现了错误Android 在OncheckedChanged中使用意图,android,android-intent,android-fragments,Android,Android Intent,Android Fragments,我可以知道我该如何称呼该行下一个活动的意图吗?(案例3) 它是在一个开关的一次检查更改内 我发现了错误 Home.this cannot be referenced from static context 我的活动如下: ...... ..... @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
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;
....
....
我无法为该线路调用intent:
final Intent setup_passcode = new Intent(Home.this, Passcode.class);
startActivity(setup_passcode);
改变
到
通过在
片段中使用getActivity()
获取上下文您可以自己解决显示toast的问题
Toast.makeText(getActivity(), "Generated!", Toast.LENGTH_SHORT).show();
您使用的是getActivity(),所以,使用getActivity作为获取上下文以用于意图的相同方法
final Intent setup_passcode = new Intent(getActivity(), Passcode.class);
startActivity(setup_passcode);
您好,您能展示一下您实现的整个类吗?:)与问题无关:我建议您使用片段。final Intent setup\u passcode=new Intent(compoundButton.getContext(),passcode.class)代码>
Toast.makeText(getActivity(), "Generated!", Toast.LENGTH_SHORT).show();
final Intent setup_passcode = new Intent(getActivity(), Passcode.class);
startActivity(setup_passcode);