Android的秒表应用程序崩溃

所以我有这个秒表,但是当我打开它的活动,这是我得到:

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> 

One Solution collect form web for “Android的秒表应用程序崩溃”

它看起来像你使用Kotlin Android扩展。 在这种情况下,您不必为视图创建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扩展以外的选项,请参阅此讨论 。

  • 可自定义parcelable对象
  • 匕首与Kotlin上的Mvp - Android
  • 在方法中使用泛型类型的接口
  • 我怎么能在Kotlin有一个复合键?
  • 无法将Kotlin Android扩展程序添加到我的项目中
  • Retrofit2 + Kotlin中的SimpleXML:MethodException:注释必须标记一个set或get方法
  • Koltin MyClass.class.java未解决的参考:java
  • 无法使Coverity Scan工作(Windows和Travis上的Java / Kotlin / Gradle 3.3)
  • NullPointerException在Kotlin中
  • 注释Kotlin中的属性时,注释的默认目标是什么?
  • Kotlin:创建地图时的条件项目
  • Kotlin language will be the best programming language for Android.