-1

On clicking the button, a new activity opens. I am trying to addView to new activity from ActivityMain.kt. I know that this would be easy if I use designer to addView, but I don't know how many views to Add. Therefor I have to do this programmatically.

Here is my activity_numbers code that is another activity which opens when I click on the button.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/activity_numbers_root_view"
    tools:context=".NumbersActiviy">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/numbers_text"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Here is the code in MainActivity.kt file

numbersText.setOnClickListener {
            val intent: Intent = Intent(this, NumbersActiviy::class.java)
            startActivity(intent)

//            Commenting the following code, the application works just fine.
            val numbersRootView:ConstraintLayout = findViewById(R.id.activity_numbers_root_view)
            val textView = TextView(numbersRootView.context)
            textView.text = "This is from " + wordsList.get(0)
            numbersRootView.addView(textView)

        }

Here is the error I get: Image of the error

I have also tried putting the code outisde the onClickListener but not worked. I have also tried using import kotlinx.android.synthetic.main.activity_numbers.*

  • findViewById works in layout file which is specified in setContentView in onCreate() method of activity. – Firoz Jaroli Sep 22 '20 at 11:54
  • Which is the layout used in the MainActivity? – Gabriele Mariotti Sep 22 '20 at 11:56
  • if you need to show records in the next activity and your values are Strings, why not use a recyclerview in the next activity? that way you can pas an array of all your Strings to the recyclerview adapter in the next page and it will take care of making the records and showing them. – MehranB Sep 22 '20 at 11:57

2 Answers2

1

startActivity will launch a new Activity but your findViewById is being executed at the "old" activity. The error you have is a NullPointerException caused by your cast to a non nullable ConstraintLayout

If you want to update the new Activity values, you'll need to your code in the onCreate method of the new Activity. Something like

class MainActivity {
  ...
  numbersText.setOnClickListener {
    val intent: Intent = Intent(this, NumbersActiviy::class.java)
    /* add extras to intent */ 
    startActivity(intent)
  }
  ...
}

class NumbersActivity {
  fun onCreate(...) {
    val numbersRootView:ConstraintLayout = findViewById(R.id.activity_numbers_root_view)
    val textView = TextView(numbersRootView.context)
    textView.text = "This is from " + /* get extra from intent? */
    numbersRootView.addView(textView)
  }
}

Beware that you'll need to pass extras in your intent. Check this question

I don't remember the exact syntax, it's been a while since I was coding for Android

Alberto S.
  • 7,409
  • 6
  • 27
  • 46
  • You are saying that put the code which is causing error outside onClickListener. I have tried. By doing so, app doesn't run. – Tanzeel Ahmed Sep 22 '20 at 12:16
  • @Pelocho is saying your ``onClick`` code should just launch ``NumbersActivity``, and that's all. Then ``NumbersActivity`` can inflate the ``activity_numbers.xml`` layout file as usual, and that layout is where the view with the ID you're searching for is. ``MainActivity`` can't see it – cactustictacs Sep 22 '20 at 12:31
  • Exactly, I added some code for reference. Beware that you won't have `wordsList` in your new activity, so you'll need to pass it around – Alberto S. Sep 22 '20 at 12:37
0

O yes I found the error. The following code which was causing the error must be added to newActivity.kt file instead in mainActivity.kt file. After doing that the code worked just fine.

val numbersRootView:ConstraintLayout = findViewById(R.id.activity_numbers_root_view)
            val textView = TextView(numbersRootView.context)
            textView.text = "This is from " + wordsList.get(0)
            numbersRootView.addView(textView)