ViewPager+Fragment设置监听器后程序崩溃
本来写的ViewPager+Fragment好好地,移动页卡也没问题,但是我加上监听事件以后,再去滑动页卡程序就崩溃强退了。下面是MainActivity代码:
package com.example.myapplication;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.PagerTabStrip;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends FragmentActivity implements ViewPager.OnPageChangeListener {
private List viewList;
private ViewPager pager;
private List titleList;
private PagerTabStrip tabStrip;
private ListfragmentList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化
viewList =new ArrayList();
titleList =new ArrayList();
fragmentList =new ArrayList();
//加载Fragment
fragmentList.add(new MyFragment1());
fragmentList.add(new MyFragment2());
fragmentList.add(new MyFragment3());
fragmentList.add(new MyFragment4());
//为页卡加载标题
titleList.add("第一页");
titleList.add("第二页");
titleList.add("第三页");
titleList.add("第四页");
tabStrip= (PagerTabStrip) findViewById(R.id.tab);
//设置标题属性
tabStrip.setBackgroundColor(Color.BLACK);
tabStrip.setTextColor(Color.WHITE);
tabStrip.setDrawFullUnderline(false);
tabStrip.setTabIndicatorColor(Color.RED);
pager= (ViewPager) findViewById(R.id.pager);
//加载页卡
View view1=View.inflate(this,R.layout.view1,null);
View view2=View.inflate(this,R.layout.view2,null);
View view3=View.inflate(this,R.layout.view3,null);
View view4=View.inflate(this,R.layout.view4,null);
viewList.add(view1);
viewList.add(view2);
viewList.add(view3);
viewList.add(view4);
//设置适配器
MyPagerAdapter pagerAdapter =new MyPagerAdapter(viewList,titleList);
MyFragmentPagerAdapter pagerAdapter1 =new MyFragmentPagerAdapter(getSupportFragmentManager(),fragmentList,titleList);
//加载适配器
//pager.setAdapter(pagerAdapter);
pager.setAdapter(pagerAdapter1);
//加载监听器
pager.addOnPageChangeListener(this);
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
Toast.makeText(this,position,Toast.LENGTH_SHORT).show();
}
@Override
public void onPageScrollStateChanged(int state) {
}
} |
免责声明:本内容仅代表回答会员见解不代表天盟观点,请谨慎对待。
版权声明:作者保留权利,不代表天盟立场。
|
|
|
|