Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/211.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 无法解析getActivity()方法_Android - Fatal编程技术网

Android 无法解析getActivity()方法

Android 无法解析getActivity()方法,android,Android,我正在做esspresso测试时尝试截图 这是我的代码,我无法解析getActivity方法,我尝试过使用该方法,但也没有成功 package com.lifehealth.irma.test.Screenshotscripts; import android.app.Activity; import android.support.test.espresso.action.ViewActions; import android.support.test.espresso.matcher.Vie

我正在做esspresso测试时尝试截图 这是我的代码,我无法解析getActivity方法,我尝试过使用该方法,但也没有成功

package com.lifehealth.irma.test.Screenshotscripts;
import android.app.Activity;
import android.support.test.espresso.action.ViewActions;
import android.support.test.espresso.matcher.ViewMatchers;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.test.ActivityInstrumentationTestCase2;
import android.test.suitebuilder.annotation.LargeTest;

import com.lifehealth.MainActivity;
import com.lifehealth.irma.R;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;


import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.action.ViewActions.closeSoftKeyboard;
import static android.support.test.espresso.action.ViewActions.doubleClick;
import static android.support.test.espresso.action.ViewActions.swipeUp;
import static android.support.test.espresso.action.ViewActions.typeText;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;

//hamcrest

@RunWith(AndroidJUnit4.class)
@LargeTest

// Name of the activity + Test
public class Screencap {
    //Strings to be typed into tests declaration
    public static final String STRING_TO_BE_TYPED = "Test1233";
    public static final String loginID = "LIFEHEALTH";
    public static final String Device = "TestDevice";

    //Rule that tells the system which screen to start/boot up on this is telling the device to start from the main menu screen.
    @Rule
    public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(
            MainActivity.class);

    //@Test must be there to start a test if this is missing it will not run the test
    @Test
    public void Screencap1 () { /* passes*/
        //Enters login
        onView(withId(R.id.editOid)) /*editTextUserInput*/
                .perform(typeText(loginID), closeSoftKeyboard());
        // then click next
        onView(withId(R.id.btn_next)).perform(click());
        // "Clicks the settings
        onView(withId(R.id.btnSettings)).perform(click());
        //Clicks Device Settings.
        onView(withText("Device Settings")).perform(click());
        //Scroll up
        onView(withText("Configure IRMA Base")).perform(swipeUp());
        onView(withText("Configure IRMA Base")).perform(swipeUp());
        onView(withText("Configure IRMA Base")).perform(swipeUp());
        onView(withText("Configure IRMA Base")).perform(swipeUp());
        onView(withText("Configure IRMA Base")).perform(swipeUp());
        onView(withText("Configure IRMA Base")).perform(swipeUp());
        onView(withText("Barcode Reader Timeout")).perform(swipeUp());
        onView(withText("Barcode Reader Timeout")).perform(swipeUp());
        onView(withText("Barcode Reader Timeout")).perform(swipeUp());
        //language
        onView(withText("Language")).perform(doubleClick());
getActivity()方法提供活动的上下文。您可以使用ActivityName.this代替它


希望这将对您有所帮助。

getActivity
用于
片段
。您应该使用
getApplicationContext()
YourClassName。在本例中,这是

有人请帮忙!
        HelperClass.takeScreenshot("Whatever you want to call the file", getActivity());


//wait 1 min
        try {
            Thread.sleep(1 * 60 * 1000);
        } catch (InterruptedException e) {

        }
        /* Clicks back button */
        onView(withId(R.id.btn_back)).perform(click());
        //clicks main menu button
        onView(withText("Main Menu")).perform(click());

    }

}