I am trying to make a login page where if user enters a correct mobile and pwd it should go in ProfileActivity.java
my code
public class LoginActivity extends AppCompatActivity {
EditText editTextUsername, editTextPassword;
ProgressBar progressBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
if (SharedPrefManager.getInstance(this).isLoggedIn()) {
finish();
startActivity(new Intent(this, ProfileActivity.class));
}
progressBar = (ProgressBar) findViewById(R.id.progressBar);
editTextUsername = (EditText) findViewById(R.id.editTextUsername);
editTextPassword = (EditText) findViewById(R.id.editTextPassword);
//if user presses on login
//calling the method login
Button login = (Button)findViewById(R.id.buttonLogin);
login.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
userLogin();
}
});
//if user presses on not registered
findViewById(R.id.textViewRegister).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//open register screen
finish();
startActivity(new Intent(getApplicationContext(), MainActivity.class));
}
});
}
private void userLogin() {
//first getting the values
final String mobile = editTextUsername.getText().toString();
final String password = editTextPassword.getText().toString();
//validating inputs
if (TextUtils.isEmpty(mobile)) {
editTextUsername.setError("Please enter your username");
editTextUsername.requestFocus();
return;
}
if (TextUtils.isEmpty(password)) {
editTextPassword.setError("Please enter your password");
editTextPassword.requestFocus();
return;
}
//if everything is fine
StringRequest stringRequest = new StringRequest(Request.Method.POST, URLs.URL_LOGIN,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
progressBar.setVisibility(View.GONE);
try {
//converting response to json object
JSONObject obj = new JSONObject(response);
//if no error in response
if (!obj.getBoolean("error")) {
Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_SHORT).show();
//getting the user from the response
JSONObject userJson = obj.getJSONObject("user");
//creating a new user object
User user = new User(
userJson.getInt("id"),
userJson.getString("mobile"),
userJson.getString("password")
);
//storing the user in shared preferences
SharedPrefManager.getInstance(getApplicationContext()).userLogin(user);
//starting the profile activity
Intent intent = new Intent(LoginActivity.this, ProfileActivity.class);
startActivity(intent);
finish();
} else {
Toast.makeText(getApplicationContext(), obj.getString("message"), Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
Log.e("TAG", response);
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
}
}) {
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("mobile", mobile);
params.put("password", password);
return params;
}
};
VolleySingleton.getInstance(this).addToRequestQueue(stringRequest);
}
}
In this code neither a toast, progressbar, nor the intent to redirect to next activity is working. I have also extended ProfileActivity with Activity. I have registerd profileactivity.java in manifest file.
What am i doing wrong here? thank you for your suggestions
finish() method should be put a end, because it will ignore all next lines.
Intent intent = new Intent(LoginActivity.this, ProfileActivity.class);
startActivity(intent);
finish();
You are using "id" field as an integer and it's an String ,Secondly from where you are getting
User user = new User(
userJson.getString("id"),
"", // please add mobile and password in JSON
""
);
I solved it. In the line ,
JSONObject userJson = obj.getJSONObject("user");
I named the object as user. But in PHP I named it as result As soon as I changed it, it started working.
Although, Thanks for your support community :).
Related
I'm trying to login with four different users using php and mysql database in android, and after if any user login with email and password i'm trying to fetch their information like (Name, Address, ID, etc.) from mysql database.
And i want that whenever if any of user from those four users login they can able to see their own information from database like(Name, Address, ID, etc.)
I have already tried to separate those users with the user type and with if-else condition that is in code that i provided.
public class LoginActivity extends AppCompatActivity {
private static final String TAG="LoginActivity";
private TextView createaccount;
private EditText Ausername,Apassword;
private Button LoginButton;
private Context mContext;
//URLS class
//ALL URLs
URLs urLs;
RelativeLayout relativeLayout1,relativeLayout2;
Handler handler=new Handler();
Runnable runnable=new Runnable() {
#Override
public void run() {
relativeLayout1.setVisibility(View.VISIBLE);
relativeLayout2.setVisibility(View.VISIBLE);
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
urLs = new URLs();
if (SharedPrefManager.getInstance(getApplicationContext()).isLoggedIn()){
finish();
Intent intentD = new Intent(LoginActivity.this, DashboardActivity.class);
startActivity(intentD);
}
relativeLayout1 = findViewById(R.id.rel3);
relativeLayout2 = findViewById(R.id.rel2);
Ausername = findViewById(R.id.etuname);
Apassword = findViewById(R.id.etpass);
LoginButton = findViewById(R.id.loginbutton);
handler.postDelayed(runnable, 2000);
createaccount = findViewById(R.id.tvregistration);
createaccount.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(LoginActivity.this, Rigistration_Option.class);
startActivity(intent);
}
});
LoginButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
userLogin();
}
});
}
private void userLogin(){
//first getting the values
final String email = Ausername.getText().toString();
final String password = Apassword.getText().toString();
//validating inputs
if (TextUtils.isEmpty(email)) {
Ausername.setError("Please enter your username");
Ausername.requestFocus();
return;
}
if (TextUtils.isEmpty(password)) {
Apassword.setError("Please enter your password");
Apassword.requestFocus();
return;
}
//if everything is fine
class UserLogin extends AsyncTask<Void, Void, String> {
ProgressBar progressBar;
#Override
protected void onPreExecute() {
super.onPreExecute();
progressBar = findViewById(R.id.progressbar);
progressBar.setVisibility(View.VISIBLE);
}
#Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
progressBar.setVisibility(View.GONE);
try {
//converting response to json object
JSONObject obj = new JSONObject(s);
//if no error in response
if (!obj.getBoolean("error")) {
Toast.makeText(getApplicationContext(), obj.getString("message"), Toast.LENGTH_SHORT).show();
//getting the user from the response
JSONObject userJson = obj.getJSONObject("user");
//creating a new user object
CA_SIGNUP_GTST user = new CA_SIGNUP_GTST(
userJson.getInt("id"),
userJson.getString("name"),
userJson.getString("email"),
userJson.getString("mobile"),
userJson.getString("address")
);
//storing the user in shared preferences
SharedPrefManager.getInstance(getApplicationContext()).userLogin(user);
//starting the profile activity
finish();
Intent intentD = new Intent(LoginActivity.this, DashboardActivity.class);
startActivity(intentD);
} else {
Toast.makeText(getApplicationContext(), "Invalid username or password", Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
#Override
protected String doInBackground(Void... voids) {
//creating request handler object
RequestHandler requestHandler = new RequestHandler();
HashMap<String, String> params = new HashMap<>();
URLs urLs = new URLs();
if (urLs.equals("cma_login")) {
params.put("email", email);
params.put("password", password);
return requestHandler.sendPostRequest(URLs.CMA_LOGIN, params);
}
else if (urLs.equals("ca_login")){
params.put("email", email);
params.put("password", password);
return requestHandler.sendPostRequest(URLs.CA_LOGIN, params);
}
else if (urLs.equals("cs_login")){
params.put("email", email);
params.put("password", password);
return requestHandler.sendPostRequest(URLs.CS_LOGIN, params);
}
else if (urLs.equals("adv_login")){
params.put("email", email);
params.put("password", password);
return requestHandler.sendPostRequest(URLs.ADV_LOGIN, params);
}
return null;
}
}
UserLogin ul = new UserLogin();
ul.execute();
}
}
i expect that after user login they can see their own information but when i try to login with second and the third user then they are unable to login.
I want the data of textview in the second activity to pass in the third but i dont want to display it in the third activity. I want to directly send the data from the second activity to Pass in my sql db after clicking the button in the final activity. Is is achievable?
This is my first activity.
public class MainActivity extends AppCompatActivity {
private EditText email, password;
private Button btn_login;
private ProgressBar loading;
private static String URL_LOGIN ="http://192.168.1.83/Attendance/login.php";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loading= findViewById(R.id.loading);
email = findViewById(R.id.editTextUserEmail);
password = findViewById(R.id.editTextPassword);
btn_login = findViewById(R.id.buttonRegister);
btn_login.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String mEmail = email.getText().toString().trim();
String mPassword = password.getText().toString().trim();
if (!mEmail.isEmpty() || !mPassword.isEmpty()) {
Login(mEmail, mPassword);
}else{
email.setError("Please Enter Email");
password.setError("Please Enter Password");
}
}
});
}
private void Login(final String email, final String password) {
loading.setVisibility(View.VISIBLE);
btn_login.setVisibility(View.GONE);
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_LOGIN,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
Log.d("JSON", jsonObject.toString());
String success = jsonObject.getString("success");
JSONArray jsonArray =
jsonObject.getJSONArray("login");
if (success.equals("1"))
{
for (int i = 0; i<jsonArray.length(); i++){
JSONObject object =
jsonArray.getJSONObject(i);
String name =
object.getString("name").trim();
String email =
object.getString("email").trim();
// Toast.makeText(MainActivity.this,
"Success Login \nYour name: "+name+"\nYour Email: "+email,
// Toast.LENGTH_LONG).show();
Intent intent = new
Intent(MainActivity.this, HomeActivity.class);
intent.putExtra("name",name);
intent.putExtra("email", email);
startActivity(intent);
loading.setVisibility(View.GONE);
}
}
} catch (JSONException e) {
e.printStackTrace();
loading.setVisibility(View.GONE);
btn_login.setVisibility(View.VISIBLE);
Toast.makeText(MainActivity.this, "Error"
+e.toString(),
Toast.LENGTH_LONG).show();
}
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, "Error"
+error.toString(),
Toast.LENGTH_LONG).show();
}
})
{
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("email", email);
params.put("password", password);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
This is my second activity
public class HomeActivity extends AppCompatActivity {
private TextView name,email;
private TextView welcome;
private Button Send;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
name = findViewById(R.id.name);
email = findViewById(R.id.email);
Send = findViewById(R.id.btn_send);
welcome = findViewById(R.id.welcome);
final Intent intent = getIntent();
String extraName = intent.getStringExtra("name");
String extraEmail = intent.getStringExtra("email");
name.setText(extraName);
email.setText(extraEmail);
Send.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent1 = new Intent(HomeActivity.this,StatusActivity.class);
intent1.putExtra("wel", welcome.getText().toString());
intent1.putExtra("name1", name.getText().toString());
intent1.putExtra("email1", email.getText().toString());
startActivity(intent1);
}
});
}
And this is my final activity but Here i dont want to show the text view i just want to post it from here.
public class StatusActivity extends AppCompatActivity {
private TextView welcome, name, email;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_status);
welcome = findViewById(R.id.abcd);
name = findViewById(R.id.name1);
email = findViewById(R.id.email1);
final Intent intent = getIntent();
String extraName1 = intent.getStringExtra("name1");
String extraEmail1 = intent.getStringExtra("email1");
String extraWelcome = intent.getStringExtra("wel");
name.setText(extraName1);
email.setText(extraEmail1);
welcome.setText(extraWelcome);
}
Your question is pretty clear until you put MySql to the topic. Why you want to pass the data from one activity to another through the database. Why involve database operations which take time and resources.
You may pass data from one activity to another through Intents (primitive values, Strings and Parcelables)!
All you have to do is:
Get the data (int your case from TextView).
Create the Intent to navigate to the next Activity.
Put the data to the Intent.
Start the Activity.
Retrieve the data from the Intent.
For example if will navigate to the next Activity via button click then in that button's onClick():
// 1.
String data = myTextView.getText();
// 2.
Intent intent = new Intent(yourContext,YourNextActivity.class);
// 3.
intent.putStringExtra("MY DATA KEY",data);
// 4.
yourContext.startActivity(intent);
// 5. Now in YourNextActivity's onCreate();
String retrievedData = YourNextActivity.this.getIntent().getStringExtra("MY DATA KEY");
// Done!
where I can put SharedPreference in my working progress thesis project. I'm having a simple trouble in here. here is my sample code for loginActivity im still wondering on how/where I put the other SharedPreference code.
Im having trouble with one time login using SharedPreference .I don't know where do i put the code. i searched thru the net but nothing works for me i know i am having trouble placing the code but i can't fix it.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
auth = FirebaseAuth.getInstance();
progressBar = findViewById(R.id.prgBar);
btnLogin = findViewById(R.id.user_login);
userEmail = findViewById(R.id.user_email);
userPwd = findViewById(R.id.user_password);
btnReg = findViewById(R.id.user_reg);
btnReg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
}
});
btnLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String email = userEmail.getText().toString();
final String password = userPwd.getText().toString();
if (TextUtils.isEmpty(email)) {
Toast.makeText(getApplicationContext(), "Enter email address!", Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(password)) {
Toast.makeText(getApplicationContext(), "Enter password!", Toast.LENGTH_SHORT).show();
return;
}
//authenticate user
auth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(LoginActivity.this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
// If sign in fails, display a message to the user. If sign in succeeds
// the auth state listener will be notified and logic to handle the
// signed in user can be handled in the listener.
if (!task.isSuccessful()) {
// there was an error
if (password.length() < 6) {
userPwd.setError(getString(R.string.minimum_password));
} else {
Toast.makeText(LoginActivity.this, getString(R.string.auth_failed), Toast.LENGTH_LONG).show();
}
} else {
Intent intent = new Intent(LoginActivity.this, userInterface.class);
startActivity(intent);
finish();
}
}
});
}
});
}
}
Since you are using Firebase-authentication, you do not need to use SharedPreferences to see if the user had logged in before.
You can do this, example in the Splash Activity //first activity
FirebaseUser user=FirebaseAuth.getInstance().getCurrentUser();
if(user!=null){
//Signed in, go to home activity
Intent i=new Intent(SplashActivity.this,HomeActivity.class);
startActivity(i);
}
else{
//not logged in
Intent intent=new Intent(SplashActivity.this,LoginActivity.class);
startActivity(intent);
}
I make an sign up activity to create new user on Firebase using Simple Email Password shown below
CreateAccountActivity
public class CreateAccountActivity extends AppCompatActivity {
private ProgressDialog mprogress;
private RelativeLayout signUpactivity;
FirebaseUser user;
private Button msignUp;
public EditText mName,mEmail,mPassword,mCon_Password,mschl_name;
Spinner gender;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_creat_acc);
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
mAuth = FirebaseAuth.getInstance();
mprogress = new ProgressDialog(this);
msignUp = (Button) findViewById(R.id.signUp);
signUpactivity = (RelativeLayout) findViewById(R.id.activity_signUp);
mName = (EditText) findViewById(R.id.namefield);
mEmail = (EditText) findViewById(R.id.emailField);
mPassword = (EditText) findViewById(R.id.passwordField);
mschl_name = (EditText) findViewById(R.id.sname);
String[] option = new String[]{
"Male", "Female"
};
gender = (Spinner) findViewById(R.id.gender);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, option);
arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
gender.setAdapter(arrayAdapter);
msignUp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(isNetworkAvailable()){
mprogress.setMessage("Creating Account...");
mprogress.show();
mprogress.setCanceledOnTouchOutside(false);
mprogress.setCancelable(false);
createAccount();
}else{
Snackbar.make(signUpactivity, "Network UnAvailable", Snackbar.LENGTH_SHORT).show();
}
}
});
}
private boolean isNetworkAvailable() {
ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = manager.getActiveNetworkInfo();
return networkInfo != null && networkInfo.isConnected();
}
View focusview = null;
boolean cancel = false;
private void createAccount(){
final String name = mName.getText().toString();
String email = mEmail.getText().toString();
String password = mPassword.getText().toString();
final String gendertxt = gender.getSelectedItem().toString();
final String schl_name = mschl_name.getText().toString();
String edu_email = email+"#edutree.com";
if (isNetworkAvailable()){
mAuth.createUserWithEmailAndPassword(edu_email,password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(Task<AuthResult> task) {
if(task.isSuccessful()){
user = FirebaseAuth.getInstance().getCurrentUser();
String user_email = user.getEmail();
String gendertxt = gender.getSelectedItem().toString();
UserProfileChangeRequest profileChangeRequest = new UserProfileChangeRequest.Builder().setDisplayName(name).build();
user.updateProfile(profileChangeRequest).addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(Task<Void> task) {
if(task.isSuccessful()){
Log.d("Profile", "User Profile Updated successfully");
FirebaseCrash.log("Profile Updated");
}else {
Log.d("Error","error while updating profile");
FirebaseCrash.log("Error while updating profile");
}
}
});
mprogress.dismiss();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(CreateAccountActivity.this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
Toast.makeText(CreateAccountActivity.this, "Welcome "+ FirebaseAuth.getInstance().getCurrentUser().getEmail(), Toast.LENGTH_LONG).show();
}
},500);
}else{
Snackbar.make(signUpactivity, "Error occurred" , Snackbar.LENGTH_SHORT).show();
mprogress.dismiss();
}
}
});
}else {
Snackbar.make(signUpactivity, "Network UnAvailable", Snackbar.LENGTH_SHORT).show();
mprogress.dismiss();
}
}
}
When user enters the information it start method CreateAccount() and then when task is successful it updates user profile and set the display name and Starts the MainActivity. but it starts the MainActivty its not showing the user name.
MainActivity
Can Anyone help me to fix this problem, and when user get sign out and sign in again then it shows the name otherwise it didn't show after Sign up.
Any Answers were highly appreciated
You need to move the code that starts MainActivity to inside the onComplete() method for the profile change. Right now your code starts the Firebase user profile change to store the display name, but it shows MainActivity before that user profile change is completed.
You can update this piece of your code, look at what I added at the end of the onComplete() method:
user.updateProfile(profileChangeRequest).addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(Task<Void> task) {
if(task.isSuccessful()){
Log.d("Profile", "User Profile Updated successfully");
FirebaseCrash.log("Profile Updated");
}else {
Log.d("Error","error while updating profile");
FirebaseCrash.log("Error while updating profile");
}
mprogress.dismiss();
Intent intent = new Intent(CreateAccountActivity.this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
Toast.makeText(CreateAccountActivity.this, "Welcome "+ FirebaseAuth.getInstance().getCurrentUser().getEmail(), Toast.LENGTH_LONG).show();
}
});
Then, you can remove this code later in the function:
mprogress.dismiss();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(CreateAccountActivity.this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
Toast.makeText(CreateAccountActivity.this, "Welcome "+ FirebaseAuth.getInstance().getCurrentUser().getEmail(), Toast.LENGTH_LONG).show();
}
},500);
I just started with fragments and want to learn how to do more functional app with sliding menu but I get error on setcontentview because I want to make a login screen which is an activity.
links to my tutorials.
slide menu:
android sliding menu
and login screen:
android login and register
public class LoginActivity extends Fragment {
public LoginActivity() {
// Required empty public constructor
}
private static final String TAG = RegisterActivity.class.getSimpleName();
private Button btnLogin;
private Button btnLinkToRegister;
private EditText inputEmail;
private EditText inputPassword;
private ProgressDialog pDialog;
private SessionManager session;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_login);
inputEmail = (EditText) getView().findViewById(R.id.email);
inputPassword = (EditText) getView().findViewById(R.id.password);
btnLogin = (Button) getView().findViewById(R.id.btnLogin);
btnLinkToRegister = (Button) getView().findViewById(R.id.btnLinkToRegisterScreen);
// Progress dialog
pDialog = new ProgressDialog(this);
pDialog.setCancelable(false);
// Session manager
session = new SessionManager(getApplicationContext());
// Check if user is already logged in or not
if (session.isLoggedIn()) {
// User is already logged in. Take him to main activity
Intent intent = new Intent(LoginActivity.this, LoginMainActivity.class);
startActivity(intent);
finish();
}
// Login button Click Event
btnLogin.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String email = inputEmail.getText().toString();
String password = inputPassword.getText().toString();
// Check for empty data in the form
if (email.trim().length() > 0 && password.trim().length() > 0) {
// login user
checkLogin(email, password);
} else {
// Prompt user to enter credentials
Toast.makeText(getApplicationContext(),
"Please enter the credentials!", Toast.LENGTH_LONG)
.show();
}
}
});
// Link to Register Screen
btnLinkToRegister.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(),
RegisterActivity.class);
startActivity(i);
finish();
}
});
}
/**
* function to verify login details in mysql db
* */
private void checkLogin(final String email, final String password) {
// Tag used to cancel the request
String tag_string_req = "req_login";
pDialog.setMessage("Logging in ...");
showDialog();
StringRequest strReq = new StringRequest(Method.POST,
AppConfig.URL_REGISTER, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.d(TAG, "Login Response: " + response.toString());
hideDialog();
try {
JSONObject jObj = new JSONObject(response);
boolean error = jObj.getBoolean("error");
// Check for error node in json
if (!error) {
// user successfully logged in
// Create login session
session.setLogin(true);
// Launch main activity
Intent intent = new Intent(LoginActivity.this,
LoginMainActivity.class);
startActivity(intent);
finish();
} else {
// Error in login. Get the error message
String errorMsg = jObj.getString("error_msg");
Toast.makeText(getApplicationContext(),
errorMsg, Toast.LENGTH_LONG).show();
} `
I get error at: -setContentView,
- pDialog = new ProgressDialog(this);
- session = new SessionManager(getApplicationContext());
- Intent intent = new Intent(LoginActivity.this, LoginMainActivity.class);
startActivity(intent);
finish();
You mixed an Activity and a Fragment. If you use setContentView, then you have to extend an Activity.
Also you get an error here:
Intent intent = new Intent(LoginActivity.this,
LoginMainActivity.class);
because the LoginActivity is a fragment.
Also, here
Intent i = new Intent(getApplicationContext(),
RegisterActivity.class);
startActivity(i);
change getApplicationContext to LoginActivity.this after changing the superclass of LoginActivity to Activity.