Tengo en mi aplicacionaplicación varios activity, la principal es la del login y el home, lo que necesito es que alguna de las funciones del home puedan ser accesadas sin iniciar sesionsesión, entonces puse un botonbotón en el login que deberiadebería hacer lo mismo que el botonbotón del home, pero al invocar el metodométodo esta se cierra. Cual es la manera correcta de hacerlo?
LoginActivity:aqui es donde tengo que invocar la funcionfunción vinculada al btnSucursales pero cuando realizo el onclicklistener y lo clickeo en el simulador este crashea
import android.content.Intent;
import android.content.IntentSender;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.material.snackbar.Snackbar;
import com.google.android.play.core.appupdate.AppUpdateInfo;
import com.google.android.play.core.appupdate.AppUpdateManager;
import com.google.android.play.core.appupdate.AppUpdateManagerFactory;
import com.google.android.play.core.install.InstallState;
import com.google.android.play.core.install.InstallStateUpdatedListener;
import com.google.android.play.core.install.model.AppUpdateType;
import com.google.android.play.core.install.model.InstallStatus;
import com.google.android.play.core.install.model.UpdateAvailability;
import com.google.android.play.core.tasks.OnSuccessListener;
import com.google.gson.Gson;
import es.dmoral.toasty.Toasty;
import main.laboratus.app.main.HomeFragment;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class LoginActivity extends AppCompatActivity {
Button btnLogin;
ImageView btnSucursales, btnPromociones;
EditText edUsername, edPassword;
TextView tvForgetPassword;
HomeFragment metodo = new HomeFragment();
AppUpdateManager mAppUpdateManager;
private final static int RC_APP_UPDATE=659;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
btnLogin = findViewById(R.id.btnLogin);
btnSucursales = findViewById(R.id.btnSucursales);
btnPromociones = findViewById(R.id.btnPromociones);
edUsername = findViewById(R.id.et_email);
edPassword = findViewById(R.id.et_password);
tvForgetPassword = findViewById(R.id.tvForgetPassword);
tvForgetPassword.setOnClickListener(view -> startActivity(new Intent(LoginActivity.this, UsernameActivity.class)));
//
btnLogin.setOnClickListener(view -> {
if(TextUtils.isEmpty(edUsername.getText().toString()) || TextUtils.isEmpty(edPassword.getText().toString()) ) {
String message = "Todas las entradas requeridas ..";
Toast.makeText(LoginActivity.this,message,Toast.LENGTH_LONG).show();
}else{
Toast.makeText(LoginActivity.this, "Autenticando usuario...",Toast.LENGTH_SHORT).show();
loginUser(1,edUsername.getText().toString(),edPassword.getText().toString());
}
});
mAppUpdateManager = AppUpdateManagerFactory.create(this);
mAppUpdateManager.getAppUpdateInfo().addOnSuccessListener(new OnSuccessListener<AppUpdateInfo>()
{
@Override
public void onSuccess(AppUpdateInfo result)
{
if(result.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE
&& result.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE))
{
try
{
mAppUpdateManager.startUpdateFlowForResult(result,AppUpdateType.FLEXIBLE, LoginActivity.this
,RC_APP_UPDATE);
} catch (IntentSender.SendIntentException e)
{
e.printStackTrace();
}
}
}
});
mAppUpdateManager.registerListener(installStateUpdatedListener);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data)
{
/* we can check without requestCode == RC_APP_UPDATE because
we known exactly there is only requestCode from startUpdateFlowForResult() */
if(requestCode == RC_APP_UPDATE && resultCode != RESULT_OK)
{
Toast.makeText(this, "Cancel", Toast.LENGTH_SHORT).show();
}
super.onActivityResult(requestCode, resultCode, data);
}
private InstallStateUpdatedListener installStateUpdatedListener =new InstallStateUpdatedListener()
{
@Override
public void onStateUpdate(InstallState state)
{
if(state.installStatus() == InstallStatus.DOWNLOADED)
{
showCompletedUpdate();
}
}
};
@Override
protected void onStop()
{
if(mAppUpdateManager!=null) mAppUpdateManager.unregisterListener(installStateUpdatedListener);
super.onStop();
}
public void loginUser(Integer ptipoUsuario, String pUsuario, String pClave){
Call<String> loginResponseCall = ApiClient.getService().loginUser(ptipoUsuario, pUsuario,pClave);
loginResponseCall.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
if(response.isSuccessful()){
Gson gson = new Gson();
LoginResponse loginResponse = gson.fromJson(response.body() , LoginResponse.class);
if (loginResponse.getNom_paciente().equals("0")) {
Toasty.error(LoginActivity.this, "No se encontró tal usuario.", Toast.LENGTH_SHORT, true).show();
}else {
startActivity(new Intent(LoginActivity.this, MainActivity.class)
.putExtra("data", loginResponse)
.putExtra("pId",Integer.parseInt(pUsuario)));
finish();
}
}else{
String message = "Ocurrió un error por favor intente más tarde...";
Toast.makeText(LoginActivity.this,message,Toast.LENGTH_LONG).show();
}
}
@Override
public void onFailure(Call<String> call, Throwable t) {
String message = t.getLocalizedMessage();
Toast.makeText(LoginActivity.this,message,Toast.LENGTH_LONG).show();
}
});
}
private void showCompletedUpdate()
{
Snackbar snackbar = Snackbar.make(findViewById(android.R.id.content),"New app is ready!",
Snackbar.LENGTH_INDEFINITE);
snackbar.setAction("Install", new View.OnClickListener()
{
@Override
public void onClick(View view)
{
mAppUpdateManager.completeUpdate();
}
});
snackbar.show();
}
}```
package main.laboratus.app;
import android.content.Intent;
import android.content.IntentSender;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.material.snackbar.Snackbar;
import com.google.android.play.core.appupdate.AppUpdateInfo;
import com.google.android.play.core.appupdate.AppUpdateManager;
import com.google.android.play.core.appupdate.AppUpdateManagerFactory;
import com.google.android.play.core.install.InstallState;
import com.google.android.play.core.install.InstallStateUpdatedListener;
import com.google.android.play.core.install.model.AppUpdateType;
import com.google.android.play.core.install.model.InstallStatus;
import com.google.android.play.core.install.model.UpdateAvailability;
import com.google.android.play.core.tasks.OnSuccessListener;
import com.google.gson.Gson;
import es.dmoral.toasty.Toasty;
import main.laboratus.app.main.HomeFragment;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class LoginActivity extends AppCompatActivity {
Button btnLogin;
ImageView btnSucursales, btnPromociones;
EditText edUsername, edPassword;
TextView tvForgetPassword;
HomeFragment metodo = new HomeFragment();
AppUpdateManager mAppUpdateManager;
private final static int RC_APP_UPDATE=659;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
btnLogin = findViewById(R.id.btnLogin);
btnSucursales = findViewById(R.id.btnSucursales);
btnPromociones = findViewById(R.id.btnPromociones);
edUsername = findViewById(R.id.et_email);
edPassword = findViewById(R.id.et_password);
tvForgetPassword = findViewById(R.id.tvForgetPassword);
tvForgetPassword.setOnClickListener(view -> startActivity(new Intent(LoginActivity.this, UsernameActivity.class)));
//
btnLogin.setOnClickListener(view -> {
if(TextUtils.isEmpty(edUsername.getText().toString()) || TextUtils.isEmpty(edPassword.getText().toString()) ) {
String message = "Todas las entradas requeridas ..";
Toast.makeText(LoginActivity.this,message,Toast.LENGTH_LONG).show();
}else{
Toast.makeText(LoginActivity.this, "Autenticando usuario...",Toast.LENGTH_SHORT).show();
loginUser(1,edUsername.getText().toString(),edPassword.getText().toString());
}
});
mAppUpdateManager = AppUpdateManagerFactory.create(this);
mAppUpdateManager.getAppUpdateInfo().addOnSuccessListener(new OnSuccessListener<AppUpdateInfo>()
{
@Override
public void onSuccess(AppUpdateInfo result)
{
if(result.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE
&& result.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE))
{
try
{
mAppUpdateManager.startUpdateFlowForResult(result,AppUpdateType.FLEXIBLE, LoginActivity.this
,RC_APP_UPDATE);
} catch (IntentSender.SendIntentException e)
{
e.printStackTrace();
}
}
}
});
mAppUpdateManager.registerListener(installStateUpdatedListener);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data)
{
/* we can check without requestCode == RC_APP_UPDATE because
we known exactly there is only requestCode from startUpdateFlowForResult() */
if(requestCode == RC_APP_UPDATE && resultCode != RESULT_OK)
{
Toast.makeText(this, "Cancel", Toast.LENGTH_SHORT).show();
}
super.onActivityResult(requestCode, resultCode, data);
}
private InstallStateUpdatedListener installStateUpdatedListener =new InstallStateUpdatedListener()
{
@Override
public void onStateUpdate(InstallState state)
{
if(state.installStatus() == InstallStatus.DOWNLOADED)
{
showCompletedUpdate();
}
}
};
@Override
protected void onStop()
{
if(mAppUpdateManager!=null) mAppUpdateManager.unregisterListener(installStateUpdatedListener);
super.onStop();
}
public void loginUser(Integer ptipoUsuario, String pUsuario, String pClave){
Call<String> loginResponseCall = ApiClient.getService().loginUser(ptipoUsuario, pUsuario,pClave);
loginResponseCall.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
if(response.isSuccessful()){
Gson gson = new Gson();
LoginResponse loginResponse = gson.fromJson(response.body() , LoginResponse.class);
if (loginResponse.getNom_paciente().equals("0")) {
Toasty.error(LoginActivity.this, "No se encontró tal usuario.", Toast.LENGTH_SHORT, true).show();
}else {
startActivity(new Intent(LoginActivity.this, MainActivity.class)
.putExtra("data", loginResponse)
.putExtra("pId",Integer.parseInt(pUsuario)));
finish();
}
}else{
String message = "Ocurrió un error por favor intente más tarde...";
Toast.makeText(LoginActivity.this,message,Toast.LENGTH_LONG).show();
}
}
@Override
public void onFailure(Call<String> call, Throwable t) {
String message = t.getLocalizedMessage();
Toast.makeText(LoginActivity.this,message,Toast.LENGTH_LONG).show();
}
});
}
private void showCompletedUpdate()
{
Snackbar snackbar = Snackbar.make(findViewById(android.R.id.content),"New app is ready!",
Snackbar.LENGTH_INDEFINITE);
snackbar.setAction("Install", new View.OnClickListener()
{
@Override
public void onClick(View view)
{
mAppUpdateManager.completeUpdate();
}
});
snackbar.show();
}
}