当我单击登录类上的注册按钮时,我收到了一个空指针异常,它给出了该错误。我试图修改局部和全局变量,但似乎没有任何东西可以修复错误,我可能在 onClickListener 上搞混了,因为当我删除该代码时它似乎可以工作。
注册.class。
public class Signup extends Activity {
/*List Variables */
private ScrollView scrollView1;
private EditText editTextUN;
private EditText editTextPW1;
private EditText editTextPW2;
private EditText editTextEMAIL;
private Button buttonSignup;
private Button buttonCancelSignup;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
/*set ContentView */
setContentView(R.layout.signup);
/*Populate fields */
populate();
}
private void populate() {
// TODO Auto-generated method stub
/*initialize the ScrollView */
ScrollView scrollView1 = (ScrollView) findViewById(R.id.scrollView1);
/*initialize the EditText for the FIRST username field */
final EditText editTextUN = (EditText) findViewById(R.id.editTextUN);
/*initialize the FIRST password field */
final EditText editTextPW1 = (EditText) findViewById(R.id.editTextPW1);
/*initialize the SECOND password field */
final EditText editTextPW2 = (EditText) findViewById(R.id.editTextPW2);
/*initialize the signup Button */
Button buttonSignup = (Button) findViewById(R.id.btsignup);
/*initialize the cancel Button */
Button buttonCancelSignup = (Button) findViewById(R.id.buttonCancelSignup);
/*initialize the email EditText */
final EditText editTextEMAIL = (EditText) findViewById(R.id.editTextEMAIL);
/*setup OnClickListener */
buttonSignup.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
/*check to see if it's the signup button */
if(v.getId()== R.id.buttonSignup) {
/*get everything to a string */
String user = editTextUN.getText().toString();
String pass1 = editTextPW1.getText().toString();
String pass2 = editTextPW2.getText().toString();
String email = editTextEMAIL.getText().toString();
/*check to see if there's Text in all the Edit Text's */
if(user !=null && user.length()>5 && pass1.length()>5 && pass1 !=null && pass2.length()>5 && pass2 !=null && email.length()>5 && email !=null) {
/*send to comHelper */
ComHelper.sendSignUp(user, pass1, pass2, email);
while(ComHelper.sendSignUp(null, null, null, null)) {
}
}
} else {
finish();
}
}
});
}
}
日志猫:
06-06 17:15:30.045: E/AndroidRuntime(1197): FATAL EXCEPTION: main
06-06 17:15:30.045: E/AndroidRuntime(1197): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gta5news.qwuik/com.gta5news.qwuik.Signup}: java.lang.NullPointerException
06-06 17:15:30.045: E/AndroidRuntime(1197): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
06-06 17:15:30.045: E/AndroidRuntime(1197): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
06-06 17:15:30.045: E/AndroidRuntime(1197): at android.app.ActivityThread.access$600(ActivityThread.java:123)
06-06 17:15:30.045: E/AndroidRuntime(1197): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
06-06 17:15:30.045: E/AndroidRuntime(1197): at android.os.Handler.dispatchMessage(Handler.java:99)
06-06 17:15:30.045: E/AndroidRuntime(1197): at android.os.Looper.loop(Looper.java:137)
06-06 17:15:30.045: E/AndroidRuntime(1197): at android.app.ActivityThread.main(ActivityThread.java:4424)
06-06 17:15:30.045: E/AndroidRuntime(1197): at java.lang.reflect.Method.invokeNative(Native Method)
06-06 17:15:30.045: E/AndroidRuntime(1197): at java.lang.reflect.Method.invoke(Method.java:511)
06-06 17:15:30.045: E/AndroidRuntime(1197): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-06 17:15:30.045: E/AndroidRuntime(1197): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-06 17:15:30.045: E/AndroidRuntime(1197): at dalvik.system.NativeStart.main(Native Method)
06-06 17:15:30.045: E/AndroidRuntime(1197): Caused by: java.lang.NullPointerException
06-06 17:15:30.045: E/AndroidRuntime(1197): at com.gta5news.qwuik.Signup.populate(Signup.java:70)
06-06 17:15:30.045: E/AndroidRuntime(1197): at com.gta5news.qwuik.Signup.onCreate(Signup.java:40)
06-06 17:15:30.045: E/AndroidRuntime(1197): at android.app.Activity.performCreate(Activity.java:4465)
06-06 17:15:30.045: E/AndroidRuntime(1197): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
06-06 17:15:30.045: E/AndroidRuntime(1197): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
编辑:这是在 x86 模拟器上。