gpt4 book ai didi

java - 局部变量 NullPointerException

转载 作者:行者123 更新时间:2023-12-02 00:21:19 33 4
gpt4 key购买 nike

当我单击 Login 类上的注册按钮时,出现 nullpointerException,它给出了该错误。我尝试修改本地和全局变量,但似乎没有任何方法可以修复该错误,我可能在 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 模拟器上。

最佳答案

以上所有陈述都是正确的,但我认为 NullPointerException 的原因是:

as in Button buttonSignup = (Button) findViewById(R.id.btsignup); 右边的 id 是 R.id.buttonSignup

看起来buttonSignup在那里为空

正如您所使用的

................................................

/*检查是否是注册按钮*/

            if(v.getId()== `R.id.buttonSignup`) {

......................................................

关于java - 局部变量 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10919020/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com