TextView Null膨胀父布局后

TextView Null膨胀父布局后。 我正在运行一个循环,这膨胀了一些这些布局: for(i in ticketAr?.indices!!){ var inf1:LayoutInflater = LayoutInflater.from(context) var infv:View = inf1.inflate(R.layout.ticket_individual,null,false) infv.setId(parseInt(ticketAr[i].string(“ntb_id”))) var thetitle = infv.tck_event_title thetitle.setText(ticketAr[i].string(“age_group_desc”)) } 堆栈跟踪 10-25 09:30:35.839 11549-11549/com.example.xx.listview E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.xx.listview, PID: 11549 java.lang.NullPointerException: Attempt to invoke virtual method ‘void android.widget.TextView.setText(java.lang.CharSequence)’ on a null object reference at com.example.xx.listview.Ordering_Setup$getData$1.invoke(Ordering_Setup.kt:145) at com.example.xx.listview.Ordering_Setup$getData$1.invoke(Ordering_Setup.kt:29) at com.github.kittinunf.fuel.core.DeserializableKt$response$1.invoke(Deserializable.kt:37) at com.github.kittinunf.fuel.core.DeserializableKt$response$1.invoke(Deserializable.kt) at […]

Kotlin通用属性问题

在将我的Android项目从Java转换为Kotlin时,我遇到了Kotlin的一些问题。 假设我有接口I和扩展接口I的接口O. interface I{ } interface O: I{ } 通用类A具有扩展接口I的通用参数V,扩展类A的通用类B: abstract class A { } class B : A() { } 当我试图创建这样的属性: val returnB: A get() = b 我得到编译器错误“需要A,findB”。 在Java中,这将工作没有任何问题。 我怎样才能使用Kotlin访问? 我需要在我的应用程序中使用这种方法的基本类。 具有Navigator类的generics参数的BaseViewModel: abstract class BaseViewModel(application: Application, val repositoryProvider: RepositoryProvider) : AndroidViewModel(application) { var navigator: N? = null fun onDestroyView() { navigator = null } […]

如何在科特林无限和懒惰地循环列表?

我有一个directions列表,并希望find下一个方向,当我左转或右转。 这是我有的工作代码: enum class Turn { R, L } enum class Direction { N, E, S, W } val directionsInRightTurnOrder = listOf(Direction.N, Direction.E, Direction.S, Direction.W) private fun calculateNextHeading(heading: Direction, turn: Turn): Direction { val currentIndex = directionsInRightTurnOrder.indexOf(heading) var nextIndex = currentIndex + if (turn == Turn.R) 1 else -1 if (nextIndex >= directionsInRightTurnOrder.size) nextIndex = […]

改进API调用:如何确保在进行api调用后该值不为空?

我有下面的AuthenticationResponse模型,我使用retrofit2进行3个API调用。 如果我进行了verifyEmail调用,那么JSON响应主体只包含有效的电子邮件属性(如{“validEmail”:true})。 其他2个调用只包含“resetSuccesful”或其他4的属性。 我怎样才能确保/检查,当我收到verifyEmail调用的响应fe,它包含一个非空的validEmail值? 服务: interface AuthenticationService { @POST(“auth/checkEmail”) fun verifyEmail(@Body email: String): Call @POST(“auth/login”) fun login(@Body loginCredentials: LoginCredentials): Call @POST(“auth/resetPassword”) fun resetPassword(@Body email: String): Call } 模型: data class AuthenticationResponse( val validEmail: Boolean? = null, val loginSuccess: Boolean? = null, val retriesLeft: Int? = null, val authToken: String? = null, val accountBlocked: Boolean? = […]

删除Kotlin中另一个字符串中出现的字符

让我先说一下,我对Kotlin真的很陌生,但是对Python有点熟悉。 我的目标是通过某种function从一个字符串中删除所有出现的字符。 我可以告诉你如何在Python中执行此操作: def removechars (s, chars) return s.translate(None, chars) 我可以像这样使用它: print(removechars(“The quick brown fox jumped over the sleazy dog!”, “qot”)) 它会给这个输出: The uick brwn fx jumped ver the sleazy dg! 我怎么能在Kotlin类似?

如何使用Android Room Persistence Library将Column注释为NOT NULL

我的数据类看起来像这样 @Entity(tableName = “items”) data class Item( @ColumnInfo(name = “name”) var name: String = “”, @ColumnInfo(name = “room”) var room: String = “”, @ColumnInfo(name = “quantity”) var quantity: String = “”, @ColumnInfo(name = “description”) var description: String = “”, @PrimaryKey(autoGenerate = true) @ColumnInfo(name = “id”) var id: Long = 0 ) 房间使用SQLite,而SQLite在其数据库中支持NOT NULL列。 我尝试使用@NonNull注释列,但它没有效果。 有没有办法使房间数据库中的列不可空?

如何在回收视图中为自定义ViewHolder实现OnClickListener?

我正在创建一个应用程序,应该显示在recyclerview中的蓝牙设备,我希望用户能够单击项目执行操作。 现在我只是试图让吐司出现在点击后,但我想可能会显示一个对话框给对选择等,但我明显缺少的东西在我onclicklistener的用法。 我试图让我的ViewHolder类: DeviceHolder实现View.OnClickListener ,并将调用Toast.makeText()内我的onClick覆盖。 然而,没有任何事情发生。 我相信我只是错过了一些小事,并会感谢帮助find问题。 另外,我正在做的这kotlin,我是新来的,如果有可能更有效,kotlintypes的方式做到这一点,也将有所帮助。 我在下面张贴我的代码。 提前致谢。 class DeviceAdapter(val mContext : Context) : RecyclerView.Adapter(){ val mDevices = ArrayList() interface OnClickListener{ fun onClick(v: View) } fun updateItems(list: ArrayList){ mDevices.clear() mDevices.addAll(list) Log.d(TAG, “updating items : $mDevices”) notifyDataSetChanged() } fun ViewGroup.inflate(@LayoutRes res: Int, attachToRoot: Boolean = false): View{ return LayoutInflater.from(mContext).inflate(res, this, attachToRoot) } override fun […]

Kotlin:Java利用日期到数据绑定的字符串

我想通过Databinding在视图中使用我的Data类的Date值。 如果我使用日期字段上的toString()方法,它将起作用。 但是我想自定义日期值。 所以我用Method创建了Utils对象。 这是Util对象 object DateUtils { fun toSimpleString(date: Date) : String { val format = SimpleDateFormat(“dd/MM/yyy”) return format.format(date) } } 但是如果我想在这样的xml中使用这个方法 … android:text=”@{DateUtils.toSimpleString(journey.date)}” 我得到一个错误cannot find method toSimpleString(java.util.Date) in class … 这是我的Dataclass: data class Journey(var title: String, var date: Date?, var destination: String) 这个代码有什么问题?

预期的typesID资源

我正在使用ANKO建立简单的项目。 当我尝试为EditText设置id时,出现以下错误。 确保传递给API的资源ID是正确的types; 例如,调用Resources.getColor(R.string.name)是错误的。 import android.support.v7.app.AppCompatActivity import android.os.Bundle import org.jetbrains.anko.* class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) relativeLayout { editText{ id = 1 } } } }

如何获得IntelliJ IDEA代码完成作为Kotlin顶级函数导入静态Java方法?

在编辑Kotlin代码时,IntelliJ IDEA代码完成提示了静态Java方法,但是它导入了Java类而不是静态方法作为顶级函数(请参阅函数作用域 )。 例如键入“chrome”并按Enter将import org.openqa.selenium.remote.DesiredCapabilities并将“chrome”替换为“DesiredCapabilities.chrome()”,而不是添加import org.openqa.selenium.remote.DesiredCapabilities.chrome并将“铬“作为”铬“。 如何获得IntelliJ IDEA代码完成作为顶级函数导入静态Java方法?