1

hice una app de mandar mail aparte y estoy intentando implementarla dentro de otra app, bien pues me da este error Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

este es mi codigo

public class ContactFragment extends Fragment  {

//public EditText mEmail;
public EditText mSubject;
public EditText mMessage;
private Button buttonSend_contact;

// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";

// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;

public ContactFragment() {
    // Required empty public constructor
}

/**
 * Use this factory method to create a new instance of
 * this fragment using the provided parameters.
 *
 * @param param1 Parameter 1.
 * @param param2 Parameter 2.
 * @return A new instance of fragment ContactFragment.
 */
// TODO: Rename and change types and number of parameters
public static ContactFragment newInstance(String param1, String param2) {
    ContactFragment fragment = new ContactFragment();
    Bundle args = new Bundle();
    args.putString(ARG_PARAM1, param1);
    args.putString(ARG_PARAM2, param2);
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_videos, container, false);

    mMessage = view.findViewById(R.id.messageID);
    mSubject = view.findViewById(R.id.subjectID);

    buttonSend_contact =  view.findViewById(R.id.buttonSendMail);

    buttonSend_contact.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            sendMail();
        }
    });




    return view;
}
private void sendMail() {

    String mail = "[email protected]";
    String message = mMessage.getText().toString();
    String subject = mSubject.getText().toString().trim();

    //Send Mail
    JavaMailAPI javaMailAPI = new JavaMailAPI(getActivity(),mail,subject,message);

    javaMailAPI.execute();

}

este es mi xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ContactFragment">

<!-- <EditText
    android:id="@+id/mailID"
    android:layout_margin="10dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Email"/> -->

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <EditText
        android:id="@+id/subjectID"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:hint="Subject" />

    <EditText
        android:id="@+id/messageID"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:hint="Message" />

    <Button
        android:id="@+id/buttonSendMail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button" />
</LinearLayout>


</FrameLayout>

1 respuesta 1

1

Mi error fue tipografico, me confundi al poner en el inflater un layout que no es, en vez de poner fragment_vides debi poner fragment_contact, por lo demas el codigo funciona perfectamente

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.