Skip to content Skip to sidebar Skip to footer

Getactivity() Call Causes Runtimeexception: Could Not Launch Intent Intent Act=android.intent.action.main

Updated #1: more info added to the end of this post I'm new to Android development and testing. I have three Espresso tests. First test passes, but the second one will not run beca

Solution 1:

I found a workaround. I added code that hits back button multiple times in the tearDown() method to close all previously opened activities. Unfortunately I didn't found another method how to close all opened activities in Espresso. Robotium has a very convenient method for that purpose solo.finishOpenedActivities().

publicvoidtearDown()throws Exception {
    Log.d(TAG, "TEARDOWN");

    goBackN();

    super.tearDown();
}

privatevoidgoBackN() {
    finalintN=10; // how many times to hit back buttontry {
        for (inti=0; i < N; i++)
            Espresso.pressBack();
    } catch (com.google.android.apps.common.testing.ui.espresso.NoActivityResumedException e) {
        Log.e(TAG, "Closed all activities", e);
    }
}

Solution 2:

pressBack solution does not worked for me but I found another:

@OverrideprotectedvoidtearDown()throws Exception {
    closeAllActivities(getInstrumentation());
    super.tearDown();
}

publicstaticvoidcloseAllActivities(Instrumentation instrumentation)throws Exception {
    finalintNUMBER_OF_RETRIES=100;
    inti=0;
    while (closeActivity(instrumentation)) {
        if (i++ > NUMBER_OF_RETRIES) {
            thrownewAssertionError("Limit of retries excesses");
        }
        Thread.sleep(200);
    }
}

publicstatic <X> X callOnMainSync(Instrumentation instrumentation, final Callable<X> callable)throws Exception {
    final AtomicReference<X> retAtomic = newAtomicReference<>();
    final AtomicReference<Throwable> exceptionAtomic = newAtomicReference<>();
    instrumentation.runOnMainSync(newRunnable() {
        @Overridepublicvoidrun() {
            try {
                retAtomic.set(callable.call());
            } catch (Throwable e) {
                exceptionAtomic.set(e);
            }
        }
    });
    finalThrowableexception= exceptionAtomic.get();
    if (exception != null) {
        Throwables.propagateIfInstanceOf(exception, Exception.class);
        Throwables.propagate(exception);
    }
    return retAtomic.get();
}

publicstatic Set<Activity> getActivitiesInStages(Stage... stages) {
    final Set<Activity> activities = Sets.newHashSet();
    finalActivityLifecycleMonitorinstance= ActivityLifecycleMonitorRegistry.getInstance();
    for (Stage stage : stages) {
        final Collection<Activity> activitiesInStage = instance.getActivitiesInStage(stage);
        if (activitiesInStage != null) {
            activities.addAll(activitiesInStage);
        }
    }
    return activities;
}

privatestaticbooleancloseActivity(Instrumentation instrumentation)throws Exception {
    finalBooleanactivityClosed= callOnMainSync(instrumentation, newCallable<Boolean>() {
        @Overridepublic Boolean call()throws Exception {
            final Set<Activity> activities = getActivitiesInStages(Stage.RESUMED,
                    Stage.STARTED, Stage.PAUSED, Stage.STOPPED, Stage.CREATED);
            activities.removeAll(getActivitiesInStages(Stage.DESTROYED));
            if (activities.size() > 0) {
                finalActivityactivity= activities.iterator().next();
                activity.finish();
                returntrue;
            } else {
                returnfalse;
            }
        }
    });
    if (activityClosed) {
        instrumentation.waitForIdleSync();
    }
    return activityClosed;
}

Solution 3:

This seems to work for me to close all of the activities in the stack except the first one.

@After@OverridepublicvoidtearDown()throws Exception
   {
      Intentintent=newIntent(getActivity(), getActivity().getClass());
      intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // Removes other Activities from stack
      intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
      myActivity.startActivity(intent);
      super.tearDown();

   }

Solution 4:

Try the following:

@BeforepublicvoidsetUp()throws Exception {
    super.setUp();
    Log.d(TAG, "SETUP");
    activity = getActivity();
}

Post a Comment for "Getactivity() Call Causes Runtimeexception: Could Not Launch Intent Intent Act=android.intent.action.main"