为什么我的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个回答

mViewPager获取id了吗?就是findViewById

qq_36416431
qq_36416431 mViewPager= (ViewPager) findViewById(R.id.activity_crime_pager_view_pager);这不是引用了吗??我也出现了类似的问题。。。回复qq_38817183:
2 年多之前 回复
qq_38817183
白马dev 感谢!解决了,不是引用错了布局,是忘了引用,所以就绑定不了id。真想扇自己一巴掌。。。
接近 3 年之前 回复
ziyejinwei1994
ziyejinwei1994 回复qq_38817183: 是不是布局引用错了,这个好像是mViewPager为空了
接近 3 年之前 回复
qq_38817183
白马dev 获取了的
接近 3 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问