从Fragment移动到Activity,扩展了FragmentActivity

时间:2016-04-07 03:12:00

标签: android android-fragments android-viewpager android-fragmentactivity

我有登录片段,成功验证后我正在导航到一个活动" AfterLogin"其中有page viewPager。使用以下代码:

Toast.makeText(getActivity(), "Login Successfull", Toast.LENGTH_LONG).show();
                progressDialog.dismiss();
                Intent intent = new Intent(getActivity(),AfterLogin.class);
                startActivity(intent);
            }
        });

我在AfterLogin.java中有以下代码,这是我的第二个活动 - 从片段导航。

public class AfterLogin extends FragmentActivity {
    ViewPager viewPager=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        viewPager = (ViewPager) findViewById(R.id.pager);

        FragmentManager fragmentManager = getSupportFragmentManager();

        viewPager.setAdapter(new MyAdapter(fragmentManager));

        ActionBar actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
    }


}

class MyAdapter extends FragmentStatePagerAdapter {
    public MyAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {

        Fragment fragment = null;
        //Log.d("info", "get item called" +position);
        if(position == 0){

            fragment = new AttendanceResultFeed();
        }

        if(position == 1){

            fragment = new CollegeFeedFrag();
        }

        if(position == 2){

            fragment = new BranchFeedFrag();
        }
        return fragment;
    }

    @Override
    public int getCount() {
        // Log.d("info", "get count called");
        return 3;
    }

    @Override
    public CharSequence getPageTitle(int position) {

        if (position == 0){

            return "Attendance and Result";
        }

        if (position == 1){

            return "College Feeds";
        }

        if (position == 2){

            return "Branch Feeds";
        }
        return super.getPageTitle(position);
    }

在此过程中,我收到错误:

AfterLogin}:

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:2298)
                                                                                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
                                                                                at android.app.ActivityThread.access$800(ActivityThread.java:144)
                                                                                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
                                                                                at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                at android.os.Looper.loop(Looper.java:135)
                                                                                at android.app.ActivityThread.main(ActivityThread.java:5221)
                                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                                at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
                                                                             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.rakesh_kr.master_piece.AfterLogin.onCreate(AfterLogin.java:26)
                                                                                at android.app.Activity.performCreate(Activity.java:5937)
                                                                                at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
                                                                                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
                                                                                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
                                                                                at android.app.ActivityThread.access$800(ActivityThread.java:144) 
                                                                                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
                                                                                at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                at android.os.Looper.loop(Looper.java:135) 
                                                                                at android.app.ActivityThread.main(ActivityThread.java:5221) 
                                                                                at java.lang.reflect.Method.invoke(Native Method) 

任何人都可以帮我解决这个问题。

如果我从一个单独的项目中运行AfterLogin,它可以正常工作。

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

试试这个:

startActivity(new Intent(getApplicationContext(), XXXX.class));

告诉我它是怎么回事