从片段动态创建imageview时遇到问题。这是代码:
public class Fragment_Categorize extends Fragment implements View.OnDragListener, View.OnLongClickListener {
Context context;
public Fragment_Categorize() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment__categorize, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
context = getActivity().getApplicationContext();
ImageView imm = new ImageView(context);
imm.setBackgroundResource(R.drawable.apple);
ImageView black = (ImageView) getActivity().findViewById(R.id.black);
black.setOnLongClickListener(this);
动态创建的imageview在getId()上返回-1,其中静态工作正常。有什么理由吗?