I have a custom Fragment that inflates it’s content from test.xml
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.test, container, true);
return v;
}
Inside of test.xml, I have a toggle button defined like so:
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="update"
/>
When I click on the toggle button, I get the following error:
Could not find a method update(View) in the activity class
android.view.ContextThemeWrapper for onClick handler on view class
android.widget.ToggleButton
I have the following method defined in both the calling activity and the fragment, but neither is getting called:
public void update(View view) {
Log.d(LOG_TAG, "starting update!");
}
I’m confused.
When you are creating the LayoutInflater you are creating, probably, with the ContextThemeWrapper and to work you need to create with the activity context.
I had similar problem. I was creating like this:
(LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
then I done like this:
(LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
and worked.
I hope I have helped.