将长值从ViewPager Activity传递到Fragment时出现空指针异常

时间:2015-12-16 16:13:09

标签: android android-fragments android-viewpager android-bundle

我已经在这里研究了各种主题(Includin被Frankenstein标记为重复的那个)但是无法弄清楚为什么我得到Null pointer。我对这一切都很陌生,所以请保持友善:)

我有一个main_activity,当点击button时,在我的INSERT数据库上运行SQLite命令并返回id。然后我将id传递给我的ViewPager Activity并将其捆绑以传递给最初的Summary fragment。当我在片段getArguments()中使用onCreate时,它会返回null pointer。我已经花了好几个小时,并且无法弄清楚为什么它是null,因为我可以通过bundles成功通过其他事情。

我已使用System.out检查了ID的值,并且它一直填充到getArguments()

我的Main_Activity获取ID:

public class MainActivity extends ListActivity {

…

    private boolean addFactFind(){
        //Get a new sequence number
        newFF();
        //Create new factFind
        System.out.println("THIS IS THE ID in METHOD "+id);
        Intent factFindIntent = new Intent(this, EditFactFind.class);
        factFindIntent.putExtra("id",id);
        System.out.println("THIS IS THE INTENT in METHOD " + factFindIntent);
        //Give fact find new sequence number
        //setSeqOnFrag(newSeqNo);
        //Open new fact find
        startActivity(factFindIntent);
        return true;

    }

    private long newFF() {

        Fact_FindDBHelper dbHelper = new Fact_FindDBHelper(this);
        id = dbHelper.insert();
        // Assign ID to Fact Find
        //EditFactFind.assignFFID(id);
        System.out.println("THIS IS THE FF ID "+id);
        return id;
    }




…

}

我的ViewPager Activity

public class EditFactFind extends FragmentActivity implements NavigationDrawerFragment.NavigationDrawerCallbacks,Summary.TransSummary,Serializable {

    public static final int RESULT_DELETE = -500;
    private boolean isInEditMode = true;
    private boolean isAddingFactFind = true;
    private NavigationDrawerFragment mNavigationDrawerFragment;
    private CharSequence mTitle;
    public String UPDATE_SUMMARY;

    // the pager widget, handles swipes
    private ViewPager mViewPager;
    // the pager adapter, which provides the pages to the view pager widget
    private PagerAdapter mPagerAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.edit_factfind);
        // Locate the viewpager in edit_factfind.xml
       // ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
        // Set the ViewPagerAdapter into ViewPager
       // viewPager.setAdapter(new Pager(getSupportFragmentManager()));

        final EditText titleEditText = (EditText)findViewById(R.id.titleEditText);
        final TextView dateTextView = (TextView)findViewById(R.id.dateTextView);

        mNavigationDrawerFragment = (NavigationDrawerFragment)
                getFragmentManager().findFragmentById(R.id.navigation_drawer); // Activity_nav_drawer.xml
        mTitle = getTitle();

       //Get the ID passed down from the Main_Activity
        Intent mIntent = getIntent();
        long ID = mIntent.getLongExtra("id", 0);
        System.out.println("THIS IS THE ID at EDIT " + ID);

        //Create a bundle to pass the id to the fragment
        Summary sumobj = new Summary();
        Bundle bundle = new Bundle();
        bundle.putLong("id",ID);
        System.out.println(bundle);

        sumobj.setArguments(bundle);


        //Instantiate a ViewPager and a PagerAdapter
        mViewPager=(ViewPager) findViewById(R.id.pager);
        mPagerAdapter = new Pager(getSupportFragmentManager(),bundle);
        mViewPager.setAdapter(mPagerAdapter);

        // Set up the drawer.
        mNavigationDrawerFragment.setUp(
                R.id.navigation_drawer,
                (DrawerLayout) findViewById(R.id.drawer_layout));


        System.out.println(mViewPager);
        System.out.println(mPagerAdapter);



   //     Summary sumFragment = (Summary) mPagerAdapter.instantiateItem(mViewPager, mViewPager.getCurrentItem());

   //     System.out.println(sumFragment);
   //     sumFragment.updateFFID(ID);

     }
…
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }

…



}

最后是我的Summary Fragment

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Bundle args = this.getArguments();
    long id = args.getLong("id",0); // This is where the Null Pointer crops up
    TextView FFID = (TextView) getView().findViewById(R.id.Sum_FFID);
    FFID.setText(String.valueOf(id));
}'

传呼机课程

public class Pager extends FragmentStatePagerAdapter {
    public Bundle fragmentBundle;

    public Pager(android.support.v4.app.FragmentManager fm, Bundle data) {

        super(fm);
        fragmentBundle = data;
    }
    // number of pages to show in slider
    public static final int NUM_PAGES = 28;

    SparseArray<Fragment> registeredFragments = new SparseArray<Fragment>();

    // Tab Titles
    private String tabtitles[] = new String[] { "Summary",
            "Personal",
            …….
            "Checklist"};
    Context context;


    @Override
    public Fragment getItem(int position){

        Fragment fragment = new Summary();

        switch(position) {
            case 0:
                return fragment = new Summary();
            case 1:
                return fragment = new Personal();
   ………
        }

        fragment.setArguments(this.fragmentBundle);
        return fragment;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        registeredFragments.remove(position);
        super.destroyItem(container, position, object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        Fragment fragment = (Fragment) super.instantiateItem(container, position);
        registeredFragments.put(position, fragment);
        return fragment;
    }

    @Override
    public int getCount() {
        return NUM_PAGES;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return tabtitles[position];
    }

    public Fragment getRegisteredFragment(int position){
        return registeredFragments.get(position);
    }
}

0 个答案:

没有答案