Navigating to the another actvity app getting crash in android

110 Views Asked by At

I'm new to Android and making the Login Page for the SudentApplication.

Have 3 activities in application,FrontPage LoginActivity and PageLogActivity. In FrongPage just gave a login symbol when user clicks on that symbol it will navigate to next activity call "LoginActvity", Here User giving UserName and Password and that connecting to the Php server there if both the username and password matches the response will come as "true".and also given remember to the user. That Code is Like this: LoginActivity

public class LoginActivity extends AppCompatActivity implements TextWatcher, 
CompoundButton.OnCheckedChangeListener {

final String TAG = LoginActivity.class.getSimpleName();

private CheckBox rem_userpass;
SharedPreferences sharedPreferences;
SharedPreferences.Editor editor;

private EditText etUserNmae,etPassword;
private Button btLogin;

String userName;
String password;

private static final String PREF_NAME = "prefs";
private static  final String KEY_REMEMBER = "remember";
private static final  String KEY_USERNAME = "username";
private  static final  String KEY_PASS = "password";

String url = "https://domainname.com/folderName/login";

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

    sharedPreferences = getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
    editor = sharedPreferences.edit();

    etUserNmae = (EditText) findViewById(R.id.etusername);
    etPassword = (EditText) findViewById(R.id.etpassword);
    rem_userpass = (CheckBox) findViewById(R.id.checkBox);
    btLogin = (Button) findViewById(R.id.btlogin);

    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    if(sharedPreferences.getBoolean(KEY_REMEMBER,false)){
        rem_userpass.setChecked(true);
    }
    else {
        rem_userpass.setChecked(false);
    }


    etUserNmae.setText(sharedPreferences.getString(KEY_USERNAME,""));
    etPassword.setText(sharedPreferences.getString(KEY_PASS,""));

    etUserNmae.addTextChangedListener(LoginActivity.this);
    etPassword.addTextChangedListener(LoginActivity.this);
    rem_userpass.setOnCheckedChangeListener(LoginActivity.this);

    btLogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            int idi = view.getId();
            switch (idi) {
                case R.id.btlogin:
                    doing();
                    break;

            }
        }

        private void doing() {
             userName = etUserNmae.getText().toString();
             password = etPassword.getText().toString();

            if (TextUtils.isEmpty(userName)) {
                etUserNmae.setError("Please enter UserName");
                etPassword.setError("Please enter password");
                return;
            } else if (TextUtils.isEmpty(password)) {
                etPassword.setError("Please enter password");
                return;
            }else {
                StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Log.d(TAG, response);
                        if (response.contains("true")) {
                            Intent intent = new Intent(LoginActivity.this, PageLogActivity.class);
                            startActivity(intent);
                        } else {
                            Toast.makeText(getApplicationContext(), "Wrong Username and Password", Toast.LENGTH_SHORT).show();
                        }

                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                        Toast.makeText(getApplicationContext(), "Error while reading data", Toast.LENGTH_SHORT).show();

                    }
                }) {
                    @Override
                    protected Map<String, String> getParams() throws AuthFailureError {
                        Map<String, String> params = new HashMap<>();
                        params.put("username", etUserNmae.getText().toString());
                        params.put("password", etPassword.getText().toString());
                        return params;
                    }
                };
           MySingleton.getmInstance(getApplicationContext()).addToRequestQueue(stringRequest);
            }
        }
    });

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if(id == R.id.home){
        NavUtils.navigateUpFromSameTask(this);
    }
    return super.onOptionsItemSelected(item);
}

@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}

@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    managePrefs();

}

@Override
public void afterTextChanged(Editable editable) {
}

@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
    managePrefs();
}

 private void managePrefs(){
     if(rem_userpass.isChecked()) {
         editor.putString(KEY_USERNAME, etUserNmae.getText().toString().trim());
         editor.putString(KEY_PASS, etPassword.getText().toString().trim());
         editor.putBoolean(KEY_REMEMBER, true);
         editor.apply();
     }else {
        editor.putBoolean(KEY_REMEMBER, false);
         editor.remove(KEY_USERNAME);
         editor.remove(KEY_PASS);
         editor.apply();
     }
     }
 }

Geeting the response but when click Login button ut's getting the msg as a Unfrochunatly app as closed. It's not going to the NextACtivity.

And MYSingleTon class is like this:

public class MySingleton {

private static MySingleton mInstance;
private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;
private static Context mCtx;


private MySingleton(Context context){
    mCtx = context;
    mRequestQueue = getRequestQueue();
    //for loadig Image....

    mImageLoader  = new ImageLoader(mRequestQueue, new ImageLoader.ImageCache() {

        private final LruCache<String,Bitmap>
           cache = new LruCache<String, Bitmap>(20);

        @Override
        public Bitmap getBitmap(String url) {
            return cache.get(url);
        }

        @Override
        public void putBitmap(String url, Bitmap bitmap) {

          cache.put(url,bitmap);
        }
    });
}

public  static synchronized MySingleton getmInstance(Context context){
    if(mInstance == null){

        mInstance = new MySingleton(context);

    }
    return mInstance;
}

public RequestQueue getRequestQueue(){

    if (mRequestQueue == null){

        mRequestQueue = Volley.newRequestQueue(mCtx.getApplicationContext());
    }
    return mRequestQueue;
}

public <T> void addToRequestQueue(Request<T> req){
    getRequestQueue().add(req);
}

public ImageLoader getmImageLoader(){
    return mImageLoader;
}}
0

There are 0 best solutions below