所以我有这个秒表但是当我打开它的活动时,这就是我得到的:
FATAL EXCEPTION: main
Process: com.reecreate.woderator2, PID: 5869
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.reecreate.woderator2/com.reecreate.woderator2.Controller.StopwatchActivity}: kotlin.UninitializedPropertyAccessException: lateinit property textView has not been initialized
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: kotlin.UninitializedPropertyAccessException: lateinit property textView has not been initialized
at com.reecreate.woderator2.Controller.StopwatchActivity.onCreate(StopwatchActivity.kt:52)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
这是我的代码:
class StopwatchActivity : AppCompatActivity() {
lateinit var textView: TextView
lateinit var start: Button
lateinit var pause: Button
lateinit var reset: Button
lateinit var lap: Button
var MillisecondTime: Long = 0
var StartTime:Long = 0
var TimeBuff:Long = 0
var UpdateTime = 0L
lateinit var handler: Handler
var Seconds: Int = 0
var Minutes:Int = 0
var MilliSeconds:Int = 0
lateinit var listView: ListView
var ListElements = arrayOf<String>()
lateinit var ListElementsArrayList: MutableList<String>
lateinit var adapter: ArrayAdapter<String>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_stopwatch)
textView = textView as TextView
start = button as Button
pause = button2 as Button
reset = button3 as Button
lap = button4 as Button
listView = listview1 as ListView
handler = Handler()
ListElementsArrayList = ArrayList(Arrays.asList(*ListElements))
adapter = ArrayAdapter(this,
android.R.layout.simple_list_item_1,
ListElementsArrayList
)
listView.adapter = adapter
start.setOnClickListener {
StartTime = SystemClock.uptimeMillis()
handler.postDelayed(runnable, 0)
reset.setEnabled(false)
}
pause.setOnClickListener(View.OnClickListener {
TimeBuff += MillisecondTime
handler.removeCallbacks(runnable)
reset.setEnabled(true)
})
reset.setOnClickListener(View.OnClickListener {
MillisecondTime = 0L
StartTime = 0L
TimeBuff = 0L
UpdateTime = 0L
Seconds = 0
Minutes = 0
MilliSeconds = 0
textView.text = "00:00:00"
ListElementsArrayList.clear()
adapter.notifyDataSetChanged()
})
lap.setOnClickListener(View.OnClickListener {
ListElementsArrayList.add(textView.text.toString())
adapter.notifyDataSetChanged()
})
}
var runnable: Runnable = object : Runnable {
override fun run() {
MillisecondTime = SystemClock.uptimeMillis() - StartTime
UpdateTime = TimeBuff + MillisecondTime
Seconds = (UpdateTime / 1000).toInt()
Minutes = Seconds / 60
Seconds = Seconds % 60
MilliSeconds = (UpdateTime % 1000).toInt()
textView.text = ("" + Minutes + ":"
+ String.format("%02d", Seconds) + ":"
+ String.format("%03d", MilliSeconds))
handler.postDelayed(this, 0)
}
}
}
我试图确切地看到我的textView出现了什么问题,但我没有看到任何可能导致问题的内容。我把它交给私人,初始化和所有这些,但我不知道
非常感谢任何帮助:)
由于
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.reecreate.woderator2.Controller.StopwatchActivity"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="25dp">
<TextView
android:text="00:00:00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView"
android:textSize="50dp"
android:textStyle="bold"
android:textColor="#009688"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Button
android:text="Start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="41dp"
android:id="@+id/button" />
<Button
android:text="Pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button2"
android:layout_alignBaseline="@+id/button"
android:layout_alignBottom="@+id/button"
android:layout_centerHorizontal="true" />
<Button
android:text="Reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/button2"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="@+id/button3" />
<Button
android:text="Save Lap"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:id="@+id/button4"
android:layout_below="@+id/button"
android:layout_centerHorizontal="true" />
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/button4"
android:layout_centerHorizontal="true"
android:layout_marginTop="12dp"
android:id="@+id/listview1"/>
</RelativeLayout>
答案 0 :(得分:0)
您似乎正在使用Kotlin Android Extensions。在这种情况下,您不必为视图创建lateinit var
,您只需在XML文件中引用它们的ID即可。
所以不要这样:
class StopwatchActivity : AppCompatActivity() {
lateinit var textView: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_stopwatch)
textView = textView as TextView
textView.text = "00:00:00"
}
}
这样做:
class StopwatchActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_stopwatch)
textView.text = "00:00:00"
}
}
与你所有其他观点相同。
有关Kotlin Android Extensions以外的选项,请参阅this discussion。