0

I have a series of buttons in my android application and when I use my debugger to see if the program work correctly I saw this message

No such instance field: 'btnEmail'

This is my Fragment as named ContactoFragment.java:

public class ContactoFragment extends Fragment {
    Button btnEmail, btnFacebook, btnTwitter, btnGooglePlus;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        RelativeLayout rootview = (RelativeLayout) inflater.inflate(R.layout.fragment_contacto, container, false);

        btnEmail = (Button) rootview.findViewById(R.id.btnEmail);
        btnEmail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intentEmail = new Intent(Intent.ACTION_SEND);
                intentEmail.setType("text/plain");
                intentEmail.putExtra(Intent.EXTRA_EMAIL,"atellez@iesnervion.es");
                intentEmail.putExtra(Intent.EXTRA_SUBJECT,"Nueva sugerencia");
            }
        });

        btnTwitter = (Button) rootview.findViewById(R.id.btnTwitter);
        btnTwitter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });

        btnFacebook = (Button) rootview.findViewById(R.id.btnFacebook);
        btnFacebook.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });

        btnGooglePlus = (Button) rootview.findViewById(R.id.btnGooglePlus);
        btnGooglePlus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });


        return rootview;
    }
}

And this is my fragment_contacto.xml

<RelativeLayout 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="es.laramira.atellez.moroninfo.Fragments.ContactoFragment">

    <TextView
        android:id="@+id/txtSuggest"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="¿Tienes alguna sugerencia? :)"
        android:textSize="22sp"
        android:textAlignment="center"
        android:textStyle="bold"
        android:textColor="#000"
        android:layout_marginTop="60dp"
        android:layout_centerHorizontal="true"/>

    <Button
        android:id="@+id/btnEmail"
        android:drawableLeft="@drawable/ic_email"
        android:text="Email"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/txtSuggest"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:backgroundTint="#757575"/>

    <Button
        android:id="@+id/btnFacebook"
        android:drawableLeft="@drawable/ic_facebook"
        android:text="Facebook"
        android:layout_marginTop="20dp"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btnTwitter"
        android:layout_alignLeft="@+id/btnTwitter"
        android:layout_alignStart="@+id/btnTwitter"
        android:backgroundTint="#3F51B5"/>

    <Button
        android:id="@+id/btnTwitter"
        android:drawableLeft="@drawable/ic_twitter"
        android:text="Twitter"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_below="@+id/btnEmail"
        android:layout_alignLeft="@+id/btnEmail"
        android:layout_alignStart="@+id/btnEmail"
        android:backgroundTint="#2196F3"/>
    <Button
        android:id="@+id/btnGooglePlus"
        android:drawableLeft="@drawable/ic_google_plus"
        android:text="Google+"
        android:layout_marginTop="20dp"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btnFacebook"
        android:layout_alignLeft="@+id/btnFacebook"
        android:layout_alignStart="@+id/btnFacebook"
        android:backgroundTint="#F44336"/>
</RelativeLayout>

What is the problem? And what is the correct solution?

Nikhil
  • 1,212
  • 13
  • 30
ATellez
  • 115
  • 1
  • 11

5 Answers5

0

Check the R.java code, see if there's any value to your btnEmail. If you are not updating the R or if the xml have some error, it can cause this problem or If you're using pro-guard and obfuscation is true. you have to comment out obfuscation in build gradle. eg: add this in the pro-guard -dontobfuscate So do the build again and see if there is problem on the packages addres, xml or dependecies errors.

mmelotti
  • 336
  • 3
  • 11
0

Try to restart your Android studio and rebuild your project. Maybe you used the btnEmail to your other xml file and having a big size project that the Android Studio cannot find who use the btnEmail well due to logs. Just try to restart your android studio.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
0

You need rename id of button in xml file. Because xml accept lowercase and underscore. It same android:id="@+id/btnFacebook" to android:id="@+id/btn_facebook"

Dungnbhut
  • 176
  • 5
0

Use this below code for your reference.

public class ContactoFragment extends Fragment {
    Button btnEmail, btnFacebook, btnTwitter, btnGooglePlus;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = 
 inflater.inflate(R.layout.fragment_contacto, container, false);

        return view;
    }

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    initalizeLayout(view);
}
private void initalizeLayout(View view) {
btnEmail = (Button) view.findViewById(R.id.btnEmail);
        btnEmail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intentEmail = new Intent(Intent.ACTION_SEND);
                intentEmail.setType("text/plain");
                intentEmail.putExtra(Intent.EXTRA_EMAIL,"atellez@iesnervion.es");
                intentEmail.putExtra(Intent.EXTRA_SUBJECT,"Nueva sugerencia");
            }
        });

        btnTwitter = (Button) view.findViewById(R.id.btnTwitter);
        btnTwitter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });

        btnFacebook = (Button) view.findViewById(R.id.btnFacebook);
        btnFacebook.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });

        btnGooglePlus = (Button) view.findViewById(R.id.btnGooglePlus);
        btnGooglePlus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
}
}
Sathish Kumar VG
  • 2,154
  • 1
  • 12
  • 19
0

I found this answers that could solve your problem, some of the answers says that the event goes to your Activity, and not to the Fragment, where you put the OnClickListener:

How to handle button clicks using the XML onClick within Fragments

Fragments onClick method in fragment element

Community
  • 1
  • 1
Mercedes
  • 89
  • 6