Android Problem Summary (Android.os.NetworkOnMainThreadException exception)

Source: Internet
Author: User

Problem Description:

Android.os.NetworkOnMainThreadException exception (do not access network resources on the main thread)

Problem Analysis:

Careful study of The reason for the error is that the code does not conform to the Android specification ), online search found: android3.0 version started ( specifically is not starting from this version, do not delve into the ) on the mandatory program can not access the network in the main thread, to put the access network in a separate thread.

How to resolve:

1, If you want to ignore these mandatory policy issues, you can add in the OnCreate () method

Strictmode.setthreadpolicy (New StrictMode.ThreadPolicy.Builder ()

. Detectdiskreads (). Detectdiskwrites (). Detectnetwork (). Penaltylog (). build ());

Strictmode.setvmpolicy (New StrictMode.VmPolicy.Builder ()

. Detectleakedsqlliteobjects (). Detectleakedclosableobjects ()

. Penaltylog (). Penaltydeath (). build ());

2, put in a separate thread: (I use the click button to view the network pictures, learning)

Private Bitmap Bitmap = null;//global variable (if I put it in a local variable, I will be prompted: The immutable local variable cannot be assigned, and the lazy is put into the global variable )

Button Listener:

private final class buttonlistenerevent implements onclicklistener{@Overridepublic  void onclick (VIEW V)  {    final String path =  Imagepath.gettext (). toString ();    final handler handler = new  Handler () {    public void handlemessage (message msg) {         switch  (msg.what)  {         Case 0:            imageview.setimagebitmap ( Bitmap);            break;         default:            break;         }    }};new thread () {     public void&nBsp;run () {    try {        byte[] data  = imageservice.getimage (Path);        //  to set the bitmap for the image          bitmap = bitmapfactory.decodebytearray (data, 0,         data.length);         // Send an empty message after processing         handler.sendemptymessage (0);     } catch  (exception e)  {             // TODO Auto-generated catch block             e.printstacktrace ();             toast.maketext (Getapplicationcontext (),  r.string.error, 1)           &nBsp;  .show ();            }         }    }.start ();     }}


I am new to Android, if there is a wrong place also ask you a lot of guidance ~


Android Problem Summary (Android.os.NetworkOnMainThreadException exception)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.