我可以知道,我总是可以安全Context地Activity进入View吗?
View {
Activity activity = (Activity)this.getContext();
}
Run Code Online (Sandbox Code Playgroud)
到目前为止,它一直很好.我想知道,上面的代码会失败吗?
我们尝试了一切来完成特定活动,但未能这样做.代码运行没有任何错误或警告只是无法完成活动.我们尝试了stackoverflow中的每个解决方案以及其他论坛.需要一个解释的解决方案.
public void HttpSmsRequest(final String Phone){
final ProgressDialog pDialog = new ProgressDialog(this);
pDialog.setMessage("Loading...";
pDialog.show();
Map<String, String> jsonParams = new HashMap<String, String>();
// jsonParams.put("param1", youParameter);
jsonParams.put("Phone", Phone);
//jsonParams.put("rememberMe", "true";
JsonObjectRequest myRequest = new JsonObjectRequest(
Request.Method.POST,
AppGlobal.host+"PhoneVerification/sendSms",
new JSONObject(jsonParams),
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try
{
Log.v("Success", "success: " + response.toString());
//MessageBox.Show(ProfileInfoActivity.this, "Response: " + response.toString());
pDialog.dismiss();
JSONObject obj=new JSONObject(response.toString());
// String ID=obj.getString("ID";
String Flag=obj.getString("Flag";
String Message=obj.getString("Message";
// Context context = getApplicationContext();
//
/// appPrefs.setUserIdentity(Integer.parseInt(ID)); …Run Code Online (Sandbox Code Playgroud) 这是我的代码:
private void makeDialog2() {
AlertDialog.Builder about = new AlertDialog.Builder(getContext());
about.setTitle("You Won!");
about.setPositiveButton("Play Again",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
Intent playIntent2 = new Intent(getContext(),
PracticePlayActivity.class);
playIntent2.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
getContext().startActivity(playIntent2);
((Activity) getContext()).finish();
}
});
about.setNegativeButton("Exit", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg1, int arg2) {
Intent playIntent = new Intent(getContext(),
PlayChooserActivity.class);
playIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
getContext().startActivity(playIntent);
((Activity) getContext()).finish();
}
});
about.show();
}
Run Code Online (Sandbox Code Playgroud)
当用户输掉游戏并想要重试时,会提示此代码.但是当我按下重试次数超过4次时,应用程序崩溃了.我怀疑是内存泄漏.在logcat中进行一些测试后,我设法在重试后发现活动仍在运行.
我的计划是用两件事来解决这个问题.回收我的drawables并结束整个第一个活动.但是,即使在我打完成后,第一个活动也没有关闭.有帮助吗?(在我的代码的其他部分使用getContext()到目前为止已经工作).
编辑:通过结束活动它会自动销毁变量吗?还是我还需要清除Android内存中的位图?我有什么想法可以做到这一点?