gpt4 book ai didi

android.view.WindowLeaked : Activity has leaked window com. android.internal.policy.impl.PhoneWindow$DecorView

转载 作者:行者123 更新时间:2023-11-30 01:44:41 32 4
gpt4 key购买 nike

我正在使用 android 登录和注册,我遇到了这个错误,我正在尝试解决它,但我是 android 的新手,请帮助我。 ServerRequest.class 是

package com.example.aou6kor.samplefragment;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

import java.util.ArrayList;

/**
* Created by Anil on 11/22/2015.
*/
public class ServerRequests {
public ProgressDialog progressDialog;
public static final int CONNECTION_TIMEOUT=1000*25;
public static final String SERVER_ADDRESS="http://trial.web44.net/";


public ServerRequests(Context context){

progressDialog=new ProgressDialog(context);
progressDialog.setCancelable(false);
progressDialog.setTitle("Processing");
progressDialog.setMessage("Please wait..");
}

public void storeUserDataInBackground(User user,GetUserCallback userCallback){
progressDialog.show();
new StoreUserDataAsyncTask(user,userCallback).execute();
progressDialog.dismiss(); //new change



}

public void fetchUserDataInBackground(User user,GetUserCallback callback){

progressDialog.show();
new fetchUserDataAsyncTask(user,callback).execute();
}

public class StoreUserDataAsyncTask extends AsyncTask<Void,Void,Void>{

User user;
GetUserCallback userCallback;

public StoreUserDataAsyncTask(User user,GetUserCallback userCallback){
this.user=user;
this.userCallback=userCallback;

}
@Override
protected Void doInBackground(Void... voids) {
ArrayList<NameValuePair>dataToSend=new ArrayList<>();
dataToSend.add(new BasicNameValuePair("name",user.name));
dataToSend.add(new BasicNameValuePair("username",user.username));
dataToSend.add(new BasicNameValuePair("phone",user.phone+""));
dataToSend.add(new BasicNameValuePair("mail",user.mail));
dataToSend.add(new BasicNameValuePair("password", user.password));


HttpParams httpParams=new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, CONNECTION_TIMEOUT);
HttpConnectionParams.setSoTimeout(httpParams, CONNECTION_TIMEOUT);

HttpClient client=new DefaultHttpClient(httpParams);
HttpPost post=new HttpPost(SERVER_ADDRESS+"Register.php");
try{
post.setEntity(new UrlEncodedFormEntity(dataToSend));
client.execute(post);
}catch (Exception e){
e.printStackTrace();
}



return null;
}
@Override
protected void onPostExecute(Void aVoid){
progressDialog.dismiss();
userCallback.done(null);
super.onPostExecute(aVoid);
}
}
public class fetchUserDataAsyncTask extends AsyncTask<Void,Void,User> {

User user;
GetUserCallback userCallback;

public fetchUserDataAsyncTask(User user, GetUserCallback userCallback) {
this.user = user;
this.userCallback = userCallback;

}

@Override
protected User doInBackground(Void... voids) {
ArrayList<NameValuePair>dataToSend=new ArrayList<>();
dataToSend.add(new BasicNameValuePair("username",user.username));
dataToSend.add(new BasicNameValuePair("password", user.password));

HttpParams httpParams=new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, CONNECTION_TIMEOUT);
HttpConnectionParams.setSoTimeout(httpParams, CONNECTION_TIMEOUT);


HttpClient client=new DefaultHttpClient(httpParams);
HttpPost post=new HttpPost(SERVER_ADDRESS+"FetchUserData.php");


User returnedUser=null;
try{
post.setEntity(new UrlEncodedFormEntity(dataToSend));
HttpResponse httpResponse= client.execute(post);

HttpEntity entity=httpResponse.getEntity();
String result= EntityUtils.toString(entity);
JSONObject jsonObject=new JSONObject(result);

if(jsonObject.length()==0){
returnedUser=null;
return returnedUser;
}else{
String name=jsonObject.getString("name");
int phone=jsonObject.getInt("phone");
String mail=jsonObject.getString("mail");

returnedUser =new User(name,user.username,phone,mail,user.password);
}

}catch (Exception e){
e.printStackTrace();
}




return returnedUser;
}
@Override
protected void onPostExecute(User returnedUser){
progressDialog.dismiss();
userCallback.done(returnedUser);
super.onPostExecute(returnedUser);
}

}

}

我的登录 Activity 是

package com.example.aou6kor.samplefragment;
import android.app.AlertDialog;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Login extends AppCompatActivity implements View.OnClickListener {

Button btLogin;
EditText etUsername,etPassword;
TextView registerLink;
UserLocalStore userLocalStore;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
etUsername=(EditText)findViewById(R.id.etUsername);
etPassword=(EditText)findViewById(R.id.etPassword);
registerLink=(TextView)findViewById(R.id.tvRegisterLink);
btLogin=(Button)findViewById(R.id.btLogin);
btLogin.setOnClickListener(this);
registerLink.setOnClickListener(this);
userLocalStore=new UserLocalStore(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_login, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}

@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.btLogin:
String username=etUsername.getText().toString();
String password=etPassword.getText().toString();
User user=new User(username,password);

authenticate(user);
userLocalStore.setUserLoggedIn(true); //storing user data
userLocalStore.storeUserData(user);
startActivity(new Intent(this, MainActivity.class));

break;

case R.id.btRegister:
startActivity(new Intent(this,Register.class));
break;
}

}

private void authenticate(User user){
ServerRequests serverRequests=new ServerRequests(this);
serverRequests.fetchUserDataInBackground(user, new GetUserCallback() {
@Override
public void done(User returnedUser) {
if(returnedUser==null){
showErrorMsg();
}else {
logUserIn(returnedUser);
}
}
});
}
private void showErrorMsg(){
AlertDialog.Builder builder=new AlertDialog.Builder(Login.this);
builder.setMessage("wrong details");
builder.setPositiveButton("ok",null);
builder.show();

}
private void logUserIn(User returnedUser){
userLocalStore.storeUserData(returnedUser);
userLocalStore.setUserLoggedIn(true);

startActivity(new Intent(this,MainActivity.class));
}

}

logcat 中的错误是,

- 4733-4733/com.example.aou6kor.samplefragment E/WindowManager﹕ android.view.WindowLeaked: Activity com.example.aou6kor.samplefragment.Login has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{2b1ee4b3 V.E..... R......D 0,0-684,322} that was originally added here
at android.view.ViewRootImpl.<init>(ViewRootImpl.java:375)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:261)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
at android.app.Dialog.show(Dialog.java:298)
at com.example.aou6kor.samplefragment.ServerRequests.fetchUserDataInBackground(ServerRequests.java:51)
at com.example.aou6kor.samplefragment.Login.authenticate(Login.java:81)
at com.example.aou6kor.samplefragment.Login.onClick(Login.java:65)
at android.view.View.performClick(View.java:4761)
at android.view.View$PerformClick.run(View.java:19767)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5312)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)

logcat 中的其他错误是,

11-25 07:08:08.114    7403-7403/com.example.aou6kor.samplefragment E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.aou6kor.samplefragment, PID: 7403
java.lang.IllegalArgumentException: View=com.android.internal.policy.impl.PhoneWindow$DecorView{13f24daf V.E..... R......D 0,0-684,322} not attached to window manager
at android.view.WindowManagerGlobal.findViewLocked(WindowManagerGlobal.java:386)
at android.view.WindowManagerGlobal.removeView(WindowManagerGlobal.java:312)
at android.view.WindowManagerImpl.removeViewImmediate(WindowManagerImpl.java:84)
at android.app.Dialog.dismissDialog(Dialog.java:341)
at android.app.Dialog.dismiss(Dialog.java:324)
at com.example.aou6kor.samplefragment.ServerRequests$fetchUserDataAsyncTask.onPostExecute(ServerRequests.java:159)
at com.example.aou6kor.samplefragment.ServerRequests$fetchUserDataAsyncTask.onPostExecute(ServerRequests.java:102)
at android.os.AsyncTask.finish(AsyncTask.java:632)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5312)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)

最佳答案

ProgressDialog 已在您在此方法中调用时添加:

public void fetchUserDataInBackground(User user,GetUserCallback callback){
progressDialog.show();
new fetchUserDataAsyncTask(user,callback).execute();
}

将其更改为:

public void fetchUserDataInBackground(User user,GetUserCallback callback){
if (!progressDialog.isShowing()) {
progressDialog.show();
}
new fetchUserDataAsyncTask(user,callback).execute();
}

关于android.view.WindowLeaked : Activity has leaked window com. android.internal.policy.impl.PhoneWindow$DecorView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33900965/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com