Ext.: http://blog.csdn.net/wotoumingzxy/article/details/7797295
This exception probably means an exception when the main thread accesses the network. Android's version prior to 4.0 supported access to the network in the main thread, but after 4.0 it was optimized for this part of the program, meaning that the code to access the network could not be written in the main thread.
So what do you do to get it working? Please see here:
For example, get a URL address from the network.
url = Authutil.getauthorizationurl (); if (url = = null) { toast.maketext (webviewactivity.this, R.string.auth_url_empty,.). Show ();
If it is written directly in the main thread, it will still work in the 2.x version, but after 4.0, an error occurs.
There are a lot of relevant information on the Internet, such as the main thread to add
See Strictmode document Strictmode.setthreadpolicy (New StrictMode.ThreadPolicy.Builder () . Detectdiskreads () . Detectdiskwrites () . Detectnetwork () //or. Detectall () for all detectable problems . Penaltylog () . Build ()); Strictmode.setvmpolicy (New StrictMode.VmPolicy.Builder () . Detectleakedsqlliteobjects () . Detectleakedclosableobjects () . Penaltylog (). Penaltydeath (). build ());
But
. Detectleakedclosableobjects () will error,
This means that this method is not used, but the view source found its method is public
/* * * Detect when an {@link java.io.Closeable} or other * object with a explict termination method is finalized
* without had been closed. * * <p>you always want to explicitly close such objects to * Avoid unnecessary resources leaks. */Public Builder detectleakedclosableobjects () { return enable (Detect_vm_closable_leaks); }
I'm a little confused about this, but the seniority is not yet. Ha ha.
So I just load the data in the second way, multi-threaded, asynchronous loading.
The code is as follows:
Send an empty message in the main function:
New Thread () { @Override public void Run () { //TODO auto-generated method Stub super.run (); url = Authutil.getauthorizationurl (); if (url = = null) { toast.maketext (webviewactivity.this, R.string.auth_url_empty,.). Show () ; Handler.sendemptymessage (0); } }. Start ();
The message is received in handler and processed accordingly.
Private Handler Handler = new Handler () {public void Handlemessage (Message msg) {switch (msg.what) {case 0:<span style= "White-space:pre" ></span>load (URL, webView); break;};};
this resolves the exception.
If there is anything wrong, please give me more advice.
October 16, 2014 14:15:58
In essence, in the current development of Android, the part about network access can not be put into the main thread!
Android.os.NetworkOnMainThreadException Exception (RPM)