1

I am trying to run the open source app in which i am getting the error as

lateinit property textInput has not been initialized

The main code where the textInput is used

class MainActivity : BaseActivity() {

    private val drawerToggle by lazy { ActionBarDrawerToggle(this, drawer_layout, drawer_open, drawer_close) }

    private val survivalContent by lazy { SurvivalContent(assets) }

    private lateinit var currentUrl: String
    private lateinit var currentTopicName: String
    private lateinit var textInput: MutableList<String>


    private var lastFontSize = State.getFontSize()
    private var lastNightMode = State.nightModeString()
    private var lastAllowSelect = State.allowSelect()

    private val linearLayoutManager by lazy { LinearLayoutManager(this) }

    private var isInEditMode by observable(false, onChange = { _, _, newMode ->
        if (newMode) {
            fab.setImageResource(drawable.ic_image_remove_red_eye)
            contentRecycler.adapter = EditingRecyclerAdapter(textInput)
        } else {
            fab.setImageResource(drawable.ic_editor_mode_edit)
            contentRecycler.adapter = MarkdownRecyclerAdapter(textInput, imageWidth(), onURLClick)
        }

        contentRecycler.scrollToPosition(State.lastScrollPos)
    })

Since i am new to android development i don't to how to initialize the lateinit variable before using it.Before this i tried to do the git submodule update as mentioned in this github but it didn't worked.so now i hope the problem is in initializing the textInput variable.

Yashwin.L
  • 19
  • 1
  • 9

2 Answers2

1

You will have to initialise textInput variable before using it. For this you can do something like this :

textInput = mutableListOf("Abc", "Xyz")

From docs : Accessing a lateinit property before it has been initialized throws a special exception that clearly identifies the property being accessed and the fact that it hasn't been initialized.

Read more: https://kotlinlang.org/docs/reference/properties.html#late-initialized-properties-and-variables

Shrey Garg
  • 1,317
  • 1
  • 7
  • 17
1

Do this :

 private  var currentUrl: String =""
 private  var currentTopicName: String =""
 private  var textInput= mutableListOf<String>()

You can do same thing with lateinit do you really don't need to user here. But if you wish to do then.

private lateinit var currentUrl: String
private lateinit var currentTopicName: String
private lateinit var textInput: MutableList<String>

In onCreate() method you have to just initialize these all variables.

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    currentUrl = String()
    currentTopicName = String()
    textInput= mutableListOf()
}

Let me know if any more help needed.

haresh
  • 1,424
  • 2
  • 12
  • 18
  • accept answer if it worked for you also ask any help if needed – haresh Jan 02 '20 at 03:43
  • the app ran without any problem but the content's inside the app is empty! i don't know way all can see is the blank space! is it because i took that app from open source or any other problem and here is the [github](https://github.com/ligi/SurvivalManual) for the app.please share me if you have any idea about this problem. – Yashwin.L Jan 02 '20 at 06:30