大家好我設置了一個ViewPager,但我不斷收到Set Adapter的錯誤。此代碼鏈接到PagerAdapter.Can任何人都可以幫助嗎?setAdapter(adapter);?
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);
}
}
這裏是我需要運行
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; }}
這裏mypageradapter是,即時通訊使用
<?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>
安置自己的'MyPagerAdapter'代碼,請忘了
import android.support.v4.view.PagerAdapter;
。 – Ralgha請注意logcat。 – Geobits
好吧,我會這麼做 – YES