qq_38817183
白马dev
采纳率100%
2017-07-23 16:58 阅读 1.8k
已采纳

为什么我的FragmentStatePagerAdapter返回空值

package com.example.administrator.criminalintent;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;

import java.util.List;
import java.util.UUID;

/**

  • Created by Administrator on 2017/7/23 0023. */

public class CrimePagerActivity extends FragmentActivity {
private ViewPager mViewPager;
private List mCrimes;
private static final String EXTRA_CRIME_ID="com.example.android.criminalintent.crime_id";
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mViewPager= (ViewPager) findViewById(R.id.activity_crime_pager_view_pager);

    mCrimes=CrimeLab.get(this).getCrimes();
    FragmentManager fragmentManager=getSupportFragmentManager();
    UUID crimeId= (UUID) getIntent().getSerializableExtra(EXTRA_CRIME_ID);
    FragmentStatePagerAdapter adapter=new FragmentStatePagerAdapter(fragmentManager) {
        @Override
        public Fragment getItem(int position) {
            Crime crime=mCrimes.get(position);

            return  CrimeFragment.newInstance(crime.getmId());
        }

        @Override
        public int getCount() {
            return mCrimes.size();
        }
    };
    mViewPager.setAdapter(adapter);//报错说我的adapter是空的

    for(int i=0;i<mCrimes.size();i++){
        if(mCrimes.get(i).getmId().equals(crimeId)){
            mViewPager.setCurrentItem(i);
            break;
        }
    }

}
public static Intent newIntent(Context context, UUID crimeId){
    Intent intent=new Intent(context,CrimePagerActivity.class);
    intent.putExtra(EXTRA_CRIME_ID,crimeId);
    return intent;
}

}

//这下面是exception

07/24 00:51:55: Launching app
Cold swapped changes.
$ adb shell am start -n "com.example.administrator.criminalintent/com.example.administrator.criminalintent.CrimeListActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Client not ready yet..Waiting for process to come online
Waiting for process to come online
Connected to process 7959 on device New_Device_API_25 [emulator-5554]
I/OpenGLRenderer: Initialized EGL, version 1.4
D/OpenGLRenderer: Swap behavior 1
W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
D/OpenGLRenderer: Swap behavior 0
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.administrator.criminalintent, PID: 7959
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.administrator.criminalintent/com.example.administrator.criminalintent.CrimePagerActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.view.ViewPager.setAdapter(android.support.v4.view.PagerAdapter)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.view.ViewPager.setAdapter(android.support.v4.view.PagerAdapter)' on a null object reference
at com.example.administrator.criminalintent.CrimePagerActivity.onCreate(CrimePagerActivity.java:46)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
at android.app.ActivityThread.-wrap12(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6119) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
Application terminated.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    ziyejinwei1994 ziyejinwei1994 2017-07-24 01:23

    mViewPager获取id了吗?就是findViewById

    点赞 评论 复制链接分享

相关推荐