Activity has leaked window 原来是加了logcat错误
Posted
技术标签:
【中文标题】Activity has leaked window 原来是加了logcat错误【英文标题】:Activity has leaked window that was originally added logcat error 【发布时间】:2020-12-02 01:35:25 【问题描述】:请解决我的错误。我无法弄清楚。
当我点击提交按钮时,我的应用程序崩溃了。为什么?如果您需要我的更多输入,请告诉我。
MainActivity.java
public class MainActivity extends AppCompatActivity
Button btn;
EditText txt1, txt2,txt3;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=findViewById(R.id.button);
txt1=findViewById(R.id.editTextTextPersonName);
txt2=findViewById(R.id.editTextTextEmailAddress2);
txt3=findViewById(R.id.editTextTextPassword);
SharedPreferences pref = getSharedPreferences("mypref", MODE_PRIVATE);
Boolean islogged=pref.getBoolean("key3",false);
if (islogged)
Intent i=new Intent(MainActivity.this,MainActivity2.class);
startActivity(i);
finish();
btn.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
String email = txt2.getText().toString();
String password = txt3.getText().toString();
Intent i=new Intent(MainActivity.this,MainActivity2.class);
startActivity(i);
SharedPreferences pref = getSharedPreferences("mypref", MODE_PRIVATE);
SharedPreferences.Editor ed = pref.edit();
ed.putString("key1", email);
ed.putString("key2", password);
ed.putBoolean("key3",true);
ed.apply();
);
MainActivity2.java
public class MainActivity2 extends AppCompatActivity
Button btn;
EditText txt1, txt2;
CheckBox keeplog;
Boolean b=false;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
btn=findViewById(R.id.login);
txt1=findViewById(R.id.loginemail);
txt2=findViewById(R.id.loginpassword);
keeplog=findViewById(R.id.checkBox);
keeplog.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
b=isChecked;
);
SharedPreferences pref = getSharedPreferences("mypref", MODE_PRIVATE);
Boolean islogin=pref.getBoolean("key4",false);
if (islogin)
Intent i=new Intent(MainActivity2.this,HomeScreen.class);
startActivity(i);
finish();
btn.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
SharedPreferences pref = getSharedPreferences("mypref", MODE_PRIVATE);
String email = pref.getString("key1", null);
String password = pref.getString("key2", null);
String email2 = txt1.getText().toString();
String password2 = txt2.getText().toString();
SharedPreferences pref2 = getSharedPreferences("mypref", MODE_PRIVATE);
SharedPreferences.Editor ed = pref2.edit();
ed.putBoolean("key4",b);
ed.apply();
if (email.equals(email2) && password.equals(password2))
Intent i = new Intent(MainActivity2.this, HomeScreen.class);
startActivity(i);
finish();
);
HomeScreen.java
public class HomeScreen extends AppCompatActivity
Button button1,button2;
EditText n;
ProgressDialog pd;
String Note;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_screen);
Toast.makeText(this, "Welcome Home", Toast.LENGTH_SHORT).show();
@Override
public boolean onCreateOptionsMenu(Menu menu)
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item)
if (item.getItemId() == R.id.id1)
final AlertDialog dialogBuilder = new AlertDialog.Builder(this).create();
LayoutInflater inflater = this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.custom_dialog, null);
n = (EditText) dialogView.findViewById(R.id.edt_comment);
button1 = (Button) dialogView.findViewById(R.id.buttonSubmit);
button2 = (Button) dialogView.findViewById(R.id.buttonCancel);
dialogBuilder.setView(dialogView);
dialogBuilder.show();
button2.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
dialogBuilder.dismiss();
);
button1.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
SAVETASK();
// dialogBuilder.dismiss();
);
else if (item.getItemId() == R.id.id2)
startActivity(new Intent(HomeScreen.this,MainActivity2.class));
return super.onOptionsItemSelected(item);
public void SAVETASK()
Note = n.getText().toString();
//password = txt2.getText().toString();
class Savedata extends AsyncTask<Void, Void, Void>
@Override
protected void onPreExecute()
super.onPreExecute();
pd = new ProgressDialog(HomeScreen.this);
pd.setTitle("Saving data");
pd.setMessage("Please Wailt");
pd.show();
@Override
protected Void doInBackground(Void... voids)
Task task = new Task();
task.setNote(Note);
DatabaseClient.getInstance(getApplicationContext()).getAppDatabase().taskdao().insert(task);
return null;
@Override
protected void onPostExecute(Void aVoid)
super.onPostExecute(aVoid);
pd.dismiss();
Toast.makeText(HomeScreen.this, "Data inserted", Toast.LENGTH_SHORT).show();
// Intent intent = new Intent(HomeScreen.this, MainActivity2.class);
// startActivity(intent);
Savedata savedata = new Savedata();
savedata.execute();
Task.java
@Entity
public class Task implements Serializable
@PrimaryKey(autoGenerate = true)
private int id;
@ColumnInfo(name = "name")
private String Note;
public int getId()
return id;
public void setId(int id)
this.id = id;
public String getNote()
return Note;
public void setNote(String note)
Note = note;
TaskDao.java
@Dao
public interface TaskDao
@Query("select * from task")
List<Task> getalldata();
@Insert
void insert(Task task);
@Update
void update(Task task);
@Delete
void Delete(Task task);
AppDatabase.java
public abstract class AppDatabase extends RoomDatabase
public abstract TaskDao taskdao();
DatabaseClient.java
public class DatabaseClient
private Context context;
private static DatabaseClient mInstance;
private AppDatabase appDatabase;
private DatabaseClient(Context mctx)
this.context=mctx;
appDatabase= Room.databaseBuilder(mctx,AppDatabase.class,"MYTODO").build();
public static synchronized DatabaseClient getInstance(Context context)
if(mInstance==null)
mInstance=new DatabaseClient(context);
return mInstance;
public AppDatabase getAppDatabase()
return appDatabase;
Logcat 错误:
com.example.sharedprefdemo E/WindowManager: android.view.WindowLeaked: Activity com.example.sharedprefdemo.HomeScreen has leaked window DecorView@d93e50e[Saving data] that was originally added here
at android.view.ViewRootImpl.<init>(ViewRootImpl.java:765)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:429)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:95)
at android.app.Dialog.show(Dialog.java:473)
at com.example.sharedprefdemo.HomeScreen$1Savedata.onPreExecute(HomeScreen.java:95)
at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:708)
at android.os.AsyncTask.execute(AsyncTask.java:655)
at com.example.sharedprefdemo.HomeScreen.SAVETASK(HomeScreen.java:117)
at com.example.sharedprefdemo.HomeScreen$2.onClick(HomeScreen.java:70)
at android.view.View.performClick(View.java:7870)
at android.widget.TextView.performClick(TextView.java:14970)
at android.view.View.performClickInternal(View.java:7839)
at android.view.View.access$3600(View.java:886)
at android.view.View$PerformClick.run(View.java:29363)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:237)
at android.app.ActivityThread.main(ActivityThread.java:7814)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1068)
所以当我点击提交按钮时,我的应用程序会自动崩溃。我不知道是什么原因。
【问题讨论】:
【参考方案1】:显示对话框之前
如果(对话!= null && dialog.isShowing()) dialog.dismiss();//或取消()
尝试从异步线程中移动对话框及其初始化
HomeScreen 泄露了最初添加在这里的窗口 DecorView@d93e50e[Saving data] answer to this error
【讨论】:
对不起兄弟我没听懂你说的。以上是关于Activity has leaked window 原来是加了logcat错误的主要内容,如果未能解决你的问题,请参考以下文章
Android Activity has leaked window that was originally added
Activity has leaked window 原来是加了logcat错误
(原)android系统下绑定Server的时候报MainActivity has leaked ServiceConnection的错误
has leaked ServiceConnection com.baidu.location.LocationClient
修复A Native Collection has not been disposed, resulting in a memory leak.
Android 模拟器向 logcat 发送垃圾邮件,内容为“Service com.android.exchange.ExchangeService has leaked ServiceConnec