将数据从多个片段发送到活动

时间:2016-07-31 16:50:08

标签: java android android-fragments android-fragmentactivity

我有一个包含多个片段的活动。在启动时,活动加载一个包含一些按钮的片段。单击任何这些按钮时,将替换新的片段。替换的片段也有按钮,在点击时导致片段被另一个片段替换。 我试图直接使用interface将来自每个片段的数据发送到活动。片段1中的数据传递给活动没有任何问题。但是,当我尝试将数据从第二个片段发送到主要活动时,应用程序强制关闭。请帮忙。

我尝试构建的应用的图片表示:

Pictorial Representation of the app I'm trying to build

主要活动(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:这是应用程序崩溃时的logcat:

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)

0 个答案:

没有答案