LinearLayout 在按钮的 onClick 中为 null
LinearLayout is null inside button's onClick
我试图在单击按钮时隐藏一个线性布局,并显示同一布局文件中的另一个线性布局。但是按钮的onclick里面的线性布局是空的。
displayLayout
和 editLayout
为空。
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>
只需在您的代码中修复您的视图 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);
}
});
}
错误代码displayLayout = (LinearLayout) findViewById(R.id.linearLayout);
正确的代码displayLayout = (LinearLayout) findViewById(R.id.displayLayout);
你设置了一个不存在的id。
我试图在单击按钮时隐藏一个线性布局,并显示同一布局文件中的另一个线性布局。但是按钮的onclick里面的线性布局是空的。
displayLayout
和 editLayout
为空。
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>
只需在您的代码中修复您的视图 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);
}
});
}
错误代码displayLayout = (LinearLayout) findViewById(R.id.linearLayout);
正确的代码displayLayout = (LinearLayout) findViewById(R.id.displayLayout);
你设置了一个不存在的id。