2012-06-12 66 views
0

我的工作,需要登錄Android應用程序,現在我想在成功登錄到我的應用程序,登錄按鈕應該是不可見的退出按鈕應該是可見的,但得到的錯誤。按鈕可見性/隱形問題。

我使用這個功能:

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

Eclipse中給我的錯誤在logcat中,如:

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

Main.class

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(""); 
    } 

main.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> 
+4

你需要把的setContentView(R.layout.main)調用findViewById()之前。 – Dhruvisha

+0

@Pari這實際上是正確答案。 –

+0

謝謝,它現在工作。 –

回答

4

您必須在super.onCreate(savedInstanceState);before Accessing Any View from XML Layout File之後添加setContentView(R.layout.main)。然後,您的Button從xml.and refrenced,並且在代碼中無法從main.xml .so獲得Button refrence NullPointer異常。

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); 
+1

它在那裏。第二最後一行。 –

+0

@KazekageGaara where setcontentView()? –

+0

'如果((用戶ID)> 0){ login.setVisibility(View.GONE); //執行時,這給我錯誤 logout.setVisibility(View.VISIBLE); //執行時,這給我錯誤 }其他{ login.setVisibility(View.VISIBLE); //當執行這個給我錯誤 logout.setVisibility(View.GONE); //執行時,這給我錯誤 } 的setContentView(R.layout.main); setHeader_home(「」); ' –

4

添加此setContentView(R.layout.main);super.onCreate(savedInstanceState);

+0

謝謝,它現在工作。 –