简体   繁体   中英

Why I get an exception when I try to start a new Intent?

I'm very new to Android and I get an exception like this:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.simplepay.hellomobile/com.simplepay.hellomobile.CardPayment}: android.os.NetworkOnMainThreadException

the stacktrace is here:

at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
        at android.app.ActivityThread.access$600(ActivityThread.java:141)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5039)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
        at dalvik.system.NativeStart.main(Native Method)
        Caused by: android.os.NetworkOnMainThreadException
        at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
        at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:163)
        at libcore.io.IoBridge.recvfrom(IoBridge.java:513)
        at java.net.PlainSocketImpl.read(PlainSocketImpl.java:488)
        at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
        at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
        at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:103)
        at org.apache.http.impl.io.SocketInputBuffer.isStale(SocketInputBuffer.java:109)
        at org.apache.http.impl.AbstractHttpClientConnection.isStale(AbstractHttpClientConnection.java:205)
        at org.apache.http.impl.conn.AbstractClientConnAdapter.isStale(AbstractClientConnAdapter.java:185)
        at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:336)
        at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
        at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:653)
        at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:627)
        at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:616)
        at juspay.services.WalletService.makeServiceCall(WalletService.java:283)
        at juspay.services.WalletService.getCardTokens(WalletService.java:237)
        at com.simplepay.hellomobile.CardPayment.addContentTo(CardPayment.java:107)
        at com.simplepay.hellomobile.CardPayment.setUpLayoutAndActionListeners(CardPayment.java:81)
        at com.simplepay.hellomobile.CardPayment.onCreate(CardPayment.java:56)
        at android.app.Activity.performCreate(Activity.java:5104)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)

Note that all the network calls are been done using AsyncTask and placed inside the doInBackground method. The code is :

protected JSONObject doInBackground(Void... voids)  
{
    JSONObject initOrderResponse = walletService.initOrder(this.amount);
    return initOrderResponse;
}

where initOrder is a normal java method and it has a line like:

responseBody = httpClient.execute(post,responseHandler);

But still I get this error.

Not sure where I'm making the mistake.

Thanks in advance.

Caused by: android.os.NetworkOnMainThreadException

You are trying to make a Network call on UI thread.

Hence, use an AsyncTask to accomplish such taks.

In Android 3.0 + versions, its made mandatory that any Network calls or actions that can potentially block the UI must be done on a background thread.

EDIT:

Try this in the onCreate()

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);

I have seen people executing AsyncTask like this. new MyAsyncTask().doInBackground(); instead of new MyAsyncTask().execute(); . Please make sure that you are not doing this mistake unintentionally.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM