push string to array from another activity

51
January 11, 2019, at 6:00 PM

my aplication

I have 2 activity, the first is CategoryActivity and the second is QuestionActivity. in CategoryActivity i have a recyclerview which lists categories, and also in Category Activity, i have an array. When i click eksterior, it will go to questionActivity, and when i click submit, it will back to CategoryActivity and send data to CategotyActivity. the data sent was entered into the array, so the array will be like this: array [] = {“1”}; then i click the second item in recyclerview which is interior, then it will go to QuestionActivity again, when i click submit, it will send data to CategoryActivity and add it into the previous array, so the array will be like this : array [] = {“1”,”1”}; and so on until the items on the recyclerview run out. I have tried like this :

QuestionActivity.kt

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_result)
    button_save.setOnClickListener(){
        val intent = Intent(this@ResultActivity, KategoriActivity::class.java)
        intent.putExtra(KategoriActivity.Companion.ARRAY, "1")
        startActivity(intent)
    }
}

CategoryActivity.kt

var list = arrayOf(intent.getStringExtra(ARRAY))
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_category)
    Toast.makeText(this@KategoriActivity, ""+ list, Toast.LENGTH_SHORT).show()

}
companion object {
    val ARRAY = "array"
}
override fun onBackPressed() {
    Toast.makeText(this@KategoriActivity, ""+ list, Toast.LENGTH_SHORT).show()
    if (list.size == kategoriDatas!!.size){
        onBackPressed()
    } else {
        val builder = AlertDialog.Builder(this)
        builder.setMessage("You Must Finish All The Category")
        // add a button
        builder.setPositiveButton("OK", null)
        // create and show the alert dialog
        val dialog = builder.create()
        dialog.show()
    }
    }

and the problem is when i open category activity the app is stopped, and the log is say Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Intent.getStringExtra(java.lang.String)' on a null object reference

and the data can not push to the array, please help me

Answer 1

You can only get the intent after the activity has been created. In order to make sure of it it's best if you place the code that's gonna get the extras from Intents inside your onCreate.

Like this:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_category)
    Toast.makeText(this@KategoriActivity, ""+ list, Toast.LENGTH_SHORT).show()
    var list = arrayOf(intent.getStringExtra(ARRAY))
}

If you want to have an Application class that holds information:

class MyApplication : Application(){
var list = emptyArray<String>()
  override fun onCreate() {
    super.onCreate()
  }
}

To use it in your activity:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_category)
    Toast.makeText(this@KategoriActivity, ""+ list, Toast.LENGTH_SHORT).show()
    var list = (application as MyApplication).list
   // you can than use it to update or retrieve values from it
}
READ ALSO
My app cannot see a library within a library

My app cannot see a library within a library

I created 2 Android librariesNames are LibA and LibB

68
How to make that the snackbar action button be shown in a different line if the text is long?

How to make that the snackbar action button be shown in a different line if the text is long?

I am working with the Material Design components in the support library version 280

67
Location service giving beep sound every second above android 8.0 version

Location service giving beep sound every second above android 8.0 version

I am running the background service in that iam using location service to get the current locationFor every second it gives a beep sound in above android 8

42
Playerprefs are deleted after compilation

Playerprefs are deleted after compilation

I'm working on a simple 2D android game nowI use playerprefs to store all app data

67