Reputation: 395
Just wandering why the next code comes up with a "NoSuchMethodException: onPrefImageClick [class android.view.View]" message.
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState)
{
View mView = inflater.inflate(R.layout.pref_detail_fragment, container, false);
return mView;
} // onCreateView()
public void onPrefImageClick(final View clickedView)
{
switch(clickedView.getId())
{
case R.id.prefDetailImage:
Log.i(TAG, "Clicked on the image");
break;
case R.id.prefDetailText:
Log.i(TAG, "Clicked on the text");
break;
default:
Log.i(TAG, "Clicked some where");
}
} // onPrefImageClick()
with
android:onClick="onPrefImageClick"
present in the xml.
And this code:
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState)
{
View mView = inflater.inflate(R.layout.pref_detail_fragment, container, false);
final ImageView imgView = (ImageView) mView.findViewById(R.id.prefDetailImage);
imgView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.i(TAG, "Clicked on the image");
// Perform action on click
}
});
return mView;
} // onCreateView()
While with both the line from xml and the onPrefImageClick() method removed;
works just fine.
Can / will someone explain please?
Upvotes: 3
Views: 938
Reputation: 2305
Just remove the final and put this method on the fragment activity and not the fragment itself!
Hope it helps!
Upvotes: 0
Reputation: 1866
You should put the onPrefImageClick
in the Activity which hosts the Fragments.
This is because, Android will look for the method in the Activity not in the Fragment. Android doesn't know for sure, which Fragment is currently up and hence it looks in the Activity.
Upvotes: 9