Problema de visibilidad/invisibilidad del botón.

I am working android application that required login, Now I want After successfully login into my application, login button should be invisible and logout button should be visible, but getting errors.

Estoy usando esta función:

login.setVisibility(View.GONE);    &    logout.setVisibility(View.VISIBLE);  

Eclipse give me error in logcat like:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rahul.cheerfoolz/com.rahul.cheerfoolz.CheerfoolznativeActivity}: java.lang.NullPointerException.

Clase principal

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

      session = new SessionID();
      Button login = (Button) findViewById(R.id.home_btn_feature_login);
      Button logout = (Button) findViewById(R.id.home_btn_feature_logout);

         int userID = SessionID.getUserID();

         System.out.println("value of the userID into the main page:====>"+userID);
         //  when user login then here I got the userID value

        if ((userID) > 0) {

            login.setVisibility(View.GONE);  //when Execute this give me error
            logout.setVisibility(View.VISIBLE);  //when Execute this give me error

        } else {

            login.setVisibility(View.VISIBLE); //when Execute this give me error
            logout.setVisibility(View.GONE); //when Execute this give me error

        }

    setContentView(R.layout.main);
    setHeader_home("");
    }

Principal.xml

   <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:orientation="horizontal" >

   <!-- Here Login Button  -->

        <Button
            android:id="@+id/home_btn_feature_login"
            style="@style/HomeButton"
            android:drawableTop="@drawable/login_button"
            android:onClick="onClickFeature"
            android:text="@string/title_feature_login" />

   <!-- Here Logout Button  -->

        <Button
            android:id="@+id/home_btn_feature_logout"
            style="@style/HomeButton"
            android:drawableTop="@drawable/logout_button"
            android:onClick="onClickFeature"
            android:text="@string/title_feature_logout" />

        <Button
            android:id="@+id/home_btn_feature2"
            style="@style/HomeButton"
            android:drawableTop="@drawable/register_button"
            android:onClick="onClickFeature"
            android:text="@string/title_feature_register" />
    </LinearLayout>

preguntado el 12 de junio de 12 a las 11:06

You need to put setContentView(R.layout.main) before you call findViewById(). -

@Pari this is actually the correct answer. -

Thanks, its working now. -

2 Respuestas

Tienes que agregar setContentView(R.layout.main) después de super.onCreate(savedInstanceState); or before Accessing Any View from XML Layout File.Then your Button get refrenced from xml.and in code can't get Button refrence from main.xml .so NullPointer Exception.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);<<<<<<<<<<<<<<ADD THIS LINE


    session = new SessionID();
    Button login = (Button) findViewById(R.id.home_btn_feature_login);
    Button logout = (Button) findViewById(R.id.home_btn_feature_logout);

Respondido el 12 de junio de 12 a las 11:06

@KazekageGaara where is setcontentView()? - Samir Mangrolia

` if ((userID) > 0) { login.setVisibility(View.GONE); //when Execute this give me error logout.setVisibility(View.VISIBLE); //when Execute this give me error } else { login.setVisibility(View.VISIBLE); //when Execute this give me error logout.setVisibility(View.GONE); //when Execute this give me error } setContentView(R.layout.main); setHeader_home(""); ` - kazekage gaara

Agrega esto setContentView(R.layout.main); después de super.onCreate(savedInstanceState);

Respondido el 12 de junio de 12 a las 11:06

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.