我在片段中有一个imageView。在imageView上单击我想要开始一项活动。下面是我的代码显示空指针异常。
public static class DemoObjectFragment extends Fragment {
// public static final String ARG_OBJECT = "object";
Context con=getActivity();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_collection_object, container, false);
Bundle args = getArguments();
ImageView shadow_img=(ImageView) rootView.findViewById(R.id.shadow_img);
ImageView img=(ImageView)rootView.findViewById(R.id.img);
img.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(getActivity(), Article.class);
getActivity().startActivity(intent);
}
});
shadow_img.setImageResource(R.drawable.featured_shadow);
new ImageLoadTask(args.getString("img_url"),(ImageView)rootView.findViewById(R.id.img),con).execute(null, null);
return rootView;
}
}
答案 0 :(得分:0)
此代码运行正常,请尝试更改。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup rootView = null;
rootView = (ViewGroup) inflater.inflate(R.layout.layname, container, false);
Button btnSec = (Button)rootView.findViewById(R.id.btnSec);
btnSec.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent iz = new Intent(getActivity(), SecondActivity.class);
startActivity(iz)