How to hide include layout inside custom dialog in android

时间:2017-12-18 06:46:23

标签: android android-layout

I have two layout under <include> and i want to hide on radio button checked.

My XML code

<LinearLayout
        android:id="@+id/maid_full_emp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
        <include
            layout="@layout/maid_full_emp"

            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:visibility="gone"
            tools:layout_editor_absoluteY="8dp"
            tools:layout_editor_absoluteX="8dp" />
        </LinearLayout>
    <LinearLayout
        android:id="@+id/maid_part_emp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
        <include
            layout="@layout/maid_part_emp"

            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            tools:layout_editor_absoluteY="8dp"
            tools:layout_editor_absoluteX="8dp" />
    </LinearLayout>

Custom dialog code

Logcat output

FATAL EXCEPTION: main
    Process: aminfocraft.harsh.mymaidfinder, PID: 29548
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.LinearLayout.setVisibility(int)' on a null object reference
        at aminfocraft.harsh.mymaidfinder.Activity.MaidSearchEmpPak.MaidSearchEmpNew.onCheckedChanged(MaidSearchEmpNew.java:429)
        at android.widget.CompoundButton.setChecked(CompoundButton.java:165)
        at aminfocraft.harsh.mymaidfinder.Activity.MaidSearchEmpPak.MaidSearchEmpNew.assignViews1(MaidSearchEmpNew.java:370)
        at aminfocraft.harsh.mymaidfinder.Activity.MaidSearchEmpPak.MaidSearchEmpNew.searchUser(MaidSearchEmpNew.java:386)
        at aminfocraft.harsh.mymaidfinder.Activity.MaidSearchEmpPak.MaidSearchEmpNew.onOptionsItemSelected(MaidSearchEmpNew.java:296)
        at android.app.Activity.onMenuItemSelected(Activity.java:3219)
        at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:380)
        at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:195)

1 个答案:

答案 0 :(得分:1)

use R.id instead of R.layout

maid_full_emp = (LinearLayout )dialog.findViewById(R.id.maid_full_emp);
maid_part_emp = (LinearLayout )dialog.findViewById(R.id.maid_part_emp);

instead of

maid_full_emp = (LinearLayout )dialog.findViewById(R.layout.maid_full_emp);
 maid_part_emp = (LinearLayout )dialog.findViewById(R.layout.maid_part_emp);