0

I have a problem with a button that modifies the visibility of a LinearLayout I always get the error java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.LinearLayout.setVisibility(int)' on a null object reference

I have this function that creates a variable of the LinearLayout that I want to display

Exclusive_Login.java

public void showbutton(){
        LinearLayout btn_publicar = (LinearLayout) findViewById(R.id.btn_publicar);
        btn_publicar.setVisibility(View.VISIBLE);
    }

And it runs on this line

Exclusive_Login.java

ini_ses.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String obtusuario = usuario.getText().toString();
                String obtcontra = contrasena.getText().toString();
                if (obtusuario.equals("ADMIN") && obtcontra.equals("admin")){
                    showbutton();
                    Toast.makeText(getBaseContext(),"Datos Correctos",Toast.LENGTH_LONG).show();
                    //Intent accion = new Intent(getBaseContext(), BarraNavegacionDesplegable.class);
                    //startActivity(accion);


                }else{
                    Toast.makeText(getBaseContext(),"Introduzca correctamente los datos",Toast.LENGTH_LONG).show();
                }

            }
        });

This is Fragment_Home.xml where I reference the LinearLayout

Fragment_Home.xml

<LinearLayout
    android:id="@+id/btn_publicar"
    android:visibility="invisible"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:layout_margin="5dp"
    android:orientation="horizontal"
    android:gravity="center"
    android:background="@drawable/public_contenedor1">

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_marginRight="20dp"
        app:srcCompat="@drawable/lapiz1" />

    <Button
        android:id="@+id/publicnotice1"
        android:layout_width="280dp"
        android:layout_height="45dp"
        android:background="@drawable/style_bottom"
        android:text="Crear publicaciones"
        android:textAllCaps="false"
        android:textColor="@color/black"
        android:textSize="15sp"
        android:textStyle="bold" />

</LinearLayout>
2
  • I bet Exclusive_Login is your activity and you add a fragment component (i.e. Fragment_Home) to it, right? Commented Nov 17, 2022 at 9:57
  • In case it is a fragment component, try to locate the fragment first. HomeFragment fragment = (HomeFragment) getFragmentManager().findFragmentById(R.id.Fragment_Home); Commented Nov 17, 2022 at 10:05

1 Answer 1

0

Try with declare the layout gobally

LinearLayout  btn_publicar;

 btn_publicar = (LinearLayout) findViewById(R.id.btn_publicar);

put it on onViewCreated. And in the function

public void showbutton(){
        btn_publicar.setVisibility(View.VISIBLE);
    }

Not the answer you're looking for? Browse other questions tagged or ask your own question.