ViewPager闪退
package com.example.viewpage;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
public class MainActivity extends Activity {
private ViewPager page;
private List<View> viewList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewList=new ArrayList<View>();
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);
page=(ViewPager) findViewById(R.id.pager);
MyViewPagerAdapter adapter=new MyViewPagerAdapter(viewList);
page.setAdapter(adapter);
}
}
package com.example.viewpage;
import java.util.List;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
public class MyViewPagerAdapter extends PagerAdapter{
private List<View>viewList;
public MyViewPagerAdapter( List<View>viewList) {
// TODO 自动生成的构造函数存根
this.viewList=viewList;
}
@Override
public int getCount() {
// TODO 自动生成的方法存根
return viewList.size();
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO 自动生成的方法存根
return arg0==arg1;
}
@Override
public Object instantiateItem(View container, int position) {
// TODO 自动生成的方法存根
((ViewGroup) container).addView(viewList.get(position));
return viewList.get(position);
}
@Override
public void destroyItem(View container, int position, Object object) {
// TODO 自动生成的方法存根
((ViewGroup) container).removeView(viewList.get(position));
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" >
<android.support.v4.view.PagerTabStrip
android:id="@+id/tab"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</android.support.v4.view.PagerTabStrip>
<android.support.v4.view.PagerTitleStrip
android:id="@+id/pagertitle"
android:layout_width="fill_parent"
android:layout_height="45dp"
android:layout_gravity="bottom"
/>
</android.support.v4.view.ViewPager>
</LinearLayout>