大家好,我正在设置 ViewPager,但我一直收到 Set Adapter 错误。此代码链接到 PagerAdapter。有人可以帮忙吗?
package com.test.viewpager;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;
public class PagerAdapter extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MyPagerAdapter adapter = new MyPagerAdapter();
ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(2);
}
}
这是我需要运行的 mypageradapter
package com.test.viewpager;
import android.content.Context;
import android.os.Parcelable;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
class MyPagerAdapter extends PagerAdapter {
public int getCount() {
return 5;
}
public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int resId = 0;
switch (position) {
case 0: resId = R.layout.page1;
break;
case 1: resId = R.layout.page2;
break;
case 2: resId = R.layout.page3;
break;
case 3: resId = R.layout.page4;
break;
case 4: resId = R.layout.page5;
break; }
View view = inflater.inflate(resId, null);
((ViewPager) collection).addView(view, 0);
return view; }
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2); }
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1); }
public Parcelable saveState() {
return null; }}
这是我使用的主要 xml
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/myfivepanelpager"/>
</LinearLayout>