我尝试在我的项目中执行Instrumentation Test,但是它有一个错误java.lang.IllegalArgumentException:您无法为破坏的活动启动加载
这是我的错误:
at dalvik.system.VMStack.getThreadStackTrace(Native Method)
at java.lang.Thread.getStackTrace(Thread.java:580)
at android.support.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:88)
at android.support.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:51)
at android.support.test.espresso.ViewInteraction.waitForAndHandleInteractionResults(ViewInteraction.java:314)
at android.support.test.espresso.ViewInteraction.check(ViewInteraction.java:297)
at com.example.riken.footballmatch.view.HomeActivityTest.testFootballMatchBehaviour(HomeActivityTest.kt:40)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:527)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at android.support.test.runner.AndroidJUnit4.run(AndroidJUnit4.java:101)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:56)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:384)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1853)
Caused by: junit.framework.AssertionFailedError: 'is displayed on the screen to the user' doesn't match the selected view.
Expected: is displayed on the screen to the user
Got: "AppCompatTextView{id=2131230965, res-name=tvTeam_home_detail, visibility=VISIBLE, width=0, height=46, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=android.widget.LinearLayout$LayoutParams@1c6b0fe, tag=null, root-is-layout-requested=false, has-input-connection=false, x=117.0, y=168.0, text=, input-type=0, ime-target=false, has-links=false}"
at android.support.test.espresso.matcher.ViewMatchers.assertThat(ViewMatchers.java:539)
at android.support.test.espresso.assertion.ViewAssertions$MatchesViewAssertion.check(ViewAssertions.java:103)
at android.support.test.espresso.ViewInteraction$SingleExecutionViewAssertion.check(ViewInteraction.java:415)
at android.support.test.espresso.ViewInteraction$2.call(ViewInteraction.java:279)
at android.support.test.espresso.ViewInteraction$2.call(ViewInteraction.java:265)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5283)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
java.lang.IllegalArgumentException: You cannot start a load for a destroyed activity
at com.bumptech.glide.manager.RequestManagerRetriever.assertNotDestroyed(RequestManagerRetriever.java:323)
at com.bumptech.glide.manager.RequestManagerRetriever.get(RequestManagerRetriever.java:132)
at com.bumptech.glide.Glide.with(Glide.java:723)
at com.example.riken.footballmatch.view.DetailActivity.showAwyLogo(DetailActivity.kt:108)
at com.example.riken.footballmatch.presenter.MatchDetailPresenter$getAwayLogo$1.invokeSuspend(MatchDetailPresenter.kt:40)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:32)
at kotlinx.coroutines.DispatchedTask$DefaultImpls.run(Dispatched.kt:235)
at kotlinx.coroutines.AbstractContinuation.run(AbstractContinuation.kt:19)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5283)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
Test running failed: Instrumentation run failed due to 'java.lang.IllegalArgumentException'
DetailActivity.kt
package com.example.riken.footballmatch.view
import android.database.sqlite.SQLiteConstraintException
import android.os.Bundle
import android.support.v4.content.ContextCompat
import android.support.v4.widget.SwipeRefreshLayout
import android.support.v7.app.AppCompatActivity
import android.view.Menu
import android.view.MenuItem
import android.widget.ProgressBar
import com.bumptech.glide.Glide
import com.example.riken.footballmatch.R
import com.example.riken.footballmatch.R.id.add_fav_fragment
import com.example.riken.footballmatch.api.ApiRepository
import com.example.riken.footballmatch.database.database
import com.example.riken.footballmatch.extension.gone
import com.example.riken.footballmatch.extension.visible
import com.example.riken.footballmatch.model.FavoriteItem
import com.example.riken.footballmatch.presenter.MatchDetailPresenter
import com.example.riken.footballmatch.model.Team
import com.example.riken.footballmatch.team.detmatch.MatcDetView
import com.example.riken.footballmatch.model.MatchActivity
import com.google.gson.Gson
import kotlinx.android.synthetic.main.activity_detail.*
import org.jetbrains.anko.db.classParser
import org.jetbrains.anko.db.delete
import org.jetbrains.anko.db.insert
import org.jetbrains.anko.db.select
import org.jetbrains.anko.design.snackbar
class DetailActivity : AppCompatActivity(), MatcDetView {
private var matchdet: MutableList<MatchActivity> = mutableListOf()
private var idEvent: String? = ""
private var idTeamHome: String? = ""
private var idTeamAway: String? = ""
private lateinit var matchpresenter: MatchDetailPresenter
private lateinit var progresbar: ProgressBar
private lateinit var swipeRefresh: SwipeRefreshLayout
private var menuItem: Menu? = null
private var isFavorite: Boolean = false
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_detail)
swipeRefresh = swipe_refresh_detail
val intent = intent
idEvent = intent.getStringExtra("id")
idTeamHome = intent.getStringExtra("idHomeTeam")
idTeamAway = intent.getStringExtra("idAwayTeam")
progresbar = progresar_detail
loadingShow()
supportActionBar?.title = "Detail Match"
this.supportActionBar?.setDisplayHomeAsUpEnabled(true)
favoriteState()
val apiRequest = ApiRepository ()
val gson = Gson ()
matchpresenter = MatchDetailPresenter(this, apiRequest, gson)
matchpresenter.getAwayLogo(idTeamAway)
matchpresenter.getHomeLogo(idTeamHome)
matchpresenter.getDetailMatch(idEvent)
}
override fun matchListShow(data: List<MatchActivity>) {
matchdet.clear()
matchdet.addAll(data)
//detailmatch
tvdetail_match_date.text = matchdet[0].dateEvent
// tvdetail_match_date.text = matchdet.get(0).dateEvent
// ?.let { DateHelper.formatDateToMatch(it) }
//detailhome
tvTeam_home_detail.text = matchdet[0].teamHome
tvScore_home_detail.text = matchdet[0].scoreHome
tvHome_goal_detail.text = matchdet[0].homeGoalDetail
tvHome_shot_detail.text = matchdet[0].homeShot
tvHome_gk.text = matchdet[0].homeKeeper
tvHome_deffense.text = matchdet[0].homeDefense
tvHome_mf.text = matchdet[0].homeMidfield
tvHome_fw.text = matchdet[0].homeForward
tvHome_sub.text = matchdet[0].homeSubstitute
//detailAway
tvTeam_away_detail.text = matchdet[0].teamAway
tvScore_away_detail.text = matchdet[0].scoreAway
tvAway_goal_detail.text = matchdet[0].awayGoalDetail
tvAway_shot_detail.text = matchdet[0].awayShot
tvAway_gk.text = matchdet[0].awayKeeper
tvAway_deffense.text = matchdet[0].awayDefense
tvAway_mf.text = matchdet[0].awayMidfield
tvAway_fw.text = matchdet[0].awayForward
tvAway_sub.text = matchdet[0].awaySubstitute
loadingHide()
}
override fun showAwyLogo(data: List<Team>) {
Glide.with(this).load(data[0].team).into(ivAway_detail)
ivAway_detail.visible()
}
override fun showHomeLogo(data: List<Team>) {
Glide.with(this).load(data[0].team).into(ivHome_detail)
ivHome_detail.visible()
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.favorite_match, menu)
menuItem = menu
setFavorite()
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
android.R.id.home -> {
finish()
true
}
add_fav_fragment -> {
if (isFavorite) removeFromFavorite() else addToFavorite()
isFavorite = !isFavorite
setFavorite()
true
}
else -> super.onOptionsItemSelected(item)
}
}
private fun favoriteState() {
database.use {
val result = select(FavoriteItem.TABLE_MATCH)
.whereArgs("(MATCH_ID = {id})",
"id" to idEvent as Any
)
val favorite = result.parseList(classParser<FavoriteItem>())
if (!favorite.isEmpty()) isFavorite = true
}
}
override fun loadingShow() {
progresbar.visible()
}
override fun loadingHide() {
progresbar.gone()
}
private fun addToFavorite() {
try {
database.use {
insert(
FavoriteItem.TABLE_MATCH,
FavoriteItem.MATCH_ID to matchdet[0].idEvent,
FavoriteItem.MATCH_DATE to matchdet[0].dateEvent,
FavoriteItem.HOME_TEAM to matchdet[0].teamHome,
FavoriteItem.HOME_SCORE to matchdet[0].scoreHome,
FavoriteItem.AWAY_TEAM to matchdet[0].teamAway,
FavoriteItem.AWAY_SCORE to matchdet[0].scoreAway,
FavoriteItem.HOME_ID to matchdet[0].idHomeTeam,
FavoriteItem.AWAY_ID to matchdet[0].idAwayTeam,
FavoriteItem.HOME_GOAL to matchdet[0].homeGoalDetail,
FavoriteItem.AWAY_GOAL to matchdet[0].awayGoalDetail,
FavoriteItem.HOME_SHOTS to matchdet[0].homeShot,
FavoriteItem.AWAY_SHOTS to matchdet[0].awayShot,
FavoriteItem.HOME_GK to matchdet[0].homeKeeper,
FavoriteItem.AWAY_GK to matchdet[0].awayKeeper,
FavoriteItem.HOME_FW to matchdet[0].homeForward,
FavoriteItem.AWAY_FW to matchdet[0].awayForward,
FavoriteItem.HOME_MF to matchdet[0].homeMidfield,
FavoriteItem.AWAY_MF to matchdet[0].awayMidfield,
FavoriteItem.HOME_DF to matchdet[0].homeDefense,
FavoriteItem.AWAY_DF to matchdet[0].awayDefense,
FavoriteItem.HOME_SUB to matchdet[0].homeSubstitute,
FavoriteItem.AWAY_SUB to matchdet[0].awaySubstitute)
}
swipeRefresh.snackbar("Added to favorite").show()
} catch (e: SQLiteConstraintException){
swipeRefresh.snackbar(e.localizedMessage).show()
}
}
private fun removeFromFavorite(){
try {
database.use {
delete(
FavoriteItem.TABLE_MATCH, "(MATCH_ID = {id})",
"id" to matchdet.get(0).idEvent )
}
swipeRefresh.snackbar("Removed from favorite").show()
} catch (e: SQLiteConstraintException){
swipeRefresh.snackbar(e.localizedMessage).show()
}
}
private fun setFavorite() {
if (isFavorite)
menuItem?.getItem(0)?.icon = ContextCompat.getDrawable(this, R.drawable.fav_white)
else
menuItem?.getItem(0)?.icon = ContextCompat.getDrawable(this, R.drawable.fav_white_border)
}
}
MatchDetailPresenter:
package com.example.riken.footballmatch.presenter
import com.example.riken.footballmatch.api.ApiRepository
import com.example.riken.footballmatch.api.TheSportDBApi
import com.example.riken.footballmatch.model.MatchResponse
import com.example.riken.footballmatch.model.TeamResponse
import com.example.riken.footballmatch.team.detmatch.MatcDetView
import com.google.gson.Gson
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import org.jetbrains.anko.doAsync
import org.jetbrains.anko.uiThread
class MatchDetailPresenter (
private val matchDetailView: MatcDetView,
private val apiRepository: ApiRepository,
private val gson: Gson) {
fun getDetailMatch (idEvent: String?) {
matchDetailView.loadingShow()
GlobalScope.launch (Dispatchers.Main){
val data = gson.fromJson(apiRepository.doRequest(TheSportDBApi.getMatchDetail(idEvent)).await(),
MatchResponse::class.java)
matchDetailView.loadingHide()
matchDetailView.matchListShow(data.events)
}
}
fun getAwayLogo (idTeam: String?) {
matchDetailView.loadingShow()
GlobalScope.launch (Dispatchers.Main){
val data = gson.fromJson(apiRepository.doRequest(TheSportDBApi.getTeamLogo(idTeam)).await(),
TeamResponse::class.java)
matchDetailView.loadingHide()
matchDetailView.showAwyLogo(data.teams)
}
}
fun getHomeLogo (idTeam: String?) {
matchDetailView.loadingShow()
GlobalScope.launch(Dispatchers.Main) {
val data = gson.fromJson(
apiRepository.doRequest(TheSportDBApi.getTeamLogo(idTeam)).await(),
TeamResponse::class.java
)
matchDetailView.loadingHide()
matchDetailView.showHomeLogo(data.teams)
}
}
}
HomeActivityTest:
package com.example.riken.footballmatch.view
import android.support.test.espresso.Espresso.onView
import android.support.test.espresso.action.ViewActions
import android.support.test.espresso.assertion.ViewAssertions.matches
import android.support.test.espresso.contrib.RecyclerViewActions
import android.support.test.espresso.matcher.ViewMatchers.isDisplayed
import android.support.test.espresso.matcher.ViewMatchers.withId
import android.support.test.rule.ActivityTestRule
import android.support.test.runner.AndroidJUnit4
import android.support.v7.widget.RecyclerView
import com.example.riken.footballmatch.R.id.*
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
class HomeActivityTest {
@Rule
@JvmField var activityRule = ActivityTestRule(HomeActivity::class.java)
@Test
fun testFootballMatchBehaviour() {
delay()
onView(withId(recycle_first)).
check(matches(isDisplayed()))
onView(withId(recycle_first))
.perform(RecyclerViewActions.scrollToPosition<RecyclerView.ViewHolder>(7))
onView(withId(recycle_first))
.perform(RecyclerViewActions.actionOnItemAtPosition<RecyclerView.ViewHolder>(7, ViewActions.click()))
onView(withId(tvdetail_match_date))
.check(matches(isDisplayed()))
onView(withId(ivHome_detail))
.check(matches(isDisplayed()))
onView(withId(tvTeam_home_detail))
.check(matches(isDisplayed()))
onView(withId(tvScore_home_detail))
.check(matches(isDisplayed()))
onView(withId(ivAway_detail)).
check(matches(isDisplayed()))
onView(withId(tvTeam_away_detail))
.check(matches(isDisplayed()))
onView(withId(tvScore_away_detail))
.check(matches(isDisplayed()))
onView(withId(add_fav_fragment))
.perform(ViewActions.click())
ViewActions.pressBack()
}
private fun delay(){
try {
Thread.sleep(3000)
}catch (e : InterruptedException){
e.printStackTrace()
}
}
}
DetailActivity行108:Glide.with(this).load(data [0] .team).into(ivAway_detail) MatchDetailActivity第40行:matchDetailView.showAwyLogo(data.teams) HomeActivityTest第40行:onView(withId(tvTeam_home_detail))。check(matches(isDisplayed()))
答案 0 :(得分:1)
该问题已被提出,请参阅:You cannot start a load for a destroyed activity in relativelayout image using glide
最后,不要在您的活动中使用“ 此 ”
“ Glide.with( this )。load(data [0] .team).into(ivHome_detail) ivHome_detail.visible()“
将“ this”更改为“ getApplicationContext”,如下所示:
“ Glide.with( getApplicationContext() )。load(data [0] .team).into(ivHome_detail) ivHome_detail.visible()“