0

I have this function in my Activity extends Fragment, and i want a botton calls this function in onClick but when i click on this button the app closes and in the Run box appears "java.lang.IllegalStateException: Could not find method misanuncios(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'btn_misanuncios'"

public class HomeFragment extends Fragment {
    private FirebaseAuth.AuthStateListener mAuthListener;

    private Button btnLogOut, btnmisanuncios;
    private int CAMERA_REQUEST_CODE = 0;
    private ProgressDialog progressDialog;
   // private StorageReference mStorage;
    //private ImageView imageProfile;
    private TextView textName;



    private FirebaseAuth mAuth = FirebaseAuth.getInstance();
    String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();


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


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        //PON TITULO BAR
        ((AppCompatActivity) getActivity()).getSupportActionBar().setTitle("Perfil");

        //AGAFAR VALORS XML EN FRAGMENTS
        View view = inflater.inflate(R.layout.fragment_home, container, false);

        final TextView textName = (TextView) view.findViewById(R.id.txtName);
        final TextView textEmail = (TextView) view.findViewById(R.id.txtEmail);
        //final TextView Name_nav = (TextView) toolbar.findViewById(R.id.Name_nav);
        // PARA ACCEDER FIREBASE DATABASE POSAR PRIMER FUNCIO CREAR PRIMER

        //final TextView Name_nav = (TextView) headerView.findViewById(R.id.Name_nav);

        DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
        DatabaseReference uidRef = rootRef.child("Usuarios").child(uid);
        ValueEventListener eventListener = new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                final String name = dataSnapshot.child("name").getValue(String.class);
                final String email = dataSnapshot.child("email").getValue(String.class);
                //System.out.println(name);
                textName.setText(name);
                textEmail.setText(email);
                //Name_nav.setText(name);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {}
        };
        uidRef.addListenerForSingleValueEvent(eventListener);


        //CERRAR SESION CON BOTON
        btnLogOut = (Button) view.findViewById(R.id.btn_logout);
        btnLogOut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mAuth.getCurrentUser() != null)
                    mAuth.signOut();
                    Intent intent = new Intent(getActivity(), LoginActivity.class);
                    startActivity(intent);
            }
        });

        return view;
    }

     public void misanuncios(View view) {
    Intent intent2 = new Intent(getActivity(), HomeFragment.class);
    startActivity(intent2);
}
public void ajustes(View view) {
}
public void contacto(View view) {
}

    @Override
    public void onPause() {

        super.onPause();
    }

}

The error is in this function:

 public void misanuncios(View view) {
        Intent intent2 = new Intent(getActivity(), HomeFragment.class);
        startActivity(intent2);
    }

btn_misanuncios is the id of button:

<Button
        android:id="@+id/btn_misanuncios"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:onClick="misanuncios"
        android:text="Mis Anuncios"
        tools:ignore="OnClick" />
Fairy Band
  • 45
  • 1
  • 6

1 Answers1

0

whenever you have Fragment and if you want to define click listener by just defining android:onClick attribute then you have to define a method with the same name in actual activity from where HomeFragment has been called.

https://stackoverflow.com/a/18896666/886148

OR and otherwise, you can simply implement a click listener programmatically like this. (haven't run the code but you get the logic)

public class HomeFragment extends Fragment {
private FirebaseAuth.AuthStateListener mAuthListener;

private Button btnLogOut, btnmisanuncios;
private int CAMERA_REQUEST_CODE = 0;
private ProgressDialog progressDialog;
// private StorageReference mStorage;
//private ImageView imageProfile;
private TextView textName;


private FirebaseAuth mAuth = FirebaseAuth.getInstance();
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();


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


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    //PON TITULO BAR
    ((AppCompatActivity) getActivity()).getSupportActionBar().setTitle("Perfil");

    //AGAFAR VALORS XML EN FRAGMENTS
    View view = inflater.inflate(R.layout.fragment_home, container, false);

    final TextView textName = (TextView) view.findViewById(R.id.txtName);
    final TextView textEmail = (TextView) view.findViewById(R.id.txtEmail);
    //final TextView Name_nav = (TextView) toolbar.findViewById(R.id.Name_nav);
    // PARA ACCEDER FIREBASE DATABASE POSAR PRIMER FUNCIO CREAR PRIMER

    //final TextView Name_nav = (TextView) headerView.findViewById(R.id.Name_nav);

    DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
    DatabaseReference uidRef = rootRef.child("Usuarios").child(uid);
    ValueEventListener eventListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            final String name = dataSnapshot.child("name").getValue(String.class);
            final String email = dataSnapshot.child("email").getValue(String.class);
            //System.out.println(name);
            textName.setText(name);
            textEmail.setText(email);
            //Name_nav.setText(name);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    };
    uidRef.addListenerForSingleValueEvent(eventListener);


    //CERRAR SESION CON BOTON
    btnLogOut = (Button) view.findViewById(R.id.btn_logout);
    btnmisanuncios = (BUTTON) VIEW.findViewById(R.id.btn_misanuncio)
    btnLogOut.setOnClickListener(this);
    btnmisanuncios.setOnClickListener(this);
    btnLogOut.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mAuth.getCurrentUser() != null)
                mAuth.signOut();
            Intent intent = new Intent(getActivity(), LoginActivity.class);
            startActivity(intent);
        }
    });

    return view;
}

}

@Override
public void onClick(View v) {


    switch (v.getId()) {
        case R.id.btn_logout:
            // implements your things
            break;
        case R.id.btn_misanuncio:
            Intent intent2 = new Intent(getActivity(), HomeFragment.class);
            startActivity(intent2);
        default:
            break;
    }
}

public void ajustes(View view) {
}

public void contacto(View view) {
}

@Override
public void onPause() {

    super.onPause();
}

}
wick.ed
  • 473
  • 7
  • 15