我正在尝试使用Robotium创建一种测试方法,以检查单击按钮后Android应用程序是否完成(在代码中,finish()当用户点击该按钮时便有一个调用)。
public void test_onclickExit_finish() {
String buttonText = resources.getString(R.string.exit);
Button exitButton = solo.getButton(buttonText, true);
solo.clickOnView(exitButton);
// check here that the app has finished
// wait for the activity to finish?
assertTrue(solo.getCurrentActivity() == null);
}
Run Code Online (Sandbox Code Playgroud)
但是这个测试失败了。我不知道如何指示测试要等到活动结束。另外,我不确定使用getCurrentActivity()是否是检查应用程序是否完成的好方法。
如何检查申请/活动是否完成?
谢谢。
我有一个片段,我需要在屏幕上测量其视图的位置/宽度/高度,并传递给其他类.
所以我所拥有的是一个功能,它是这样的:
private void measureTest(){
v = ourView.findViewById(R.id.someTextField);
v.getLocationOnScreen(loc);
int w = v.getWidth();
...
SomeClass.passLocation(loc,w);
...
Run Code Online (Sandbox Code Playgroud)
问题是视图的位置/宽度/高度在片段生命周期内没有准备好.所以,如果我在这些生命周期方法中运行该函数:
onCreateView
onViewCreated
onStart
onResume
Run Code Online (Sandbox Code Playgroud)
我要么得到错误的位置和宽度/高度测量值或0值.我找到的唯一解决方案是GlobalLayoutListener向mainView 添加这样的解决方案
mainView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
public void onGlobalLayout() {
if(alreadyMeasured)
mainView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
else
measureTest();
}
});
Run Code Online (Sandbox Code Playgroud)
这可以完成工作..但它只是Yack!IMO.
有没有更好的方法呢?看起来像是一件基本的事情
android android-layout android-lifecycle android-fragments android-view
我正在尝试模拟Activity和Fragment重新创建,并且还检查onSaveInstancestate()并且onRestoreInstanceState()通常检查我是否在所有情况下都以良好的方式处理活动重新创建,例如,就像屏幕旋转导致活动重新创建它时一样自己。
但就我而言,我想检查更多可能导致重新创建的选项/案例,因为我的应用程序无法旋转(所有活动均为纵向)。
\n\n我看到了很多关于这个主题的文章、博客和 stackoverflow 问题/答案,例如这里、 这里和这里。
\n\n正如这个 stackoverflow 答案所说,为什么不总是使用 android:configChanges="keyboardHidden|orientation" ?\n 还有更多的事件可能会导致活动重新创建,因此在我读完它之后,我想测试我的应用程序中的一些事件。
\n\n例如,我在活动中按下主页按钮,然后进入设置并尝试更改语言、更改字体大小等...,但这些操作都没有使我的应用程序按照我的预期重新创建。\ n当我返回我的应用程序时,它只是恢复并onCreate()从未调用。\n所以我什至检查了有关此的官方文档。他们还说这应该导致我的活动重新创建:引用:
"When a configuration change occurs at runtime, the activity is shut down and restarted by default" \nRun Code Online (Sandbox Code Playgroud)\n\n但正如我所说,这并没有发生在我身上。
\n\n这对我来说很重要,因为我非常 na\xc3\xafve 并认为如果我的应用程序仅以纵向显示或者我是否将这一行添加到清单中:
\n\nandroid:configChanges="keyboardHidden|orientation|screenSize"\nRun Code Online (Sandbox Code Playgroud)\n\n那么一切都会好起来的,显然不是因为有更多的配置更改可以重新启动我的活动,所以我不能再运行它,我想以良好的方式处理它,现在我也想测试它。
\nandroid android-lifecycle android-fragments android-activity
我正在使用带有javascript界面的 WebView,有时当我loadUrl在webView上调用时,mWebView.getContext()返回null.
mWebView.post(new Runnable() { ...被执行吗?这段代码是否相关?
if (Looper.getMainLooper().getThread() == Thread.currentThread()) {
mWebView.loadUrl("javascript:...");
} else {
mWebView.post(new Runnable() {
public void run() {
mWebView.loadUrl("javascript:...");
}
});
}
Run Code Online (Sandbox Code Playgroud)我正在使用深层链接打开我的应用程序这是意图过滤器:
<intent-filter>
<data android:scheme="http" />
<data android:scheme="https" />
<data android:host="myapp.delivery" />
<data android:pathPattern="/.*" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
Run Code Online (Sandbox Code Playgroud)
我们的用例是:
(在步骤4之后)用户点击电子邮件应用程序(例如gmail或收件箱应用程序)上的URL,提示用户打开我的应用程序或浏览器,如下所示:
当用户点击打开我的应用程序时,会发生GotEmailActivity在gmail应用程序之上打开,我需要它在我之前打开的应用程序实例之上打开.
正如你在这里看到的那样,我的应用程序的2个实例是打开的(应用程序的第一个实例,第二个实例在gmail之上打开),:
android android-manifest android-intent android-lifecycle android-activity
我正在开发一个与 Unity3d 集成的应用程序,在我的代码的某些部分,我调用了一个 UnityPlayerActivity 来启动 Unity3d 代码。当用户完成使用 Unity3d 时,他按下一个按钮并返回到 Android 代码。我的问题是当我回到 android 代码时,在 UnityPlayerActivity 调用之后:
@Override protected void onDestroy ()
{
mUnityPlayer.quit();
super.onDestroy();
}
Run Code Online (Sandbox Code Playgroud)
应用程序关闭,不仅仅是Unity Activity。我不能调用这个方法,但我认为那会浪费内存。
有什么办法可以只退出 Unity3d 活动并保持我的应用程序正常运行,这样我就可以再次启动 Unity3d 部分吗?
塔克斯
我正在尝试将以下库添加到我的应用程序中
ext {
roomLibraryVersion = '1.1.0'
lifeCycleVersion = '1.1.1'
}
//room data base
implementation "android.arch.persistence.room:runtime:$roomLibraryVersion"
implementation "android.arch.persistence.room:compiler:$roomLibraryVersion"
annotationProcessor "android.arch.persistence.room:compiler:$roomLibraryVersion"
//life cycle
implementation "android.arch.lifecycle:runtime:$lifeCycleVersion"
implementation "android.arch.lifecycle:compiler:$lifeCycleVersion"
implementation "android.arch.lifecycle:extensions:$lifeCycleVersion"
// rx android
implementation 'io.reactivex.rxjava2:rxandroid:2.0.2'
implementation "io.reactivex.rxjava2:rxjava:2.1.14"
implementation 'android.arch.persistence.room:rxjava2:1.1.0'
//paging
implementation 'android.arch.paging:runtime:1.0.0'
Run Code Online (Sandbox Code Playgroud)
我在运行代码时遇到异常。任何人都可以帮我解决这个问题
Execution failed for task ':app:javaPreCompileDebug'.
> Annotation processors must be explicitly declared now. The following dependencies on the compile classpath are found to contain annotation processor. Please add them to the annotationProcessor configuration.
- compiler-1.1.1.jar (android.arch.lifecycle:compiler:1.1.1)
Alternatively, set android.defaultConfig.javaCompileOptions.annotationProcessorOptions.includeCompileClasspath = …Run Code Online (Sandbox Code Playgroud) 这是一个非常简单的问题:
我正在使用相对较新的Lifecycle 类(android 架构组件库的一部分)以Activity/Fragment更简单的方式处理一些事件。
这是你如何使用它来处理ON_DESTROY事件:
lifecycle.addObserver(object : LifecycleObserver {
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
fun onDestroy() {
lifecycle.removeObserver(this)
//Do something
}
})
Run Code Online (Sandbox Code Playgroud)
我无法在文档和此处找到,是应该调用removeObserver还是在ON_DESTROY事件时自动完成。
我试图阅读它,现在作为预防措施,我总是调用removeObserver.
它是安全的避免调用removeObserver时ON_DESTROY事件?
我正在尝试在干净的 android 应用程序中包含一个 CameraView 组件(来自 CameraX jetpack),如下所示:
https://medium.com/@hitherejoe/exploring-camerax-on-android-camera-view-daae6dfaa4ec https://developer.android.google.cn/jetpack/androidx/releases/camera
CameraView 非常简单,但将其绑定到 Activity 生命周期时出现问题。活动已授予相机权限。我试过在没有 CameraView 的情况下使用 CameraX(使用 SurfaceView + 所有设置),但以相同的结果和异常结束(并且 CameraView 使用起来要简单得多)。在三星 S10 和红米 Note 7 上测试过。 似乎组件尺寸/测量有问题:
例外
java.lang.BootstrapMethodError: Exception from call site #2 bootstrap method
at androidx.camera.core.Preview.updateListener(Preview.java:368)
at androidx.camera.core.Preview.updateOutput(Preview.java:586)
at androidx.camera.core.Preview.updateConfigAndOutput(Preview.java:363)
at androidx.camera.core.Preview.onSuggestedResolutionUpdated(Preview.java:524)
at androidx.camera.core.UseCase.updateSuggestedResolution(UseCase.java:408)
at androidx.camera.core.CameraX.calculateSuggestedResolutions(CameraX.java:684)
at androidx.camera.core.CameraX.bindToLifecycle(CameraX.java:195)
at androidx.camera.view.CameraXModule.bindToLifecycleAfterViewMeasured(CameraXModule.java:268)
at androidx.camera.view.CameraView.onMeasure(CameraView.java:364)
at android.view.View.measure(View.java:24973)
at androidx.constraintlayout.widget.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:1227)
at androidx.constraintlayout.widget.ConstraintLayout.onMeasure(ConstraintLayout.java:1572)
at android.view.View.measure(View.java:24973)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:7139)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at androidx.appcompat.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:143)
at android.view.View.measure(View.java:24973)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:7139)
at androidx.appcompat.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:403)
at android.view.View.measure(View.java:24973)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:7139)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185) …Run Code Online (Sandbox Code Playgroud) android android-lifecycle android-camera2 androidx android-camerax
目前,我用mLifecycleOwner = mContext as LifecycleOwnerget LifecycleOwner,它可以工作,但我认为它不是一个好的代码。
我怎样才能LifecycleOwner从ListAdapter?
class VoiceAdapters (private val aHomeViewModel: HomeViewModel, private val mPlay: PlayInterface):
ListAdapter<MVoice, VoiceAdapters.VoiceViewHolder>(MVoiceDiffCallback()) {
private lateinit var mContext: Context
private lateinit var mLifecycleOwner:LifecycleOwner
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VoiceViewHolder {
mContext = parent.context
mLifecycleOwner = mContext as LifecycleOwner
return VoiceViewHolder(
LayoutVoiceItemBinding.inflate(LayoutInflater.from(parent.context), parent, false).also {
it.lifecycleOwner = mLifecycleOwner
it.aHomeViewModel = aHomeViewModel
}
)
}
...
}
Run Code Online (Sandbox Code Playgroud) android ×10
android-view ×2
android-architecture-lifecycle ×1
androidx ×1
robotium ×1
testing ×1