想要从这些edittext字段中收集值,验证它们并通过DB处理它们。代码尚未完成,代码片段在这里,已经有很多" NullPointerException:尝试调用虚拟方法' android.widget.EdiTtext.getText()'在null对象引用"进入Logcat。
相同的Java代码
signupBtn = (Button) findViewById(R.id.signupBtn);
signupBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LayoutInflater li = LayoutInflater.from(context);
View promptsView = li.inflate(R.layout.sign_up, (ViewGroup) findViewById(R.layout.activity_main));
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
alertDialogBuilder.setView(promptsView).setCancelable(false)
.setPositiveButton("Create", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
new_userid = (EditText) findViewById(R.id.new_userid);
new_password = (EditText) findViewById(R.id.new_password);
new_confirmPassword = (EditText) findViewById(R.id.new_confirmPassword);
new_firstname = (EditText) findViewById(R.id.new_firstname);
new_lastname = (EditText) findViewById(R.id.new_lastname);
new_mobile = (EditText) findViewById(R.id.new_mobile);
new_email = (EditText) findViewById(R.id.new_email);
String userid = new_userid.getText().toString();
String password = new_password.getText().toString();
String confirmPassword = new_confirmPassword.getText().toString();
String firstname = new_firstname.getText().toString();
String lastname = new_lastname.getText().toString();
String mobile = new_mobile.getText().toString();
String email = new_email.getText().toString();
if (userid.length() < 1 && password.length() < 1 && confirmPassword.length() < 1
&& firstname.length() < 1 && lastname.length() < 1 && mobile.length() < 1
&& email.length() < 1) {
Toast.makeText(getApplicationContext(), "Please fill all the details",
Toast.LENGTH_LONG).show();
} else if (password.equals(confirmPassword) == false) {
Toast.makeText(getApplicationContext(), "Password mismatch", Toast.LENGTH_LONG)
.show();
} else {
createNewUser(userid, password, firstname, lastname, mobile, email);
}
}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
});
}
public void createNewUser(String userid, String password, String firstname, String lastname, String mobile,
String email) {
//Will Add Logic Soon
Toast.makeText(getApplicationContext(), "Account Created Successfully.", Toast.LENGTH_LONG).show();
}
对应的布局文件
<TextView
android:id="@+id/create_new_account"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginTop="8dp"
android:gravity="center"
android:text="@string/create_new_account"
android:textSize="20sp" >
</TextView>
<EditText
android:id="@+id/new_userid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginTop="8dp"
android:hint="@string/userid"
android:inputType="text" >
</EditText>
<EditText
android:id="@+id/new_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginTop="8dp"
android:hint="@string/password"
android:inputType="textPassword" >
</EditText>
<EditText
android:id="@+id/new_confirmPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginTop="8dp"
android:hint="@string/confirmPassword"
android:inputType="textPassword" >
</EditText>
<EditText
android:id="@+id/new_firstname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginTop="8dp"
android:hint="@string/firstname"
android:inputType="textCapWords" >
</EditText>
<EditText
android:id="@+id/new_lastname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginTop="8dp"
android:hint="@string/lastname"
android:inputType="textCapWords" >
</EditText>
<EditText
android:id="@+id/new_mobile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginTop="8dp"
android:hint="@string/mobile"
android:inputType="phone" >
</EditText>
<EditText
android:id="@+id/new_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginTop="8dp"
android:hint="@string/email"
android:inputType="textEmailAddress" >
</EditText>
logcat的
02-06 17:02:10.761: I/System.out(1825): *** My thread is now configured to allow connection
02-06 17:02:10.895: D/OpenGLRenderer(1825): Render dirty regions requested: true
02-06 17:02:10.907: D/(1825): HostConnection::get() New Host Connection established 0xacfa6820, tid 1825
02-06 17:02:10.934: D/Atlas(1825): Validating map...
02-06 17:02:11.061: D/libEGL(1825): loaded /system/lib/egl/libEGL_emulation.so
02-06 17:02:11.062: D/libEGL(1825): loaded /system/lib/egl/libGLESv1_CM_emulation.so
02-06 17:02:11.085: D/libEGL(1825): loaded /system/lib/egl/libGLESv2_emulation.so
02-06 17:02:11.116: D/(1825): HostConnection::get() New Host Connection established 0xa4516120, tid 1844
02-06 17:02:11.158: I/OpenGLRenderer(1825): Initialized EGL, version 1.4
02-06 17:02:11.232: D/OpenGLRenderer(1825): Enabling debug mode 0
02-06 17:02:11.287: W/EGL_emulation(1825): eglSurfaceAttrib not implemented
02-06 17:02:11.287: W/OpenGLRenderer(1825): Failed to set EGL_SWAP_BEHAVIOR on surface 0xa45192a0, error=EGL_SUCCESS
02-06 17:02:12.019: I/Choreographer(1825): Skipped 35 frames! The application may be doing too much work on its main thread.
02-06 17:02:13.941: W/EGL_emulation(1825): eglSurfaceAttrib not implemented
02-06 17:02:13.941: W/OpenGLRenderer(1825): Failed to set EGL_SWAP_BEHAVIOR on surface 0xa337e1e0, error=EGL_SUCCESS
02-06 17:02:46.379: D/AndroidRuntime(1825): Shutting down VM
02-06 17:02:46.380: E/AndroidRuntime(1825): FATAL EXCEPTION: main
02-06 17:02:46.380: E/AndroidRuntime(1825): Process: practice.myexpenses, PID: 1825
02-06 17:02:46.380: E/AndroidRuntime(1825): java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
02-06 17:02:46.380: E/AndroidRuntime(1825): at practice.myexpenses.MainActivity$3$1.onClick(MainActivity.java:126)
02-06 17:02:46.380: E/AndroidRuntime(1825): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:160)
02-06 17:02:46.380: E/AndroidRuntime(1825): at android.os.Handler.dispatchMessage(Handler.java:102)
02-06 17:02:46.380: E/AndroidRuntime(1825): at android.os.Looper.loop(Looper.java:135)
02-06 17:02:46.380: E/AndroidRuntime(1825): at android.app.ActivityThread.main(ActivityThread.java:5221)
02-06 17:02:46.380: E/AndroidRuntime(1825): at java.lang.reflect.Method.invoke(Native Method)
02-06 17:02:46.380: E/AndroidRuntime(1825): at java.lang.reflect.Method.invoke(Method.java:372)
02-06 17:02:46.380: E/AndroidRuntime(1825): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
02-06 17:02:46.380: E/AndroidRuntime(1825): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
02-06 17:02:49.820: I/Process(1825): Sending signal. PID: 1825 SIG: 9
请帮忙。