I want to find a ToggleButton in a non-activity class ,but i get a nullpointerException .i did like next code:
public class ClassMode{
public static boolean isClassMode(Context c) {
ToggleButton t = new ToggleButton(c);
t = (ToggleButton) t.findViewById(R.id.classmode);
if (t.isChecked()) {
return true;
} else {
return false;
}
}
public static void setNextMode(final Context context){
if(isClassMode){
dosomething();
}
}
}
did i make anything wrong? i am a new progromer,i suppose this question maybe nothing.but for me is in contrary.
Anybody help me ,please!Looking forward for your reply.
3Q!
findViewByIdwill find a view within the object you call it on. Usually it’s called on an activity, but can also be called on anyViewGroupthat has children.On your case, you call it on a
Buttonobject. I doubt this button contains a child with idR.id.classmode.Try:
(Note: you should post a LogCat with the actual exception you’re getting, else we’re only guessing)