0

I'm practicing Android programming, which uses Java/XML.

As for the code below: the method I'm having trouble with is OBClicked.

OBClicked is a method from an interface I defined in ShareFragment (which contains a checkbox, and a button which launches TextFragment).

I did this, because from what I have learned, defining an interface in a fragment and overriding the interface in the hosting activity is a good way to make it possible to transfer that information to another fragment.

In this case, I override the OBClicked method in MainActivity to tell TextFragment (which is just a TextView) whether the CheckBox from ShareFragment is checked. Depending on whether it's checked, I try to tell TextFragment to dispaly certain text using setText, however this crashes the program. Here's the code, any help would be greatly appreciated. :

MainActivity:

public class MainActivity extends FragmentActivity implements ShareFragment.ClickListener{

    @Override
    public void oBClicked(boolean x){
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();

        TextView t = (TextView)findViewById(R.id.text_view);
        if(x == true){
            t.setText("The box is checked");
        }else{
            t.setText("The box isn't checked");
        }
        Fragment fragment = new TextFragment();
        ft.replace(R.id.fragmentContainer, fragment);
        ft.addToBackStack(null);
        ft.commit();


    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        FragmentManager fm = getSupportFragmentManager();
        Fragment fragment = fm.findFragmentById(R.id.fragmentContainer);
        if(fragment == null){
            fragment = new ShareFragment();
        }
        FragmentTransaction ft = fm.beginTransaction();
        ft.add(R.id.fragmentContainer, fragment);
        ft.commit();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

ShareFragment:

    public class ShareFragment extends Fragment {
        Button mButton; 
    String mText;
    Boolean mTruthValue;
    CheckBox mCheckBox;
    private int mCounter;
    //CROSS FRAGMENT COMMUNICATION START
    ClickListener mListener;
    public interface ClickListener{
        public void oBClicked(boolean x);
    }

    public void oBClicked(boolean x){
        mListener.oBClicked(x);
    }
    //CROSS FRAGMENT COMMUNICATION FIN

    @Override
    public void onAttach(Activity activity){
        super.onAttach(activity);
        try{
            mListener = (ClickListener) activity;
        }
        catch(Exception e){
            //
        }

    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle save){
        View v = inflater.inflate(R.layout.fragment_share, container, false);
        mButton = (Button)v.findViewById(R.id.button1);
        mCheckBox = (CheckBox)v.findViewById(R.id.check_box);
        mCounter = 1;
        mCheckBox.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v){
                mCounter++;
            }
        });
        mButton.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v){
                //mCheckBox = (CheckBox)v.findViewById(R.id.check_box);
                if(mCounter%2 == 0){
                    mTruthValue = true;
                }else{
                    mTruthValue = false;
                }
                oBClicked(mTruthValue);
            }
        });
        return v;
    }

}

TextFragment:

   public class TextFragment extends Fragment{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle save){
        View v = inflater.inflate(R.layout.fragment_text, container, false);
        return v;
    }
}
3
  • any exception/error in stacktrace?
    – Crickcoder
    Jan 15, 2014 at 7:56
  • 1
    Please show the contents of your LogCat Jan 15, 2014 at 7:56
  • please help me to understand, Who R.id.text_view belongs to? To the activity or to the fragment?
    – Blackbelt
    Jan 15, 2014 at 8:14

1 Answer 1

0
   TextView t = (TextView)findViewById(R.id.text_view);
    if(x == true){
        t.setText("The box is checked");
    }else{
        t.setText("The box isn't checked");
    }

This looks for a TextView in your activity. If you want to get the TextView in your TextFragment, you have to find it in the fragment's onCreateView or onViewCreated. To pass data to the fragment on creation, use setArguments and getArguments.

@Override
public void oBClicked(boolean x){
    FragmentManager fm = getSupportFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    Fragment fragment = new TextFragment();

    Bundle arguments = new Bundle();
    arguments.putBoolean("data",x);
    fragment.setArguments(arguments);

    ft.replace(R.id.fragmentContainer, fragment);
    ft.addToBackStack(null);
    ft.commit();
}

and in the onCreateView of the TextFragment...

public class TextFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle save){
    View v = inflater.inflate(R.layout.fragment_text, container, false);
    TextView t = (TextView)v.findViewById(R.id.text_view);

     Bundle arguments = getArguments();
     boolean x = arguments.getBoolean("data");

     if(x == true){
       t.setText("The box is checked");
     }else{
       t.setText("The box isn't checked");
     }   
     return v;
}
0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.