当我点击Login类上的注册按钮时,我得到了一个nullpointerexception,它给出了这个错误。我试图修改本地和全局变量,似乎没有什么可以修复错误,我可能已经混淆了onClickListener,因为当我删除它似乎工作的代码。
Signup.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();
}
}
});
}
}
logcat的:
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仿真器上。
答案 0 :(得分:2)
所有上述陈述都是正确的,但我认为NullPointerException是由于:
作为in Button buttonSignup = (Button) findViewById(R.id.btsignup);
右侧ID为R.id.buttonSignup
所以看起来buttonSignup是空的
正如你在所使用的那样........................................
/ *检查是否是注册按钮* /
if(v.getId()== `R.id.buttonSignup`) {
...........................................
答案 1 :(得分:1)
我已更新您的代码并添加了评论,现在它与我合作
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ScrollView;
import android.widget.Toast;
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);
setContentView(R.layout.signup);
/*Populate fields */
populate();
}
private void populate() {
/*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 */
/* here.........i have added final .........*/
final 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.btsignup) {
/* here.........i have added if condition .........*/
if(v==buttonSignup){
Toast.makeText(getApplicationContext(), "button has been pressed", Toast.LENGTH_SHORT).show();
/*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) {
}
}
else
{
finish();
}
}
});
}
}
希望它有所帮助。