找不到空指针异常的原因

时间:2012-06-18 15:00:37

标签: android exception pointers null

有谁可以告诉我为什么我在这个类中得到一个空指针异常?

public class Login extends Activity {
    /** Called when the activity is first created. */       


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

        // Get buttons from xml file
        final Button existingUser = (Button)findViewById(R.id.but01);
        final Button play = (Button)findViewById(R.id.but02);
        final ImageView image1 = (ImageView)findViewById(R.id.iv01);
        final ImageView image2 = (ImageView)findViewById(R.id.iv02);
        final EditText email = (EditText)findViewById(R.id.et01);
        final EditText screenName = (EditText)findViewById(R.id.et02);

        // Add listener for play button and send stuff to database
        play.setOnClickListener(new View.OnClickListener() {    
            public void onClick(View v) {
                // TO DO Send user's entered data to database/server
            }
        });

/*
I have a feeling this may be causing the problem. 
I validate the text from the email and screen name text boxes as the user enters text
and display a tick or cross to the side of the box, and then validate the whole form 
on pressing a button:
*/
        // Add listener for existing user button and move to registered user screen. Pass entered email
        existingUser.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                String enteredEmail = email.getText().toString();
                String enteredScreenName = screenName.getText().toString();

                if (validateEmail(enteredEmail) == true && (validateScreenName(enteredScreenName) == true)) {
                    Intent myIntent = new Intent(getApplicationContext(), RegisteredUser.class);
                    myIntent.putExtra("enteredEmail", enteredEmail);
                    startActivity(myIntent);
                }

                else if (validateEmail(enteredEmail) == false && (validateScreenName(enteredScreenName) == true)) {
                    Toast myToast = Toast.makeText(getApplicationContext(), "That email address is not valid. Please enter a valid email address", Toast.LENGTH_SHORT);
                    myToast.show();
                }

                else if (validateEmail(enteredEmail) == true && (validateScreenName(enteredScreenName) == false)) {
                    Toast myToast = Toast.makeText(getApplicationContext(), "You need to enter a valid screen name to play. Please use only a - Z, 0 - 9, - and _.", Toast.LENGTH_SHORT);
                    myToast.show();
                }

                else {
                    Toast myToast = Toast.makeText(getApplicationContext(), "Invalid email address or screen name.", Toast.LENGTH_SHORT);
                    myToast.show();
                }
            }
        });

        // Add key listener to email edittext and validate as user enters data
        email.addTextChangedListener(new TextWatcher() {

            @Override
            public void afterTextChanged(Editable s) {
                String enteredEmail = email.getText().toString();

                if (validateEmail(enteredEmail) == true) {
                    image1.setImageResource(R.drawable.greentick);
                }

                else {
                    image1.setImageResource(R.drawable.redcross);
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {
                // TODO Auto-generated method stub

            }
        });

        // Add key listener to screen name field to validate as user types
        screenName.addTextChangedListener(new TextWatcher() {

            @Override
            public void afterTextChanged(Editable arg0) {
                String enteredScreenName = screenName.getText().toString();

                if (validateScreenName(enteredScreenName) == true) {
                    image2.setImageResource(R.drawable.greentick);      
                }

                else {
                    image2.setImageResource(R.drawable.redcross);
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {
                // TODO Auto-generated method stub

            }

        });

    }

验证方法:

    public boolean validateEmail(String email) {
        // Check email against email patter matcher and for not empty field
        boolean validated = false;
        if (android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches() && (! email.equals(""))) {
            validated = true;   
        }

        else {
            validated = false;
        }   
        return validated;
    }

    public boolean validateScreenName(String screenName) {
        // Check screen name has > 0 chars and that it contains only a-z, A-Z, _ and -
        boolean validated = false;
        if (screenName.matches("[a-zA-Z0-9_-]{1,20}") && (! screenName.equals("")) && (! screenName.contains("\\s"))) {
            validated = true;
        }

        else {
            validated = false;
        }
        return validated;
    }
}

0 个答案:

没有答案