我想将应用程序上下文注入存储库类以在存储库内创建房间数据库实例。我正在使用 Hilt 进行依赖注入。
我们可以使用 hilt 传递应用程序上下文还是必须手动传递它?
我收到以下错误:
[Dagger/MissingBinding] android.content.Context cannot be provided without an
@Provides-annotated method. public abstract static class ApplicationC
implements ExartApplication_GeneratedInjector
Run Code Online (Sandbox Code Playgroud)
我以前有下面的代码工作,现在编译器停止并标记两个if
语句并说:
如果用作表达式,'if' 必须同时具有 main 和 'else' 分支
但是正如您所看到的,这不是一个表达式,而只是一个简单的等式语句和旁边的条件语句。
try {
val json_string = responseBody!!.string()
val jsonObject = JSONObject(json_string)
if (jsonObject.has("version")) {
val remoteVersion = jsonObject.getInt("version")
if (remoteVersion > BuildConfig.VERSION_CODE) {
handler.post {
showInstallNewVersionDialog()
}
}
}
} catch (e: Exception) {
e.message?.let { Log.e(Constants.TAG, e.message!!) }
}
Run Code Online (Sandbox Code Playgroud)
有趣的是,如果我添加了空else
标签,它会运行但会警告删除空else
语句:
if (jsonObject.has("version")) {
val remoteVersion = jsonObject.getInt("version")
if (remoteVersion > BuildConfig.VERSION_CODE) {
handler.post {
showInstallNewVersionDialog()
}
} else {}
} else {}
Run Code Online (Sandbox Code Playgroud) 我有一个下面的数据框
id action
================
10 CREATED
10 111
10 222
10 333
10 DONE
10 222
10 UPDATED
777 CREATED
10 333
10 DONE
Run Code Online (Sandbox Code Playgroud)
我想创建一个新列“检查”,该列将基于数据框中前一行中的数据:
输出:
id action check
================
10 CREATED
10 111
10 222
10 333
10 DONE C
10 222
10 UPDATED
777 CREATED
10 333
10 DONE U
Run Code Online (Sandbox Code Playgroud)
我尝试使用多个 if 条件,但它对我不起作用。你能帮忙吗?
我将 IntelliJ 与混合 Java/Kotlin 项目一起使用。在我的 Kotlin 文件之一中,我有以下属性:
override val value: String
get() {
return webElement.getAttribute("value")
}
Run Code Online (Sandbox Code Playgroud)
IntelliJ 的灯泡提供“将属性 getter 转换为初始值设定项”,这会将代码更改为:
override val value: String = webElement.getAttribute("value")
Run Code Online (Sandbox Code Playgroud)
对我来说,这似乎不是一个简单的重构,而是一个重大的代码更改。我认为正在发生的事情是:
在第一个版本中,value
当我调用 时会检索该属性value
。
在更改后的版本中,该value
属性在构造类实例时立即设置,然后对该类实例永远不会更改。
但也许这更像是 C# 表达式主体成员,它使用 lambda 箭头=>
而不是大括号,return
但在其他方面的工作方式完全相同。
那么……是哪个?第二版代码何时初始化?
代码 A 是Context
、Fragment
和的扩展函数Activity
。
我认为这是多余的,我该如何优化它?
代码A
fun Context.toast(msg: String){
Toast.makeText(this, msg, Toast.LENGTH_LONG).show()
}
fun Context.toast(@StringRes resId: Int){
toast(getString(resId))
}
fun Context.toast(@StringRes resId: Int,msg: String){
toast(getString(resId) + msg)
}
fun Context.toast(msg: String,@StringRes resId: Int){
toast(msg + getString(resId))
}
//------------------------------------------------
fun Fragment.toast(msg:String) {
requireContext().toast(msg)
}
fun Fragment.toast(@StringRes resId: Int) {
toast(requireContext().getString(resId))
}
fun Fragment.toast(@StringRes resId: Int, msg: String) {
toast(requireContext().getString(resId) + msg)
}
fun Fragment.toast( msg: String, @StringRes resId: Int) {
toast(msg+ requireContext().getString(resId))
}
//------------------------------------------------
fun Activity.toast(msg: …
Run Code Online (Sandbox Code Playgroud) 我无法在 Android Studio 上打开终端,错误显示:
Cannot open Local Terminal Failed to start [/bin/bash, --rcfile, /Applications/Android Studio.app/Contents/plugins/terminal/jediterm-bash.in, -i] in /Users/{UserName}/Android
See your idea.log (Help | Show Log in Finder) for the details.
Run Code Online (Sandbox Code Playgroud)
如果您有任何想法,请告诉我。
提前致谢。
所以我是 Kotlin 的新手,我正在尝试制作一个超级简单的应用程序。它所做的只是当我单击右按钮时,它与左按钮一样向右。问题是当我单击任一按钮(例如右键)时,我可以单击它直到图像完全脱离屏幕。那么我如何实现一个代码,一旦它碰到屏幕边缘就停止移动?
我的代码
package com.example.change_position_circle
import android.animation.ObjectAnimator
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//val picture = findViewById<ImageView>(R.id.SpongeBob)
val right_button = findViewById<Button>(R.id.right)
val left_button = findViewById<Button>(R.id.left)
right_button.setOnClickListener()
{
//ObjectAnimator.ofFloat(SpongeBob, "x", 100)
SpongeBob.animate().setDuration(90).translationXBy(100f)
}
left_button.setOnClickListener()
{
//ObjectAnimator.ofFloat(SpongeBob, "translationXBy", 100f).apply {
//duration = 200
// start()
SpongeBob.animate().setDuration(90).translationXBy(-100f)
//}
}
}
}
Run Code Online (Sandbox Code Playgroud)
感谢您的帮助
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner line = new Scanner(System.in);
int counter = 1;
while (line.hasNextLine()) {
String line = line.nextLine();
System.out.println(counter + " " + line);
counter++;
}
}
}
Run Code Online (Sandbox Code Playgroud)
任务:每一行将包含一个非空字符串。读取直到EOF。对于每一行,打印行号,后跟一个空格和行内容。
输入示例:
Hello world
I am a file
Read me until end-of-file.
Run Code Online (Sandbox Code Playgroud)
示例输出:
1 Hello world
2 I am a file
3 Read me until end-of-file.
Run Code Online (Sandbox Code Playgroud)