0

Can anyone please help me solve this exception? This is the code of fragment manager and how to show my fragment in activity

private void selectItem(int position) {
    Fragment fragment = null;
    // minus 1 because we have header that has 0 position
    if (position < 1) { // because we have header, we skip clicking on it
        return;
    }
    switch (position) {
        case 2:
            fragment = new TextViewsFragment();
            break;
        case 3:
            //fragment = new ProfilFragment();
            break;
        case 4:
            //fragment = new AttendanceFragment();
            break;
        case 5:
            //fragment = new PermitFragment();
            break;
        case 6:
            //Logout();
            break;
        default:
            break;
    }
    commitFragment(fragment);

    mDrawerList.setItemChecked(position, true);
    setTitle(mDrawerItems.get(position - 1).getTitle());
    mDrawerLayout.closeDrawer(mDrawerList);
}
private class CommitFragmentRunnable implements Runnable {

    private Fragment fragment;

    public CommitFragmentRunnable(Fragment fragment) {
        this.fragment = fragment;
    }

    @Override
    public void run() {
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.content_frame, fragment).commit();
    }
}

public void commitFragment(Fragment fragment) {
    // Using Handler class to avoid lagging while
    // committing fragment in same time as closing
    // navigation drawer
    mHandler.post(new CommitFragmentRunnable(fragment));
}

the exception point to .replace(R.id.content_frame, fragment).commit();

Exception getting :

java.lang.NullPointerException
            at android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:416)
            at android.support.v4.app.BackStackRecord.replace(BackStackRecord.java:451)
            at android.support.v4.app.BackStackRecord.replace(BackStackRecord.java:443)
            at com.csform.android.uiapptemplate.LeftMenusActivity$CommitFragmentRunnable.run(LeftMenusActivity.java:221)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:149)
            at android.app.ActivityThread.main(ActivityThread.java:5268)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
            at dalvik.system.NativeStart.main(Native Method)

i tried to call one fragment into my activity by choose the left menu, but it get exception null pointer.

13
  • 1
    Because you are getting getSupportFragmentManager(); in different thread not ui thread.!! Commented Sep 15, 2015 at 9:56
  • then how to solve this? i want to show my fragment at this activity Commented Sep 15, 2015 at 9:57
  • then how to get the fragment then? i use this code in my another app, but it works normally... Commented Sep 15, 2015 at 10:00
  • You have to call this method in activity not in fragment..!! Commented Sep 15, 2015 at 10:00
  • it's in activity, not fragment -_- see my update Commented Sep 15, 2015 at 10:00

1 Answer 1

1

refer to Mamata Gelanee's comment :

private FragmentManager fragmentManager;
    @Override
    public void onCreate(Bundle savedInstanceState) {
      ......
      fragmentManager = getSupportFragmentManager();
    }

Then in your runnable :

    @Override
    public void run() {
        fragmentManager.beginTransaction()
                .replace(R.id.content_frame, fragment).commit();
    }
Sign up to request clarification or add additional context in comments.

8 Comments

then it's get exception again
android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@21c4a268 that was originally bound here at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:979) at
@lindhadwiambarwati thats different app. its not about your app -.-
i got this exception in my app, when i trying to show my fragment, it not responding and get this exception -_-
still get null exception
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.