只是徘徊爲什麼下一個代碼與出現「NoSuchMethodException:onPrefImageClick [類android.view.View]」消息。NoSuchMethodException:在的onClick片段
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()
與
android:onClick="onPrefImageClick"
存在於XML。
而這個代碼:
@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()
雖然與兩個從XML線和除去onPrefImageClick()方法;
工作得很好。
能/會有人能解釋嗎?
您是否在不支持該方法的較低版本的設備上運行? –
'public void onPrefImageClick'將這個方法寫在Fragment Activity而不是Fragment中。 – user370305