What could be the error launching the application? An error occurred while adding a button handler. Here is the code:

package com.drusilovihi.tabbedapp_b1; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageButton; import android.widget.Toast; public class TabFragment1 extends Fragment implements View.OnClickListener { private Mail m ; private ImageButton imgSend; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { m = new Mail("*************", "*******"); imgSend.findViewById(R.id.imageButton); imgSend.setOnClickListener(this); return inflater.inflate(R.layout.tab_fragment_1, container, false); } public void sendEmail(View view){ String[] toArr = {"toemail1@domain1.tld"}; // This is an array, you can add more emails, just separate them with a coma m.setTo(toArr); // load array to setTo function m.setFrom("fromEmail@domain.tld"); // who is sending the email m.setSubject("subject"); m.setBody("your message goes here"); try { //m.addAttachment("/sdcard/myPicture.jpg"); // path to file you want to attach if(m.send()) { // success Toast.makeText(getActivity(), "Email was sent successfully.", Toast.LENGTH_LONG).show(); } else { // failure Toast.makeText(getActivity() ,"Email was not sent.", Toast.LENGTH_LONG).show(); } } catch(Exception e) { // some other problem Toast.makeText(getActivity(), "There was a problem sending the email.", Toast.LENGTH_LONG).show(); } } @Override public void onClick(View v) { Toast.makeText(getActivity(), "Нажата кнопка.", Toast.LENGTH_LONG).show(); sendEmail(v); } } 

Glassrays:

06-17 17: 56: 10.732 13330-13330 / com.drusilovihi.tabbedapp_b1 E / AndroidRuntime: FATAL EXCEPTION: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.widget.ImageButton.findViewById ( int) 'on a null object reference android.support.v4.app.Fragment.performCreateView (Fragment.java:1962) android.support.v4.app.FragmentManagerImpl.moveToState (FragmentManager.java:1067‌) android.support.v4 .app.FragmentManagerImpl.moveToState (FragmentManager.java:1248‌)

  • Please add error text as well as StackTrace. - Geslot pm
  • 06-17 17: 56: 10.732 13330-13330 / com.drusilovihi.tabbedapp_b1 E / AndroidRuntime: FATAL EXCEPTION: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.widget.ImageButton.findViewById ( int) 'on a null object reference android.support.v4.app.Fragment.performCreateView (Fragment.java:1962) android.support.v4.app.FragmentManagerImpl.moveToState (FragmentManager.java:1067) android.support.v4. app.FragmentManagerImpl.moveToState (FragmentManager.java:1248) - Dmitriy Dev
  • or tell me an example of smtp client android implementation - Dmitriy Dev

2 answers 2

You are trying to get a button like this:

 imgSend.findViewById(R.id.imageButton); 

But this way you can get the widget only in the activation. In Fragmegte, you must first get the View the markup itself through inflate, then search already in this View your widget.

The code in this answer . We rootView markup in the rootView , then we look for our button in this rootView , and not in a vacuum, as you have now

     @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.tab_fragment_1, container, false); imgSend = rootView.findViewById(R.id.imageButton); imgSend.setOnClickListener(this); m = new Mail("*************", "*******"); return rootView; } 
    • I tried 3 ways of the handler and with all the launch error of the application ... - Dmitriy Dev