ViewPager的报错:ViewPager does not have a PagerAdapter set



03-14 22:13:14.211 17310-17310/tech.androidstudio.tablayoutviewpagerdemo E/AndroidRuntime: FATAL EXCEPTION: main
03-14 22:13:14.211 17310-17310/tech.androidstudio.tablayoutviewpagerdemo E/AndroidRuntime: Process: tech.androidstudio.tablayoutviewpagerdemo, PID: 17310
03-14 22:13:14.211 17310-17310/tech.androidstudio.tablayoutviewpagerdemo E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{tech.androidstudio.tablayoutviewpagerdemo/tech.androidstudio.tablayoutviewpagerdemo.MainActivity}: java.lang.IllegalArgumentException: ViewPager does not have a PagerAdapter set
03-14 22:13:14.211 17310-17310/tech.androidstudio.tablayoutviewpagerdemo E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2314)
03-14 22:13:14.211 17310-17310/tech.androidstudio.tablayoutviewpagerdemo E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2386)
03-14 22:13:14.211 17310-17310/tech.androidstudio.tablayoutviewpagerdemo E/AndroidRuntime:     at android.app.ActivityThread.access$800(ActivityThread.java:148)
03-14 22:13:14.211 17310-17310/tech.androidstudio.tablayoutviewpagerdemo E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)
03-14 22:13:14.211 17310-17310/tech.androidstudio.tablayoutviewpagerdemo E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
03-14 22:13:14.211 17310-17310/tech.androidstudio.tablayoutviewpagerdemo E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:135)
03-14 22:13:14.211 17310-17310/tech.androidstudio.tablayoutviewpagerdemo E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5310)
03-14 22:13:14.211 17310-17310/tech.androidstudio.tablayoutviewpagerdemo E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
03-14 22:13:14.211 17310-17310/tech.androidstudio.tablayoutviewpagerdemo E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)
03-14 22:13:14.211 17310-17310/tech.androidstudio.tablayoutviewpagerdemo E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
03-14 22:13:14.211 17310-17310/tech.androidstudio.tablayoutviewpagerdemo E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
03-14 22:13:14.211 17310-17310/tech.androidstudio.tablayoutviewpagerdemo E/AndroidRuntime:   Caused by: java.lang.IllegalArgumentException: ViewPager does not have a PagerAdapter set
03-14 22:13:14.211 17310-17310/tech.androidstudio.tablayoutviewpagerdemo E/AndroidRuntime:     at android.support.design.widget.TabLayout.setupWithViewPager(TabLayout.java:612)
03-14 22:13:14.211 17310-17310/tech.androidstudio.tablayoutviewpagerdemo E/AndroidRuntime:     at tech.androidstudio.tablayoutviewpagerdemo.MainActivity.onCreate(MainActivity.java:34)
03-14 22:13:14.211 17310-17310/tech.androidstudio.tablayoutviewpagerdemo E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:5953)
03-14 22:13:14.211 17310-17310/tech.androidstudio.tablayoutviewpagerdemo E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1128)
03-14 22:13:14.211 17310-17310/tech.androidstudio.tablayoutviewpagerdemo E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2267)
03-14 22:13:14.211 17310-17310/tech.androidstudio.tablayoutviewpagerdemo E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2386) 
03-14 22:13:14.211 17310-17310/tech.androidstudio.tablayoutviewpagerdemo E/AndroidRuntime:     at android.app.ActivityThread.access$800(ActivityThread.java:148) 
03-14 22:13:14.211 17310-17310/tech.androidstudio.tablayoutviewpagerdemo E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292) 
03-14 22:13:14.211 17310-17310/tech.androidstudio.tablayoutviewpagerdemo E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102) 
03-14 22:13:14.211 17310-17310/tech.androidstudio.tablayoutviewpagerdemo E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:135) 
03-14 22:13:14.211 17310-17310/tech.androidstudio.tablayoutviewpagerdemo E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5310) 
03-14 22:13:14.211 17310-17310/tech.androidstudio.tablayoutviewpagerdemo E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method) 
03-14 22:13:14.211 17310-17310/tech.androidstudio.tablayoutviewpagerdemo E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372) 
03-14 22:13:14.211 17310-17310/tech.androidstudio.tablayoutviewpagerdemo E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901) 

03-14 22:13:14.211 17310-17310/tech.androidstudio.tablayoutviewpagerdemo E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696) 


原因分析:

因为没有设置adapter 就开始使用ViewPager了,例如我这里mTabLayout.setupWithViewPager(mViewPager);就在mViewPager.setAdapter(adapter);的前面使用了。所以报错了。

package tech.androidstudio.tablayoutviewpagerdemo;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import java.util.ArrayList;
import java.util.List;

import tech.androidstudio.tablayoutviewpagerdemo.adapter.TabLayoutFragmentPagerAdapter;

public class MainActivity extends AppCompatActivity {

    private TabLayout mTabLayout;
    private ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTabLayout = (TabLayout)findViewById(R.id.tablayout);
        mViewPager = (ViewPager)findViewById(R.id.viewPager);

        List<Fragment> mList = new ArrayList<Fragment>();
        mList.add(new FirstFragment());
        mList.add(new SecondFragment());
        mList.add(new ThirdFragment());

        TabLayoutFragmentPagerAdapter adapter = new TabLayoutFragmentPagerAdapter(getSupportFragmentManager(),mList);

        //这一步是最关键,将tablayout viewpager 连接起来了   mTabLayout.setupWithViewPager(mViewPager);

        mViewPager.setAdapter(adapter);
    }
}


解决方法:

修改就是在使用之前进行adapter设置。

就是调换mTabLayout.setupWithViewPager(mViewPager);和mViewPager.setAdapter(adapter);的位置。如下


package tech.androidstudio.tablayoutviewpagerdemo;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import java.util.ArrayList;
import java.util.List;

import tech.androidstudio.tablayoutviewpagerdemo.adapter.TabLayoutFragmentPagerAdapter;

public class MainActivity extends AppCompatActivity {

    private TabLayout mTabLayout;
    private ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTabLayout = (TabLayout)findViewById(R.id.tablayout);
        mViewPager = (ViewPager)findViewById(R.id.viewPager);

        List<Fragment> mList = new ArrayList<Fragment>();
        mList.add(new FirstFragment());
        mList.add(new SecondFragment());
        mList.add(new ThirdFragment());

        TabLayoutFragmentPagerAdapter adapter = new TabLayoutFragmentPagerAdapter(getSupportFragmentManager(),mList);

        mViewPager.setAdapter(adapter);
        //这一步是最关键,将tablayout viewpager 连接起来了  mTabLayout.setupWithViewPager(mViewPager);


    }
}


 
 

你可能感兴趣的:(ViewPager的报错:ViewPager does not have a PagerAdapter set)