我正在使用Android应用程序来显示网络错误。
NetErrorPage.java
package exp.app;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class NetErrorPage extends Activity implements OnClickListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.neterrorlayout);
Button reload=(Button)findViewById(R.id.btnReload);
reload.setOnClickListener(this);
showInfoMessageDialog("Please check your network connection","Network Alert");
}
public void onClick(View arg0)
{
if(isNetworkAvailable())
{
Intent myIntent = new Intent((Activity)NetErrorPage.this, MainActivity.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
((Activity)NetErrorPage.this).startActivity(myIntent);
finish();
}
else
showInfoMessageDialog("Please check your network connection","Network Alert");
}
public void showInfoMessageDialog(String message,String title)
{
AlertDialog alertDialog = new AlertDialog.Builder(NetErrorPage.this).create();
alertDialog.setTitle("Network Alert");
alertDialog.setMessage(message);
alertDialog.setButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which)
{
dialog.cancel();
}
});
alertDialog.show();
}
private boolean isNetworkAvailable()
{
NetworkInfo ActiveNetwork;
@SuppressWarnings("unused")
String IsNetworkConnected;
@SuppressWarnings("unused")
String ConnectionType;
ConnectivityManager connectivitymanager;
connectivitymanager=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
try
{
ActiveNetwork=connectivitymanager.getActiveNetworkInfo();
ConnectionType=ActiveNetwork.getTypeName();
IsNetworkConnected=String.valueOf(ActiveNetwork.getState());
return true;
}
catch(Exception error)
{
return false;
}
}
}
但我收到的错误如下,
08-17 11:59:08.019: E/WindowManager(16460): Activity exp.app.NetErrorPage has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40534a18 that was originally added here
08-17 11:59:08.019: E/WindowManager(16460): android.view.WindowLeaked: Activity exp.app.NetErrorPage has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40534a18 that was originally added here
08-17 11:59:08.019: E/WindowManager(16460): at android.view.ViewRoot.<init>(ViewRoot.java:263)
08-17 11:59:08.019: E/WindowManager(16460): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
08-17 11:59:08.019: E/WindowManager(16460): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
08-17 11:59:08.019: E/WindowManager(16460): at android.view.Window$LocalWindowManager.addView(Window.java:424)
08-17 11:59:08.019: E/WindowManager(16460): at android.app.Dialog.show(Dialog.java:241)
08-17 11:59:08.019: E/WindowManager(16460): at sync.directtrac.NetError.showInfoMessageDialog(NetErrorPage.java:114)
08-17 11:59:08.019: E/WindowManager(16460): at sync.directtrac.NetError.onCreate(NetErrorPage.java:26)
08-17 11:59:08.019: E/WindowManager(16460): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-17 11:59:08.019: E/WindowManager(16460): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
08-17 11:59:08.019: E/WindowManager(16460): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
08-17 11:59:08.019: E/WindowManager(16460): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-17 11:59:08.019: E/WindowManager(16460): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
08-17 11:59:08.019: E/WindowManager(16460): at android.os.Handler.dispatchMessage(Handler.java:99)
08-17 11:59:08.019: E/WindowManager(16460): at android.os.Looper.loop(Looper.java:130)
08-17 11:59:08.019: E/WindowManager(16460): at android.app.ActivityThread.main(ActivityThread.java:3687)
08-17 11:59:08.019: E/WindowManager(16460): at java.lang.reflect.Method.invokeNative(Native Method)
08-17 11:59:08.019: E/WindowManager(16460): at java.lang.reflect.Method.invoke(Method.java:507)
08-17 11:59:08.019: E/WindowManager(16460): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
08-17 11:59:08.019: E/WindowManager(16460): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
08-17 11:59:08.019: E/WindowManager(16460): at dalvik.system.NativeStart.main(Native Method)
我已经搜索了更多...但我没有任何正确的想法来清除它。
我想要的是,在加载此页面时,应添加布局并显示对话框。
请帮我清除此错误
注意:我也试过这个
@Override
protected void onResume() {
super.onResume();
runOnUiThread(new Runnable() {
public void run() {
showInfoMessageDialog("Please check your network connection","Network Alert");
}
});
}
答案 0 :(得分:74)
谢谢大家给我很多建议。最后我得到了解决方案。那就是我已经启动了两次NetErrorPage intent。有一次,我检查了网络连接可用性并在页面启动事件中启动了意图。第二次,如果页面有错误,那么我已经在OnReceivedError事件中启动了意图。因此,第一次对话框未关闭,之前调用第二个对话框。所以我得到了一个错误。
错误原因:我调用了showInfoMessageDialog方法 关闭第一个之前两次。
现在我已经删除了第二个电话和已清除的错误: - )。
答案 1 :(得分:57)
将此dialog.cancel();
更改为dialog.dismiss();
解决方案是在退出dismiss()
之前调用您在 NetErrorPage.java:114 中创建的Dialog
上的Activity
,例如在onPause()
。
视图引用了它们的父Context
(取自构造函数参数)。如果您在不破坏Activity
和其他动态创建的Dialog
的情况下离开View
,则他们仍然会将此引用保留为Activity
(如果您使用Context
创建new ProgressDialog(this)
}}:like {{1}}),因此GC无法收集它,导致内存泄漏。
答案 2 :(得分:5)
在我的情况下finish()
在对话框显示后立即执行。
答案 3 :(得分:4)
请尝试这种方式让我知道:
Context mContext;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.neterrorlayout);
mContext=NetErrorPage.this;
Button reload=(Button)findViewById(R.id.btnReload);
reload.setOnClickListener(this);
showInfoMessageDialog("Please check your network connection","Network Alert");
}
public void showInfoMessageDialog(String message,String title)
{
new AlertDialog.Builder(mContext)
.setTitle("Network Alert");
.setMessage(message);
.setButton("OK",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which)
{
dialog.cancel();
}
})
.show();
}
答案 4 :(得分:3)
只有在初始化Activity的窗口状态后才需要启动对话框 这只发生在onresume之后。
所以请致电
runOnUIthread(new Runnable(){
showInfoMessageDialog("Please check your network connection","Network Alert");
});
在你的OnResume函数中。不要在OnCreate中创建对话框 修改强>
使用此
Handler h = new Handler();
h.postDelayed(new Runnable(){
showInfoMessageDialog("Please check your network connection","Network Alert");
},500);
在你的Onresume而不是showonuithread
答案 5 :(得分:1)
@Override
protected void onPostExecute(final Boolean success) {
mProgressDialog.dismiss();
mProgressDialog = null;
设置值null对我有用
答案 6 :(得分:0)
我收到了这个错误,但它解决了很有趣。首先,我在api级别17得到了这个错误。当我在没有进度对话框的情况下调用一个线程(AsyncTask或其他)时,我再次使用进度对话框调用另一个线程方法,我得到了崩溃,原因是关于进度对话框的使用。
就我而言,有两个结果;
show();
进度对话框方法,然后在最后一个线程结束之前我采用了dismiss();
进度对话框方法。 所以:
ProgresDialog progressDialog = new ...
//configure progressDialog
progressDialog.show();
start firstThread {
...
}
...
start lastThread {
...
}
//be sure to finish threads
progressDialog.dismiss();
答案 7 :(得分:0)
我解决此问题的方法是不在对话框中调用意图。 ****相应地使用适用于活动或片段的语法
@Override
public void onClick(DialogInterface dialog, int which) {
checkvariable= true;
getActivity().finish();
}
@Override
public void onStop() {
super.onStop();
if (checkvariable) {
startActivity(intent);
}
}