0

I have tabs created with FragmentPagerAdapter:

public class SectionsPagerAdapter extends FragmentPagerAdapter {
static final int PAGE_COUNT = 2;

public SectionsPagerAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int index) {
    switch (index) {
    case 0:
        return new SessionDetails();
    case 1:
        return new SessionScoreTable();
    }
    return null;
}

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

I need to update one tab by clicking buttons on another one.

Parent FragmentActivity has:

fragSessionDetails = (SessionDetails) mSectionsPagerAdapter.getItem(0);

and has function:

public void setSessionCompleted(){
    fragSessionDetails.processSessionCompleted();
}

which is called from another tab (SessionScoreTable).

SessionDetails tab has function:

    public void processSessionCompleted(){
    Log.d(TAG,"processSessionCompleted" );
        bSave.setEnabled(false);
        }

bSave is button.

In log I have NullPointerException on row - bSave.setEnabled(false):

06-30 16:20:45.414: D/STS_SessionDetails(15876): processSessionCompleted 06-30 16:20:45.414: D/AndroidRuntime(15876): Shutting down VM 06-30 16:20:45.414: W/dalvikvm(15876): threadid=1: thread exiting with uncaught exception (group=0x41b62d88) 06-30 16:20:45.414: E/AndroidRuntime(15876): Exception 06-30 16:20:45.414: E/AndroidRuntime(15876): FATAL EXCEPTION: main 06-30 16:20:45.414: E/AndroidRuntime(15876): Process: com.vvv.vvvvvvvvvvvvv, PID: 15876 06-30 16:20:45.414: E/AndroidRuntime(15876): java.lang.NullPointerException 06-30 16:20:45.414: E/AndroidRuntime(15876): at com.vvv.vvvvvvvvvvvvv.SessionDetails.processSessionCompleted(SessionDetails.java:237) 06-30 16:20:45.414: E/AndroidRuntime(15876): at com.vvv.vvvvvvvvvvvvv.Session.setSessionCompleted(Session.java:151)

It looks like I lost something but have no idea what. Any help please.

Anusha Harish
  • 382
  • 3
  • 14
Idol
  • 83
  • 6

2 Answers2

0

The line

fragSessionDetails = (SessionDetails) mSectionsPagerAdapter.getItem(0);

Is very bad approach. Better approach will be to access the Fragment in runtime by change the method:

public void setSessionCompleted(){
    ((SessionDetails) mSectionsPagerAdapter.getItem(0)).processSessionCompleted();
}
yshahak
  • 4,996
  • 1
  • 31
  • 37
  • why is it bad? If there are may functions which use the fragment, it's better to create it once. Or I don't count s/th? – Idol Jun 30 '15 at 04:56
  • First of all it is bad because the FragmentAapter may replace the fragment in some case and the reference will be wrong. Second you got the null probably because you called (SessionDetails) mSectionsPagerAdapter.getItem(0) before the adapter actually inflate the layout inside the ViewPager. – yshahak Jun 30 '15 at 05:29
  • If my answer help to you I appreciate if you accept it. – yshahak Jun 30 '15 at 14:31
  • If it would be the case I do not have a row "D/STS_SessionDetails(15876): processSessionCompleted" in the log. Thanks, I've tried but it has not fixed the problem. – Idol Jun 30 '15 at 21:09
0

Finally, I've got a solution.

public class SectionsPagerAdapter extends FragmentPagerAdapter {
static final int PAGE_COUNT = 2;
private SessionDetails fragSessionDetails;
private SessionScoreTable fragSessionScoreTable;

public SectionsPagerAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int index) {
    switch (index) {
    case 0:
        return new SessionDetails();
    case 1:
        return new SessionScoreTable();
    return null;
}

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

@Override
public Object instantiateItem(ViewGroup container, int position) {
    Fragment createdFragment = (Fragment) super.instantiateItem(container, position);
    // save the appropriate reference depending on position
    switch (position) {
        case 0:
            fragSessionDetails = (SessionDetails) createdFragment;
            break;
        case 1:
            fragSessionScoreTable = (SessionScoreTable) createdFragment;
            break;
    }
    return createdFragment;
}

public SessionDetails getFragSessionDetails(){
    return fragSessionDetails;
}

public SessionScoreTable getFragSessionScoreTable(){
    return fragSessionScoreTable;
}}

and

    public void setSessionCompleted(){
    SessionDetails fragSessionDetails = mSectionsPagerAdapter.getFragSessionDetails();
    if (fragSessionDetails != null) {
        Log.d(TAG, "fragSessionDetails");
        fragSessionDetails.processSessionCompleted();
    }
}

Idea from https://stackoverflow.com/a/29287415/2881525

Community
  • 1
  • 1
Idol
  • 83
  • 6