我有一个带有两个按钮的片段。我已经实现了view.OnclickListener
方法,并在两个按钮上都设置了onclickistener
:
btn_password = (ImageButton) v.findViewById(R.id.btn_password);
btnEditar = (Button) v.findViewById(R.id.btnEditar);
btn_password.setOnClickListener(this);
btnEditar.setOnClickListener(this);
然后我实现了onclick
方法:
@Override
public void onClick(View view) {
Fragment fragment = null;
switch (view.getId()) {
case R.id.btnEditar:
registerUser(empresa,nombre,direccion,cif,tel,cuenta,id );
break;
case R.id.btn_password:
Log.d(TAG, "Register Response: id" + "clicked Change Password" );
Intent intent1 = new Intent(getActivity(), CambiarPasswordActivity.class);
startActivity(intent1);
break;
}
}
要检查是否单击了btn_password
,我提供了一条日志,用于确认单击了按钮。
我的问题是,意图不是启动活动,而是打开MainActivity
,这是保存当前片段的活动。
欢迎任何帮助
编辑
CambiarPassword活动代码
public class CambiarPasswordActivity extends AppCompatActivity {
private static final String URL = "https://.../update_password.php";
public static final String USERNAME = "username";
public static final String USERID = "unique_id";
private String user_uid,pass, pass2,usuario;
ProgressDialog progreso;
private TextView userName;
private SQLiteHandler db;
private SessionManager session;
private ProgressDialog loading;
private EditText password, password2;
public static String MISDATOS= "MisDatos";
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cambiar_password);
Log.d("CAMBIAR", "Register Response: id" + "clicked Change Password ESTOY EN CAMBIAR PASSWORD" );
password=(EditText) findViewById(R.id.password);
password2=(EditText) findViewById(R.id.password2);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pass = password.getText().toString().trim();
pass2 = password2.getText().toString().trim();
SharedPreferences prefs2 =
getSharedPreferences(MISDATOS, Context.MODE_PRIVATE);
usuario = prefs2.getString("ID", "por_defecto@email.com");
if(TextUtils.isEmpty(pass) || pass.length() < 6)
{
Toast.makeText(getApplicationContext(),
"Password too short, at least 6 characters!", Toast.LENGTH_LONG)
.show();
return;
}
if(TextUtils.isEmpty(pass2) || pass2.length() < 6)
{
Toast.makeText(getApplicationContext(),
"Password too short, at least 6 characters!", Toast.LENGTH_LONG)
.show();
return;
}
if (pass.equals(pass2) ){
Log.d("datis","CAMBIO "+pass+" "+usuario);
SendData();
}
else{
Toast.makeText(CambiarPasswordActivity.this,"Passwords don´t match",Toast.LENGTH_LONG).show();
}
}
});
// SqLite database handler
db = new SQLiteHandler(getApplicationContext());
// session manager
session = new SessionManager(getApplicationContext());
if (!session.isLoggedIn()) {
logoutUser();
}
}
private void logoutUser() {
session.setLogin(false);
db.deleteUsers();
// Launching the login activity
Intent intent = new Intent(CambiarPasswordActivity.this, LoginActivity.class);
startActivity(intent);
//finish();
}
public void SendData()
{
progreso = ProgressDialog.show(this, "Changing Password",
"Please, wait...", true);
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
System.out.println(response);
Log.d("datis","CAMBIO "+response);
Toast.makeText(CambiarPasswordActivity.this,response,Toast.LENGTH_LONG).show();
progreso.dismiss();
Intent intent = new Intent(CambiarPasswordActivity.this,
MainActivity.class);
startActivity(intent);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(CambiarPasswordActivity.this,error.toString(),Toast.LENGTH_LONG).show();
}
}){
@Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<String, String>();
params.put("password",pass);
params.put("usuario", usuario);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
}
答案 0 :(得分:2)
尝试这个。
@Override
public void onClick(View view) {
Fragment fragment = null;
switch (view.getId()) {
case R.id.btnEditar:
registerUser(empresa,nombre,direccion,cif,tel,cuenta,id );
break;
case R.id.btn_password:
Log.d(TAG, "Register Response: id" + "clicked Change Password" );
Intent intent1 = new Intent(getActivity(), CambiarPasswordActivity.class);
getActivity().startActivity(intent1);
break;
}
}
答案 1 :(得分:1)
所以实际的问题是
if (!session.isLoggedIn()) {
logoutUser();
}
由于logoutUser()
返回
session.isLoggedIn()
方法正在执行
false
。由于上述if
条件满足,并且您从logoutUser()
到Launching Intent
就是LoginActivity
。
这会成为问题,因此您需要根据自己的要求处理session.isLoggedIn()
。
谢谢
答案 2 :(得分:0)
尝试一下:
MainActivity
)的活动中,创建一个启动CambiarPasswordActivity
并将其设置为public
的方法。MainActivity.class
public void launchCambiarPasswordActivity(){
startActivity(new Intent(this, CambiarPasswordActivity.class));
}
2。并从片段中调用此方法:
YourFragment.class
@Override
public void onClick(View view) {
Fragment fragment = null;
switch (view.getId()) {
case R.id.btnEditar:
registerUser(empresa,nombre,direccion,cif,tel,cuenta,id );
break;
case R.id.btn_password:
Log.d(TAG, "Register Response: id" + "clicked Change Password" );
// Call the new method.
((MainActivity) getActivity()).launchCambiarPasswordActivity();
break;
}
}