0

大家好,我正在设置 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> 
4

2 回答 2

1

您命名了您的活动类PagerAdapter,这也是android.support.v4.view.PagerAdapter.

以其他方式命名它,您会发现真正的错误,您忘记import android.support.v4.view.PagerAdapter;在 MyPagerAdapter.java 中

于 2012-10-27T03:06:05.240 回答
-2

如果您发布一些有关您遇到的错误的信息,例如 LogCat 或堆栈跟踪甚至错误消息,这也会有所帮助。

尽管有些错误很突出,但我们在没有所有信息的情况下猜测您的问题是错误的。您可能会遇到与您的代码无关的问题。

于 2012-10-26T23:39:26.710 回答