1. 程式人生 > >viewpager中操作頁面控制元件時出現空指標異常

viewpager中操作頁面控制元件時出現空指標異常

在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);

就可以了。