Android Studio一直给我nullpointerexception

时间:2017-03-05 09:49:12

标签: java android

您好我正在做一个项目,应该在周三到期,我花了超过13个小时试图让Android工作室再次工作,首先他们是gradle的一个问题,现在我一直得到一个nullpointerexception,即使它是相同的代码是以前工作过。这是代码,请帮帮忙?因为我卡住了,可能正在做我的项目,而不是试图解决安卓工作室的问题。顺便说一句,代码中没有错误,但这里是代码。

            package ie.wit.fitnessmadeeasy;


            import android.content.Intent;
            import android.support.v7.app.AppCompatActivity;
            import android.os.Bundle;
            import android.util.Log;
            import android.view.View;
            import android.widget.EditText;
            import android.widget.TextView;
            import android.widget.Toast;

            public class LogActivity extends AppCompatActivity {

            DataBaseHelper helper = new DataBaseHelper(this);

            EditText uname = (EditText) findViewById(R.id.et_username);
            String unstr = uname.getText().toString();
           EditText pass = (EditText) findViewById(R.id.et_password);
           String passstr = pass.getText().toString();

              @Override
           protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
             setContentView(R.layout.activity_log);


            TextView registerLink = (TextView) findViewById(R.id.regHere);          //register link creates a link between the two pages

          registerLink.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
            Intent registerIntent = new Intent(LogActivity.this,   RegisterActivity.class);
            LogActivity.this.startActivity(registerIntent);  //what this does it creates an intent that opens the registeravtivity then it tells the current activity to perform the intent and open the register page
        }
    });
}


// Button login = (Button) findViewById(R.id.login);




public void onLogClick(View view) {
    if (view.getId() == R.id.login) {


        String Password = helper.searchPassstr(unstr);
        Log.v("pass", passstr);
        Log.v("pass", Password);



        if (passstr.equals(Password)) {

            Intent i = new Intent(LogActivity.this, UserAreaActivity.class);
            i.putExtra("username", unstr);

            startActivity(i);
        } else {
            Toast temp = Toast.makeText(LogActivity.this, "Username and Password don't Match", Toast.LENGTH_SHORT);
            temp.show();
        }
    }

}

}

   package ie.wit.fitnessmadeeasy;


   import android.content.Intent;
   import android.support.v7.app.AppCompatActivity;
   import android.os.Bundle;
   import android.view.View;
   //import android.widget.Button;
   import android.widget.EditText;
   import android.widget.Toast;



    public class RegisterActivity extends AppCompatActivity {

    DataBaseHelper helper = new DataBaseHelper(this);


    final EditText name = (EditText) findViewById(R.id.et_name);
    final EditText username = (EditText) findViewById(R.id.et_username);
    final EditText password1 = (EditText) findViewById(R.id.et_password);
   final EditText password2 = (EditText) findViewById(R.id.et_password);

   String namestr = name.getText().toString();
   String usernamestr = username.getText().toString();
   String password1str = password1.getText().toString();
   String password2str = password2.getText().toString();

   @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_register);



   // final EditText password2 = (EditText) findViewById(R.id.et_password);


    name.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {

            if(name.getText().length()==0){

                name.setError("Fill Out");
            }
           else if(username.getText().length()==0){

                username.setError("Fill Out");
            }


        }

    });


}


public void onRegClick(View v)
{

    if(v.getId() == R.id.confirm)
    {
       //  EditText age = (EditText) findViewById(R.id.et_age); //age is a final variable and is only assigned to activity_register as a view


       // EditText name = (EditText) findViewById(R.id.et_name);
       // EditText username = (EditText) findViewById(R.id.et_username);
       // EditText password1 = (EditText) findViewById(R.id.et_password);
       // EditText password2 = (EditText) findViewById(R.id.et_password2);




        if(namestr.equals("")){
            Toast pass = Toast.makeText(RegisterActivity.this, "Enter Name!", Toast.LENGTH_SHORT);
            pass.show();
        }
        if(usernamestr.equals("")){
            Toast pass = Toast.makeText(RegisterActivity.this, "Enter Username!", Toast.LENGTH_SHORT);
            pass.show();
        }
        if(password1str.equals("")){
            Toast pass = Toast.makeText(RegisterActivity.this, "Enter Password!", Toast.LENGTH_SHORT);
            pass.show();
        }
        if(!password1str.equals(password2str))
        {
            //popup message
            Toast pass = Toast.makeText(RegisterActivity.this, "Passwords don't match!", Toast.LENGTH_SHORT);
            pass.show();
        }
         else{

            // String namestr = name.getText().toString();
            //insert details
            RegRequest reg = new RegRequest();
            reg.setEt_name(namestr);
            reg.setEt_username(usernamestr);
            reg.setEt_password(password1str);
           // r.setEt_age(agestr);

            Toast success = Toast.makeText(RegisterActivity.this, "Success", Toast.LENGTH_SHORT);
            success.show();


            Intent i = new Intent(RegisterActivity.this, LogActivity.class);

            startActivity(i);

            helper.insertUser(reg);
        }




    }
}

}

            package ie.wit.fitnessmadeeasy;
           public class RegRequest{
           String et_name , et_username, et_password;

// int et_age;

          public void setEt_name(String et_name)
           {
          this.et_name = et_name;
           }

          public String getEt_name()
          {
         return this.et_name;
           }

public void setEt_username(String et_username)
{
    this.et_username = et_username;
}

public String getEt_username()
{
    return this.et_username;
}

public void setEt_password(String et_password)
{
    this.et_password = et_password;
}

public String getEt_password()
{
    return this.et_password;
}

}

       at ie.wit.fitnessmadeeasy.LogActivity.<init>(LogActivity.java:17)

1 个答案:

答案 0 :(得分:0)

LogActivity.java

仅声明如下字段:

EditText uname, pass;
String unstr, passstr;

onCreate()方法中初始化。

@Override
protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_log);

     uname = (EditText) findViewById(R.id.et_username);
     unstr = uname.getText().toString();
     pass = (EditText) findViewById(R.id.et_password);
     passstr = pass.getText().toString();

}

和RegisterActivity.java

DataBaseHelper helper ;

EditText name, username,password1,password2;
String namestr,usernamestr,password1str,password2str;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_register);

    helper = new DataBaseHelper(this);
    name = (EditText) findViewById(R.id.et_name);
    username = (EditText) findViewById(R.id.et_username);
    password1 = (EditText) findViewById(R.id.et_password);
    password2 = (EditText) findViewById(R.id.et_password);

   namestr = name.getText().toString();
   usernamestr = username.getText().toString();
   password1str = password1.getText().toString();
   password2str = password2.getText().toString();
}