守则是:
private void loginAndSignupDialog() {
final Dialog dialog = new Dialog(SelectServiceActivity.this);
dialog.setContentView(R.layout.dialog_login);
dialog.setTitle("Login");
dialog.findViewById(R.id.login_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final String username = ((EditText) dialog.findViewById(R.id.username)).getText().toString();
final String password = ((EditText) dialog.findViewById(R.id.password)).getText().toString();
validateUser(username, password, dialog);
}
});
dialog.findViewById(R.id.to_signup_page).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//final Dialog dialog = new Dialog(SelectServiceActivity.this);
dialog.setContentView(R.layout.dialog_signup);
dialog.setTitle("Signup");
dialog.findViewById(R.id.signup).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final String email = ((EditText) dialog.findViewById(R.id.email)).getText().toString();
final String password = ((EditText) dialog.findViewById(R.id.password)).getText().toString();
final String confirmPassword = ((EditText) dialog.findViewById(R.id.confirm_password)).getText().toString();
final String mobileNo = ((EditText) dialog.findViewById(R.id.mobile_number)).getText().toString();
if (password.equals(confirmPassword)) {
registerUser(email, password, mobileNo);
} else {
Toast.makeText(SelectServiceActivity.this, "Two passwords are not same.", Toast.LENGTH_SHORT).show();
}
}
});
}
});
dialog.show();
}
我在这些行上得到“空指针异常”:
final String email = ((EditText) dialog.findViewById(R.id.email)).getText().toString();
final String password = ((EditText) dialog.findViewById(R.id.password)).getText().toString();
final String confirmPassword = ((EditText) dialog.findViewById(R.id.confirm_password)).getText().toString();
final String mobileNo = ((EditText) dialog.findViewById(R.id.mobile_number)).getText().toString();
方案是我通过点击下面显示的登录页面上的 SIGNUP 文本从登录对话框移动到注册对话框页面:
我在登录页面上获取数据时没有错误,但在注册页面属性上获得空指针异常。
注册对话框XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_margin="20sp"
android:gravity="center"
android:orientation="vertical"
android:overScrollMode="ifContentScrolls"
android:scrollbars="none"
tools:context="in.co.servicedeal.app.SignupActivity">
<android.support.v7.widget.AppCompatImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/sd_logo" />
<android.support.v7.widget.AppCompatEditText
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/email"
android:inputType="textEmailAddress" />
<android.support.v7.widget.AppCompatEditText
android:id="@+id/signup_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/password"
android:inputType="textPassword" />
<android.support.v7.widget.AppCompatEditText
android:id="@+id/confirm_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/confirmPassword"
android:inputType="textPassword" />
<android.support.v7.widget.AppCompatEditText
android:id="@+id/mobile_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/mobileNumber"
android:inputType="phone" />
<android.support.v7.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.AppCompatCheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
app:buttonTint="@color/colorPrimary" />
<android.support.v7.widget.AppCompatTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/serviceAggrement" />
</android.support.v7.widget.LinearLayoutCompat>
<android.support.v7.widget.AppCompatButton
android:id="@+id/signup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10sp"
android:background="@color/colorPrimary"
android:elevation="@dimen/card_elevation"
android:text="@string/signUp"
android:textColor="@android:color/white"
android:textSize="20sp" />
</android.support.v7.widget.LinearLayoutCompat>
登录对话框XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.LinearLayoutCompat 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:layout_gravity="center"
android:layout_margin="20sp"
android:gravity="center"
android:orientation="vertical"
tools:context="in.co.servicedeal.app.LoginActivity">
<android.support.v7.widget.AppCompatImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/sd_logo" />
<android.support.v7.widget.AppCompatEditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/userName" />
<android.support.v7.widget.AppCompatEditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/password"
android:inputType="textPassword" />
<android.support.v7.widget.AppCompatButton
android:id="@+id/login_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25sp"
android:background="@color/colorPrimary"
android:text="@string/login"
android:textColor="@android:color/white"
android:textSize="20sp" />
<android.support.v7.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20sp"
android:layout_marginTop="10sp"
android:gravity="center">
<android.support.v7.widget.AppCompatTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/dontHaveAcct" />
<android.support.v7.widget.AppCompatTextView
android:id="@+id/to_signup_page"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5sp"
android:text="@string/signUp"
android:textStyle="bold" />
</android.support.v7.widget.LinearLayoutCompat>
</android.support.v7.widget.LinearLayoutCompat>
请建议!
答案 0 :(得分:1)
当应用程序尝试使用时抛出NullPointerException 具有空值的对象引用。
new Dialog from Parent
。getText().toString()
null或NOT 。findViewById(R.id.
你应该试试
final Dialog dialog_SignUP = new Dialog(SelectServiceActivity.this);
dialog_SignUP.setContentView(R.layout.dialog_signup);
dialog_SignUP.setTitle("Signup");
dialog_SignUP.findViewById(R.id.signup).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final String email = ((EditText) dialog_SignUP.findViewById(R.id.email)).getText().toString();
final String password = ((EditText) dialog_SignUP.findViewById(R.id.password)).getText().toString();
final String confirmPassword = ((EditText) dialog_SignUP.findViewById(R.id.confirm_password)).getText().toString();
final String mobileNo = ((EditText) dialog_SignUP.findViewById(R.id.mobile_number)).getText().toString();
if (password.equals(confirmPassword)) {
registerUser(email, password, mobileNo);
} else {
Toast.makeText(SelectServiceActivity.this, "Two passwords are not same.", Toast.LENGTH_SHORT).show();
}
}
});
}
});
dialog_SignUP.show();
答案 1 :(得分:0)
您的电子邮件,密码,confirmPassword,mobilno字段为空。
试试这个:
final String email = ((EditText)v dialog.findViewById(R.id.email)).getText().toString();
final String password = ((EditText)v dialog.findViewById(R.id.password)).getText().toString();
final String confirmPassword = ((EditText)v dialog.findViewById(R.id.confirm_password)).getText().toString();
final String mobileNo = ((EditText)v dialog.findViewById(R.id.mobile_number)).getText().toString();
答案 2 :(得分:0)
尝试创建您的界面DialogOnClickListener
,然后填写Bundle