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>