0

I've researched various topics on here (Includin the one marked as duplicate by Frankenstein) about this but can't figure out why I'm getting a Null pointer. I am new to all this as well so please be nice :)

I have a main_activity that when a button is clicked runs an INSERT command on my SQLite db and returns the id. I then pass the id down to my ViewPager Activity and bundle it up to pass to the initial Summary fragment. When I use getArguments() in the fragments onCreate it's returning a null pointer. I've spent hours on this and can't figure out why it's null as I can pass successfully via bundles in other things.

I've checked the value of the id using System.out and it's populated all the way up til the getArguments()

My Main_Activity that gets the 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;
    }




…

}

My 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);
    }

…



}

and finally my 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));
}'

Pager Class

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);
    }
}
KT79
  • 89
  • 1
  • 2
  • 10
  • 1
    please provide the implementation of your Pager Class where you are sending you bundle. – harshitpthk Dec 16 '15 at 16:17
  • try to use `Bundle args = this.getArguments();` line inside onCreateView method – ρяσѕρєя K Dec 16 '15 at 16:21
  • @humblebee - sorry, added pager class – KT79 Dec 16 '15 at 16:26
  • @ρяσѕρєяK thank you for the suggestion but get the same problem in the onCreateView() – KT79 Dec 16 '15 at 16:30
  • "i am new to this so please be nice"?! Shoo! Back to the school desk! >:) On a serious note, voting to leave closed as lacking an [MCVE] = not showing research effort. Looks like a pretty generic "i dunno how to debug, do this for me" issue so far. – ivan_pozdeev Dec 16 '15 at 17:10
  • 1
    @ivan_pozdeev As I said I've went through and can get the value of the variable right up to the point of getting it at the onCreate method of the fragment and done a load of reading so to say it's lacking research effort is unfair. I don't expect anyone to do debugging for me, I was more looking for if I was calling the getArguments within the wrong place i.e. as suggested it should maybe have been in the onCreateView. – KT79 Dec 16 '15 at 17:18
  • I still fail to find the ins and outs of what's happening with the current formulations (or rather not willing to go for the efforts needed 'cuz the writer didn't bother to do it when writing). [To a person from the side, It basically reads](http://blogs.msmvps.com/jonskeet/2010/08/29/writing-the-perfect-question/): "stuff happens, something in this pile is wrong. Find it for me in this blanket of code." – ivan_pozdeev Dec 16 '15 at 17:56
  • I fail to see the point of your snide remarks, if you don't want to help then fine. I've made efforts on this and I'm stuck hence why I'm asking for help. If there's something that can be done better then I'm more than happy to be pointed in the right direction. I'm a beginner as mentioned. What's the point in this place if you can't ask for help without some arrogant know it all slating every question? Anyway hope it made you feel like a big boy. – KT79 Dec 16 '15 at 19:26
  • The point is: I don't feel like dumpster-diving in a badly written question but do feel like educating the author so less people have the displeasure of doing this in the future. – ivan_pozdeev Dec 16 '15 at 19:39
  • Well would it not be more worthwhile saying why it's a badly written question as your input has been anything but educating – KT79 Dec 16 '15 at 19:59

0 Answers0