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.

主类

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

主.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

2 回答 2

4

您必须添加setContentView(R.layout.main)aftersuper.onCreate(savedInstanceState);before Accessing Any View from XML Layout File。然后您的 Button 从 xml 获取 refrenced。并且在代码中无法从 main.xml 获取 Button refrence .so 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);
于 2012-06-12T10:10:55.600 回答
4

setContentView(R.layout.main);之后添加这个super.onCreate(savedInstanceState);

于 2012-06-12T10:13:19.593 回答