在viewpager中添加linearlayout的点击事件出现空指针异常。
LinearLayout mylocation=(LinearLayout) findViewById(R.id.my_location);
mylocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent();
intent.setClass(MainActivity.this,SecondActivity.class);
startActivity(intent);
}
});
因为viewpager每个页面有一个View,每个View都有一个布局,所以在对页面进行操作时,需要用这个页面的View进行操作,
所在页面的View 对象是,
private View view1;
所以改为
LinearLayout mylocation=(LinearLayout) view1.findViewById(R.id.my_location);
就可以了。
本文解决在使用ViewPager时LinearLayout点击事件导致的空指针异常问题。通过正确获取当前页面的View对象,实现LinearLayout点击跳转功能。

594

被折叠的 条评论
为什么被折叠?



