Frage

I have been all over stackoverflow and a bunch of other sites for a few days full time and still wasn't able to resolve this.

I can define UI in fragment but cant interact with them like using setText() or handling clicks and each time i face runtime error and in the logcat :

Error in creating fragment.

I've tried so many solutions that seemed to work for other people but still nothing! it would be really nice if you could help here is the code for fragment:

public class FragmentContact extends Fragment implements OnClickListener {

Button btnSend;

public FragmentContact() {
}

@Override
public void onStart() {
    super.onStart();
}

// -----------------------------------------------
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater
            .inflate(R.layout.fragment_contact, container, false);

    try {
         IToast toast = new IToast(getActivity(), inflater);
         btnSend = ((Button) getActivity().findViewById(R.id.btnSend));
        EditText etMail = (EditText) getActivity().findViewById(R.id.etMail);

        //causes error:
        btnSend.setOnClickListener(this);
        //notice that i AM implementing onclicklistener!

        //also causes error:
        btnSend.setText("send");

    } catch (Exception x) {
        Toast.makeText(getActivity(), x.getMessage(), Toast.LENGTH_LONG)
                .show();
        return rootView;
    }

    return rootView;
}


@Override
public void onClick(View arg0) {
    Toast.makeText(getActivity(), "onclick handled", Toast.LENGTH_SHORT)
    .show();
}

}

and here is the code for main, where i call it:

    @SuppressLint("NewApi")
private void displayView(int position) {

    android.app.Fragment fragment = null;
    switch (position) {
    case 0:
        break;
    case 1:
        fragment = new FragmentHome();
        break;
    case 2:
        break;
    case 3:
        break;
    case 4:
        break;
    case 5:
        break;
    case 8://CONTACT_FRAGMENT---------------------------
        try{
        fragment = new FragmentContact();
        }catch(Exception x){
            toast.show(x.getMessage());
        }
        break;
        //----------------------------------------------
    case 9:
        fragment = new FragmentSuggest();
        break;
    case 10:
        Intent i=new Intent(getBaseContext(),VoxReceiverAct.class);
        startActivity(i);
        break;
    case 11:

        break;

    default:
        break;
    }

    if (fragment != null) {
        FragmentManager fragmentManager = getFragmentManager();

        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.frame_container, fragment);
        fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        //fragmentTransaction.setCustomAnimations(R.animator.slide_in_left,
                //R.animator.slide_out_right);
        fragmentTransaction.commit();


    } else {
        Log.e("MainActivity", "Error in creating fragment");
    }
}
War es hilfreich?

Lösung

This

btnSend = ((Button) getActivity().findViewById(R.id.btnSend));

Should be

btnSend = (Button) rootView.findViewById(R.id.btnSend));

cause the view belongs to the Fragment Layout.

Also it is better you remove the try catch in Fragment. You will know the cause by looking at the stacktrace. There is no need to display the exception in Toast.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top