在 Fragment 类中为 ImageView 添加 setOnClickListener 时遇到问题。由于 NullPointerException,当我单击 ImageView 时发生错误。
下面是我的代码:
public final class TestFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ImageView image = new ImageView(getActivity());
image.setTag(mContent);
image.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
HomeActivity h = new HomeActivity();
String url = h.getEventUrl();
Intent childClass = new Intent(getActivity(), HomeEventDetailActivity.class);
childClass.putExtra("eventUrl",url);
TabGroupActivity parentActivity = (TabGroupActivity)h.getParent();
parentActivity.startChildActivity("Child_Main1", childClass);
}
});
imageManager.displayImage(mContent, image, R.drawable.icon);
image.setScaleType(ScaleType.FIT_XY);
LinearLayout layout = new LinearLayout(getActivity());
layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
layout.setGravity(Gravity.CENTER);
layout.addView(image);
return layout;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString(KEY_CONTENT, mContent);
setUserVisibleHint(true);
}
}
请帮忙!!!!