android annotations @background NetworkOnMainThreadException

柔情痞子 提交于 2019-12-13 00:39:54

问题


Why I get this error? Shouldn't android annotations take care of threading? Any help will be appreciated :). Thank you

@EActivity(R.layout.activity_main)
public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        tradingHistory();

    }
    void myMethod() {
        tradingHistory();
    }
    @Background
    void tradingHistory (){
        Helper helper = new Helper();
        try {

            helper.TradeHistory tradeHistory = helper.getTradeHistory();
            System.out.println(tradeHistory.toString());
        } catch (Helper.HelperException e) {
            e.printStackTrace();
        }
    }



}

回答1:


You probably use wrong Activity in your Manifest - You should use MainActivity_ instead of MainActivity




回答2:


if you're just doing some development you can turn it off using:

import android.os.StrictMode;

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

but I'd recommend to dug into Android AsyncTask some more.



来源:https://stackoverflow.com/questions/28862525/android-annotations-background-networkonmainthreadexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!