我正在使用 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");
}
});
}