Custom listener toujours null
Bonjour à tous !
J'ai créé à ma sauce le bouton switch suivant :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| public class MySwitch extends RelativeLayout
{
public interface OnCheckListener
{
void onCheckedEvent(View event);
}
private OnCheckListener onCheckListener = null;
public void setOnCheckListener(OnCheckListener onCheckListener) {
this.onCheckListener = onCheckListener;
}
public void setChecked(boolean checked) {
this.checked = checked;
Log.e("[TRACE]", "this.onCheckListener : " + this.onCheckListener);
if(onCheckListener != null)
{
onCheckListener.onCheckedEvent(this);
}
}
} |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| public class parameter_fragment extends Fragment
{
private Context context = null;
private MySwitch service = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.parameters_fragment,container,false);
context = getActivity().getBaseContext();
Log.e("[TRACE]", "**onCreateView**");
return view;
}
@Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity);
Log.e("[TRACE]", "**onAttach**");
}
@Override
public void onDetach() {
// TODO Auto-generated method stub
super.onDetach();
}
@Override
public void onStart() {
// TODO Auto-generated method stub
super.onStart();
Log.e("[TRACE]", "**onStart**");
parametersList();
}
private void parametersList() {
// TODO Auto-generated method stub
service = new MySwitch(context);
service.setOnCheckListener(new MySwitch.OnCheckListener() {
@Override
public void onCheckedEvent(View event) {
// TODO Auto-generated method stub
Log.e("[TRACE]", "service");
if(service.isChecked())
{
Intent intent = new Intent(context, iLogService.class);
context.startService(intent);
}
else
{
Intent intent = new Intent(context, iLogService.class);
context.stopService(intent);
}
}
});
}
} |
Dès que mon switch fait appel à la méthode
Code:
1 2 3 4 5 6 7 8
| public void setChecked(boolean checked) {
this.checked = checked;
Log.e("[TRACE]", "this.onCheckListener : " + this.onCheckListener);
if(onCheckListener != null)
{
onCheckListener.onCheckedEvent(this);
}
} |
la console m'affiche : this.onCheckListener : this.onCheckListener = null :(
Une idée ? Merci d'avance :)