-5

I am developing one android app in which I have use this code - Link Here

Code Works Well using support-library's Fragment: android.support.v4.app.Fragment

I have placed One button in third_frag.xml with button id buttonfg3

Now when user click on that button text on that fragment should be get copied

I have modified ThirdFragment

public class ThirdFragment extends Fragment {
Button b1;
TextView ed1;
private ClipboardManager myClipboard;
private ClipData myClip;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    ed1=(TextView) container.findViewById(R.id.tvFragThird);
    b1 = (Button) container.findViewById(R.id.buttonfg3);
    myClipboard = (ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE);

    b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String text;
            text = ed1.getText().toString();

            myClip = ClipData.newPlainText("text", text);
            myClipboard.setPrimaryClip(myClip);

            Toast.makeText(getActivity().getApplicationContext(), "Text Copied", Toast.LENGTH_SHORT).show();
        }
    });


    View v = inflater.inflate(R.layout.first_frag, container, false);

    TextView tv = (TextView) v.findViewById(R.id.tvFragFirst);
    tv.setText(getArguments().getString("msg"));


    return v;
}


public static FirstFragment newInstance(String text) {

    FirstFragment f = new FirstFragment();
    Bundle b = new Bundle();
    b.putString("msg", text);

    f.setArguments(b);

    return f;
}

}

but when i place setOnClickListener code ,Application stops working..

Community
  • 1
  • 1
kshubham
  • 3
  • 4

1 Answers1

0

You have to inflate layout xml file for fragment first, and then you have to find button and edittext from that xml file.

So, move your View v = inflater.inflate(R.layout.first_frag, container, false); line to the top as it becomes first statement inside onCreateView

and then find edittext and button like this :

ed1=(TextView) v.findViewById(R.id.tvFragFirst);
b1 = (Button) v.findViewById(R.id.buttonfg3);
Bhargav Thanki
  • 4,924
  • 2
  • 37
  • 43