0

I have an activity, which contains multiple fragments. On startup, the activity loads with a fragment which has some buttons. On clicking any of these buttons, a new fragment gets replaced. The replaced fragment also has buttons which on clicking results in the fragment getting replaced by another fragment. I am trying to send data from each of these fragments to the activity directly using interface.Data from fragment 1 gets passed to the activity without any trouble. But the app force closes when I try to send data from the second fragment to the main activity. Please help.

Pictorial Representation of the app I'm trying to build:

Pictorial Representation of the app I'm trying to build

Main Activity(User.java):

public class User extends AppCompatActivity implements user_home.ChoiceSelection,got_1.QuestionOne{

@Override
protected void onCreate(Bundle savedInstanceState)
{
    ActionBar ab = getSupportActionBar();
    ab.hide();
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_user);

    FragmentManager fm = getFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    user_home uh = new user_home();
    ft.add(R.id.fragment_container,uh);
    ft.commit();
}

@Override
public void ChoiceSelected(String choice) {
    if(choice.equals("Game of Thrones"))
        Toast.makeText(getApplicationContext(), "Game of Thrones" , Toast.LENGTH_SHORT).show();
    if(choice.equals("Sherlock"))
        Toast.makeText(getApplicationContext(), "Sherlock" , Toast.LENGTH_SHORT).show();
    if(choice.equals("True Detective"))
        Toast.makeText(getApplicationContext(), "True Detective" , Toast.LENGTH_SHORT).show();
}

@Override
public void QuestionOneA(int ans) {
    Toast.makeText(getApplicationContext(),ans, Toast.LENGTH_SHORT).show();
}

}

First Fragment(user_home.java):

public class user_home extends Fragment{
ChoiceSelection CS;
Button btn1,btn2,btn3;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
 {

     View view = inflater.inflate(R.layout.user_home_layout,container,false);
     btn1=(Button)view.findViewById(R.id.GOT);
     btn2=(Button)view.findViewById(R.id.SH);
     btn3=(Button)view.findViewById(R.id.TD);
     ButtonClicked();
     return view;
 }

public void ButtonClicked()
{
    FragmentManager fm = getFragmentManager();
    final FragmentTransaction ft = fm.beginTransaction();
    btn1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view)
        {
            got_1 g1 = new got_1();
            CS.ChoiceSelected("Game of Thrones");
            ft.replace(R.id.fragment_container,g1);
            ft.commit();
        }
    });

   btn2.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View view) {
           sh_1 sh1 = new sh_1();
           CS.ChoiceSelected("Sherlock");
           ft.replace(R.id.fragment_container,sh1);
           ft.commit();
       }
   });

    btn3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view)
        {
             td_1 td1 = new td_1();
            CS.ChoiceSelected("True Detective");
            ft.replace(R.id.fragment_container,td1);
            ft.commit();
        }
    });
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {
        CS = (ChoiceSelection) activity;
    }
    catch (Exception e){}
}

public interface ChoiceSelection
{
    public void ChoiceSelected(String choice);

}

}

Second Fragment(got_1.java):

public class got_1 extends Fragment{
QuestionOne Q1;
public int score = 0;
Button btn1,btn2,btn3,btn4;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    View view = inflater.inflate(R.layout.got_1_layout,container,false);
    btn1 = (Button)view.findViewById(R.id.A1);
    btn2 = (Button)view.findViewById(R.id.A2);
    btn3 = (Button)view.findViewById(R.id.A3);
    btn4 = (Button)view.findViewById(R.id.A4);
    ButtonClicked();
    return view;
}

public void ButtonClicked()
{
    FragmentManager fm = getFragmentManager();
    final FragmentTransaction ft = fm.beginTransaction();
    final got_2 gt2 = new got_2();
    btn1.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View view)
        {
            score = score-1;
            Q1.QuestionOneA(score);
            score = 0;
            ft.replace(R.id.fragment_container,gt2);
            ft.commit();
        }
    });

    btn2.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View view)
        {
            score = score-1;
            Q1.QuestionOneA(score);
            score = 0;
            ft.replace(R.id.fragment_container,gt2);
            ft.commit();
        }
    });

    btn3.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View view)
        {
            score = score+4;
            Q1.QuestionOneA(score);
            score = 0;
            ft.replace(R.id.fragment_container,gt2);
            ft.commit();
        }
    });

    btn4.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View view)
        {
            score = score-1;
            Q1.QuestionOneA(score);
            score = 0;
            ft.replace(R.id.fragment_container,gt2);
            ft.commit();
        }
    });
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {
        Q1 =  (QuestionOne) activity;
    }
   catch (Exception e){}
}

public interface QuestionOne
{
    public void QuestionOneA(int ans);
}

}

PS: Here is the logcat when the app crashed:

07-31 22:35:38.583 11595-11595/com.example.tonymathew.firstapp E/AndroidRuntime: FATAL EXCEPTION: main
                                                                             Process: com.example.tonymathew.firstapp, PID: 11595
                                                                             android.content.res.Resources$NotFoundException: String resource ID #0x4
                                                                                 at android.content.res.Resources.getText(Resources.java:312)
                                                                                 at android.widget.Toast.makeText(Toast.java:286)
                                                                                 at com.example.tonymathew.firstapp.User.QuestionOneA(User.java:42)
                                                                                 at com.example.tonymathew.firstapp.got_1$3.onClick(got_1.java:70)
                                                                                 at android.view.View.performClick(View.java:5207)
                                                                                 at android.view.View$PerformClick.run(View.java:21168)
                                                                                 at android.os.Handler.handleCallback(Handler.java:746)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                 at android.os.Looper.loop(Looper.java:148)
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5443)
                                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
Tony Mathew
  • 880
  • 1
  • 12
  • 35
  • It's hard to know what caused your app to crash without a logcat – OneCricketeer Jul 31 '16 at 17:04
  • Also, you shouldnt typically use `getFragmentManager` inside a Fragment class. That means you're going to have Fragments inside of Fragments – OneCricketeer Jul 31 '16 at 17:05
  • @cricket_007 : I have added the logcat as you asked. And about the "getFragmentManager inside a Fragment Class" issue, I couldnt find any other method to open a fragment from another fragment. – Tony Mathew Jul 31 '16 at 17:10
  • The duplicate question I flagged is the reason for the crash. Regarding showing Fragments. You use the interface to call back to the Activity and replace from there, inside the Activity. – OneCricketeer Jul 31 '16 at 17:13
  • @cricket_007 : Displaying Integer on toast was the problem. Thank you so much! You are the man! – Tony Mathew Jul 31 '16 at 17:14

0 Answers0