I did the myfirstapp tutorial from developer.android.com, see here: link. I'm using Android Studio 3.5.2. I'm a complete beginner so takes a bit of time finding my way through all the menus. I managed up to 'Build a simple user interface' and actually understand what happens/need to be done. I can follow the instructions for 'Start another activity'. Modify the code for the MainActivity like this:
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity
const val EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE"
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
/** Called when the user taps the Send button */
fun sendMessage(view: View) {
val editText = findViewById<EditText>(R.id.editText)
val message = editText.text.toString()
val intent = Intent(this, DisplayMessageActivity::class.java).apply {
putExtra(EXTRA_MESSAGE, message)
}
startActivity(intent)
}
}
Then I create a second activity. First I create the window, then modify the code as follow
package com.example.myfirstapp
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
class DisplayMessageActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_display_message)
// Get the Intent that started this activity and extract the string
val message = intent.getStringExtra(EXTRA_MESSAGE)
// Capture the layout's TextView and set the string as its text
val textView = findViewById<TextView>(R.id.textView).apply {
text = message
}
}
}
The tutorial states that I need the following imports:
import androidx.appcompat.app.AppCompatActivity
import android.content.Intent
import android.os.Bundle
import android.widget.TextView
However, only the last 2 show up. If I add the first 2 manually, it gives the message 'Unused import directive'?
I also get the message for setContentView, findViewById and text that they are 'Unresolved reference'.
If I want to run the app I get the message: 'Default Activity not found'
Not sure if this is all related, but I wonder how to proceed. If I press Alt+Enter at 'intent' to import it's class it doesn't give me that option, I can create a local variable, parameter or property. Same for the others.
@Bracadabra The manifest file looks like this, I did add the activity:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".DisplayMessageActivity2"></activity>
<activity
android:name=".DisplayMessageActivity"
android:parentActivityName=".MainActivity">
<!-- The meta-data tag is required if you support API level 15 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
</application>
@Md.Assaduzzaman I did forget the first line of code for the DisplayMessageActivity, I've included it now and it activates the import androidx.appcompat.app.AppCompatActivity
but not the import android.content.Intent
The error message is still the same.