0

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.

egilio
  • 97
  • 1
  • 1
  • 6

3 Answers3

1

Please, check your AndroidManifest file, your activity should be declare in it. Something like this:

<activity android:name=".MainActivity">
    <intent-filter>
        <action android:name="android.intent.action.MAIN"/>
        <category android:name="android.intent.category.LAUNCHER"/>
    </intent-filter>
</activity>
Bracadabra
  • 3,609
  • 3
  • 26
  • 46
0

You do not need to import the Intent class and the TextView class because you currently are not using either in your second Activity.

the line intent.getStringExtra(EXTRA_MESSAGE) refers to the getIntent method attached to an activity.

You would need to import those classes when you want to instantiate them for use in your activity.

I hope this helps

Demilade
  • 513
  • 2
  • 7
  • In the tutorial they write that an intent is build by adding the EXTRA_MESSAGE constant and the sendMessage code, by adding the following code to MainActivity: which I have done (see above). – egilio Nov 19 '19 at 09:43
0

rai kavadia and Bracadabra put me on the right track. The mainActivity wasn't declared in the AndroidManifest file. In the end all worked well like this.

The MainActivity kotlin file:

package com.example.myfirstapp

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)
    }
}

The DisplayMessageActivity Kotlin file:

package com.example.myfirstapp

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.example.mysecondapp.R.layout.activity_display_message
import android.widget.TextView

class DisplayMessageActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(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 AndroidManifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myfirstapp">

    <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=".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>

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
egilio
  • 97
  • 1
  • 1
  • 6