-1

There are many topics on this subject, but I couldn't find any solution that helped my case.

IDEA BEHIND:

What I have is a ViewPager, for swipping between 3 fragments. That works. But in one of the mentioned fragments, I do some functionalities - which then I check in another one of those before mentioned Fragments. Everything works. Then, when I do the check, I open an AlertDialog, telling the user that his configuration is succesfull. By button click, I want that app transfers the user to one different Fragment (not one of the ViewPager fragments).

CODE SNIPPET - FragmentTwo:

public void displayAlertSuccess(Context ctx) {
        new AlertDialog.Builder(ctx)
                .setTitle("ACCESS GRANTED!")
                .setMessage("Congratulations!")
                .setPositiveButton("Great!", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        // continue with
                            replaceFragment();
                    }

                })
                .setNegativeButton("Format my Card", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        mainActivity.setNFC_ACTION("FORMAT");

                    }
                })
                .setIcon(android.R.drawable.ic_dialog_info)
                .show();
    }

    private void replaceFragment() {


        Fragment frag = new CardDesfire1();
        getFragmentManager().beginTransaction().replace(R.id.content_frame, frag).commit();


    }

CODE SNIPPET - XML Layouts:

I. activity_holder.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!-- As the main content view, the view below consumes the entire
     space available using match_parent in both dimensions. -->
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


</LinearLayout>

II. activity_access.xml

<!-- activity_screen_slide.xml -->
<android.support.v4.view.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

III. fragment_access_slide2.xml (fragment that has the Dialog)

<!-- fragment_screen_slide_page.xml -->
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ff0080ff">


    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:id="@+id/imageButton"
        android:src="@drawable/img_access_slide2_2"/>
</ScrollView>

IV. activity_desfire1.xml (Fragment that I want to launch onClick)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:background="@drawable/bg_des2">

    <Button
        android:layout_width="fill_parent"
        android:layout_height="25dp"
        android:text="You have discovered..."
        android:id="@+id/btn_des1_discovered"
        android:background="#3A5FCD"
        android:typeface="monospace"
        android:textColor="#ffffffff"
        android:enabled="false"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <Button
        android:id="@+id/btn_loy"
        android:text="Loyalty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/buttons_shape"
        android:shadowColor="#000000"
        android:textColor="#FFFFFF"
        android:shadowDx="0"
        android:shadowDy="0"
        android:shadowRadius="5"
        android:layout_above="@+id/btn_pay"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:textAllCaps="false"
        android:visibility="invisible"/>
    <Button
        android:id="@+id/btn_pay"
        android:text="&#181;Pay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/buttons_shape"
        android:shadowColor="#000000"
        android:textColor="#FFFFFF"
        android:shadowDx="0"
        android:shadowDy="0"
        android:shadowRadius="5"
        android:textAllCaps="false"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="70dp"
        android:layout_centerHorizontal="true"
        android:visibility="invisible"/>
    <Button
        android:id="@+id/btn_acc"
        android:text="Access"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/buttons_shape"
        android:shadowColor="#000000"
        android:textColor="#FFFFFF"
        android:shadowDx="0"
        android:shadowDy="0"
        android:shadowRadius="5"
        android:textAllCaps="false"
        android:layout_above="@+id/btn_pay"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:visibility="invisible"/>
    <ImageView
        android:layout_width="wrap_content"
        android:visibility="invisible"
        android:layout_height="wrap_content"
        android:id="@+id/img_card_des1"
        android:layout_below="@+id/btn_des1_discovered"
        android:src="@drawable/img_card_des1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="48dp" />


    <TextView
        android:layout_width="fill_parent"
        android:layout_height="100dp"
        android:visibility="invisible"
        android:textSize="18dp"
        android:gravity="center"
        android:text="@string/des1_desc"
        android:id="@+id/tv_des1_desc"
        android:textColor="#ffffffff"
        android:textStyle="italic"
        android:background="#ff8db6cd"
        android:layout_below="@+id/img_card_des1"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="40dp" />

</RelativeLayout>

LINE OF ERROR:

getFragmentManager().beginTransaction().replace(R.id.content_frame, frag).commit();

Error Description from LogCat:

java.lang.NullPointerException

Error occurs when:

My error occurs when the user clicks the button "Great" in AlertDialog, which should then invoke the Fragment replacement.

I have tried:

I have tried to fix it with the getChildFragment , but then I get different kind of error: IllegalStateException : Activity has been destroyed.

Any help is kindly appreciated.

David Kasabji
  • 1,049
  • 3
  • 15
  • 32
  • R.id.content_frame is it correct have you checked it? – keshav kowshik Apr 10 '15 at 07:41
  • Please add all relevant information, like what is `R.id.content_frame` (show the layout XML), the full stack trace of the exception. From what I gather: Try `getActivity.getFragmentManager()`. – dhke Apr 10 '15 at 07:41
  • @Kesh1234 yes that is correct. dhke My mistake, forgot to post the XML's. On it, will update. getActivity didn't help. – David Kasabji Apr 10 '15 at 07:43
  • Did you try breaking that failing compound statement down to identify exactly what is null? – ewan.chalmers Apr 10 '15 at 07:50
  • @sudocode no, I haven't yet, I am not very good with debugging, that's why I posted here first. But if nothing else, I will have to go step-by-step debugg it seems. – David Kasabji Apr 10 '15 at 07:51
  • the exact problem occurs on getFragmentManager(), that results into NullPointerException. – David Kasabji Apr 10 '15 at 08:00

2 Answers2

0

Have you declared default constructor in your CardDesfire1 fragment. If not then declare it

public CardDesfire1(){}

Make your parent Activity extend FragmentActivity

class ParentActivity extends FragmentActivity
Vikalp
  • 2,051
  • 4
  • 18
  • 24
  • Hey Vikalp, I have tried to add the constructor, but it still the same error in the same line of code (null pointer). – David Kasabji Apr 10 '15 at 07:53
  • Now, I think you should debug your code and check what is being null. What I meant to say is that check which method( getFragmentManager() OR beginTransaction() OR replace(R.id.content_frame, frag) OR commit() ) returns null. – Vikalp Apr 10 '15 at 07:58
  • thanks for the response. I have checked it meanwhile, and I already get the nullpointed at getFragmentManager(). However, I already tried getActivity.getSupportFragmentManager() and also getChildFragmentManager(), none helped the issue. – David Kasabji Apr 10 '15 at 07:59
  • If parent activity of fragments extends Activity then make it extend FragmentActivity Instead. – Vikalp Apr 10 '15 at 08:20
  • Yes, my parent activity is MainActivity and it does extend FragmentActivity. Hm, not sure what's going on here.. – David Kasabji Apr 10 '15 at 08:25
  • if you want to know the difference b/w Activity and FragmentActivity then hit this link- [http://stackoverflow.com/questions/10477997/difference-between-activity-and-fragmentactivity] – Vikalp Apr 10 '15 at 08:29
0

The problem occurs because you call getFragmentManager() in a Fragment, you should delegate displaying the dialog to the activity.

Make activity implement an interface, and have the fragment call this interface method when it's time to show the dialog.

Example: You define a new inner interface in FragmentTwo(this code goes in FragmentTwo):

public interface OnReplaceFragmentListener {
    void onReplaceFragment();
}

You link your activity to the fragment(this code goes in FragmentTwo):

private OnReplaceFragmentListener mCallback;

 @Override
public void onAttach(Activity activity) {
    super.onAttach(activity);

    // This makes sure that the container activity has implemented
    // the callback interface. If not, it throws an exception
    try {
        mCallback = (OnReplaceFragmentListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString()
                + " must implement OnReplaceFragmentListener");
    }
}

private void replaceFragment() {
    mCallback.onReplaceFragment();
}

Then make activity implement OnReplaceFragmentListener. And(this code goes into FragmentTwo's hosting activity):

@Override
void onReplaceFragment() {
    Fragment frag = new CardDesfire1();
    getFragmentManager().beginTransaction().replace(R.id.content_frame, frag).commit();
}
Zoltan Ersek
  • 755
  • 9
  • 28