在按钮的onClick中,LinearLayout为null

时间:2016-01-14 11:38:13

标签: android android-layout nullpointerexception findviewbyid

我试图在点击按钮上隐藏线性布局并显示另一个布局文件中的线性布局。但是在按钮的onclick中,linearlayouts为空。

displayLayouteditLayout为空。

        displayLayout = (LinearLayout) findViewById(R.id.linearLayout);
    editLayout = (LinearLayout) findViewById(R.id.editLayout); 
    edit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            displayLayout = (LinearLayout) findViewById(R.id.linearLayout);
            editLayout = (LinearLayout) findViewById(R.id.editLayout);
            displayLayout.setVisibility(View.GONE); //error occurs here
            editLayout.setVisibility(View.VISIBLE);
        }
    });
}

这是布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.prematixsofs.taxiapp.EditUserDetails">

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/ScrollView01"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"

    >
    <!--Display UserDetails Layout-->

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/displayLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="vertical"
            android:padding="15dp">

            <TextView
                android:id="@+id/name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="5dp"
                android:background="@drawable/custom_edittext"
                android:gravity="center"
                android:padding="8dp"
                android:paddingLeft="10dp"
                android:paddingRight="5dp" />

            <TextView
                android:id="@+id/phNo"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:background="@drawable/custom_edittext"
                android:gravity="center"
                android:padding="8dp"
                android:paddingLeft="10dp"
                android:paddingRight="5dp" />

            <TextView
                android:id="@+id/email"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:background="@drawable/custom_edittext"
                android:gravity="center"
                android:inputType="textEmailAddress"
                android:padding="8dp"
                android:paddingLeft="10dp"
                android:paddingRight="5dp" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="vertical"
            android:padding="15dp">

            <Button
                android:id="@+id/edit"
                android:layout_width="300dp"
                android:layout_height="wrap_content"
                android:background="@drawable/button_custom"
                android:text="Edit"
                android:textColor="#ffffff" />
        </LinearLayout>

    </LinearLayout>
</ScrollView>

<!--Edit Layout -->
<LinearLayout
    android:id="@+id/editLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"

    >

    <EditText
        android:id="@+id/editName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:background="@drawable/custom_edittext"
        android:gravity="center"
        android:padding="8dp"
        android:paddingLeft="10dp"
        android:paddingRight="5dp" />

    <EditText
        android:id="@+id/editPhNo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:background="@drawable/custom_edittext"
        android:gravity="center"
        android:padding="8dp"
        android:paddingLeft="10dp"
        android:paddingRight="5dp" />

    <EditText
        android:id="@+id/editPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:background="@drawable/custom_edittext"
        android:gravity="center"
        android:padding="8dp"
        android:paddingLeft="10dp"
        android:paddingRight="5dp" />

    <EditText
        android:id="@+id/editConfirmPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:background="@drawable/custom_edittext"
        android:gravity="center"
        android:padding="8dp"
        android:paddingLeft="10dp"
        android:paddingRight="5dp" />

</LinearLayout>

2 个答案:

答案 0 :(得分:1)

只需在代码中修复您的视图ID

即可
    displayLayout = (LinearLayout) findViewById(R.id.displayLayout);
    editLayout = (LinearLayout) findViewById(R.id.editLayout); 
    edit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            displayLayout.setVisibility(View.GONE);
            editLayout.setVisibility(View.VISIBLE);
        }
    });
}

答案 1 :(得分:0)

错误代码 displayLayout =(LinearLayout)findViewById(R.id.linearLayout);

正确代码 displayLayout =(LinearLayout)findViewById(R.id.displayLayout);

你设置了一个不存在的id。