Java 如何设置带条件的导航视图?
在android中,我有两个左右两侧的导航视图。如下图所示,左侧为注册为家长的用户,右侧为注册为学费提供者的用户。例如,如果用户以家长身份注册,则他/她只能打开左侧导航。我应该编写什么代码来确保他/她是每个导航视图的用户。有人能帮我或教我吗 我的界面示例 我正在尝试这种编码:-Java 如何设置带条件的导航视图?,java,android,android-imagebutton,android-navigationview,Java,Android,Android Imagebutton,Android Navigationview,在android中,我有两个左右两侧的导航视图。如下图所示,左侧为注册为家长的用户,右侧为注册为学费提供者的用户。例如,如果用户以家长身份注册,则他/她只能打开左侧导航。我应该编写什么代码来确保他/她是每个导航视图的用户。有人能帮我或教我吗 我的界面示例 我正在尝试这种编码:- userCategory = (RadioGroup) findViewById(R.id.user_radio_group_btn); Integer checkedID = userCategory.getC
userCategory = (RadioGroup) findViewById(R.id.user_radio_group_btn);
Integer checkedID = userCategory.getCheckedRadioButtonId();
radioButton = (RadioButton) findViewById(checkedID);
String selection = radioButton.getText().toString();
if (selection.equals("Parents"))
{
menuRight.setVisibility(View.GONE);
tuitionProvider.setVisibility(View.GONE);
}
if (selection.equals("Tuition Provider"))
{
menuLeft.setVisibility(View.GONE);
parents.setVisibility(View.GONE);
}
else
{
Intent RegistrationIntent = new Intent(home.this, RegistrationActivity.class);
startActivity(RegistrationIntent);
Toast.makeText(this, "Please register first.", Toast.LENGTH_SHORT).show();
}
XML编码:-
<RadioGroup
android:id="@+id/user_radio_group_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:orientation="horizontal">
<RadioButton
android:id="@+id/parents_radio_button"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginStart="50dp"
android:layout_marginTop="175dp"
android:background="@drawable/roundedbutton"
android:text="Parents" />
<RadioButton
android:id="@+id/tuition_provider_radio_button"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginStart="35dp"
android:layout_marginTop="175dp"
android:background="@drawable/roundedbutton"
android:text="Tuition Provider" />
</RadioGroup>
我犯了一个错误
原因:java.lang.NullPointerException:尝试对空对象引用调用虚拟方法“boolean java.lang.Object.equalsjava.lang.Object”
在com.addaj.mobileRecommensionSystem.home.onCreatehome.java:59中,检查谁已登录,只需让一个导航抽屉对用户不可见,第二个抽屉可见即可。 例如,您检查userCategory,如果它是家长,则使教师的导航抽屉不可见
final RadioGroup userCategory = (RadioGroup) findViewById(R.id.user_radio_group_btn);
userCategory.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
Integer checkedID = userCategory.getCheckedRadioButtonId();
View radioButton = userCategory.findViewById(checkedID);
int radioId = userCategory.indexOfChild(radioButton);
RadioButton btn = (RadioButton) userCategory.getChildAt(radioId);
String selection = (String) btn.getText();
if(selection.equals("Parents")){
menuRight.setVisibility(View.GONE);
tuitionProvider.setVisibility(View.GONE);
}else{
menuLeft.setVisibility(View.GONE);
parents.setVisibility(View.GONE);
}
}
}
});
我已经测试了您的代码,您需要使用android:checked=true在XML中设置一个单选按钮 您需要设置至少一个单选按钮,如 android:checked=true 比如:
<RadioGroup
android:id="@+id/user_radio_group_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/parents_radio_button"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:checked="true"
android:text="Parents" />
<RadioButton
android:id="@+id/tuition_provider_radio_button"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="Tuition Provider" />
</RadioGroup>
解决方案:
而不是:
Integer checkedID = userCategory.getCheckedRadioButtonId();
在这里,用户分类是一个单选按钮。使用以下命令:
Integer checkedID = (enter here your RadioGroupId).getCheckedRadioButtonId();
试试看它是否有效。在home.java文件的错误日志中清楚地提到了第59行的内容,其中有些内容为空;也发布您的XML详细信息。已经在我的问题中编辑了:我已经测试了您的代码,您需要使用android:checked=true设置XML中的一个单选按钮。java编码怎么样,先生?您的java代码非常完美。所以你不需要改变它。这是最新的编码吗,先生?因为刚才我已经更改了:“:”先生,如果在我的home.xml中没有任何按钮。但是我可以在我的home.java中编写按钮编码吗?RadioGroup userCategory=RadioGroup findviewbydr.id.user\u radio\u group\u btn;整数checkedID=userCategory.getCheckedRadioButtonId;RadioButton RadioButton=RadioButton FindViewBydCheckedDid;字符串选择=radioButton.getText.toString;if selection.equalsParents{Toast.makeTextthis,Parents,Toast.LENGTH_SHORT.show;}if selection.EqualsTurship{Toast.makeTextthis,Tuast.LENGTH_SHORT.show;}