Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Starting an Activity in onPostExecute

I am having an issue when trying to start an intent when an AsyncTask is finished on the onPostExecute method. The first time I call the AsyncTask is in the splash screen activity.

public class SplashScreen extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

       startJsonGet();
    }

    public void startJsonGet() {
        JsonGet getData = new JsonGet(mBuildings, SplashScreen.this);
        getData.execute();

    }
}

This calls the JsonGet class

public class JsonGet extends AsyncTask<Void, Void, Void> {

    Context context;

    JsonGet(ArrayList<Building> mBuildings, Context context){
        super();
        this.mBuildings = mBuildings;
        this.context = context;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // before making http calls

    }

    @Override
    protected Void doInBackground(Void... arg0) {
        JsonParser jsonParser = new JsonParser();
        String json = jsonParser
                .getJSONFromUrl("");

        Log.e("Response: ", "> " + json);


        if (json != null) {
            try {

               //Does stuff
                }

            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        Intent i=new Intent(context, MainActivity.class);
        context.startActivity(i);

    }

}

The JSON is parsing correctly, and the log shows this. But when I attempt to call an intent in the post execute it crashes. I need to call this JsonGet function from a different class. (Ex: Update the Json data when the user clicks a refresh button.) So calling the intent from the splash screen class will not work.

Here is the log that I get

    10-25 16:03:12.156    8481-8481/com.andrewcode.broncomaps E/eglCodecCommon﹕ writeFully: failed: Broken pipe
10-25 16:03:12.156    8481-8481/com.andrewcode.broncomaps E/EGL_emulation﹕ tid 8481: eglChooseConfig(576): error 0x3001 (EGL_NOT_INITIALIZED)
10-25 16:03:12.164    8481-8481/com.andrewcode.broncomaps E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.IllegalArgumentException: eglChooseConfig failed EGL_NOT_INITIALIZED
            at android.view.HardwareRenderer$GlRenderer.chooseEglConfig(HardwareRenderer.java:893)
            at android.view.HardwareRenderer$GlRenderer.initializeEgl(HardwareRenderer.java:845)
            at android.view.HardwareRenderer$GlRenderer.initialize(HardwareRenderer.java:786)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1502)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
            at android.view.Choreographer.doCallbacks(Choreographer.java:562)
            at android.view.Choreographer.doFrame(Choreographer.java:532)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
            at android.os.Handler.handleCallback(Handler.java:725)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5041)
            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)
like image 463
Andrew Gable Avatar asked Oct 25 '13 22:10

Andrew Gable


1 Answers

I don't see any problem with asynctask. Problem seems to be with your emulator. Try to restart it. Maybe this link helps you: EGL_emulation failed to establish connection to host - android

like image 128
Devrim Avatar answered Nov 20 '22 12:11

Devrim