Android Studio 3.1.4问题

时间:2018-08-27 21:24:30

标签: android kotlin

我是AS的新手,并且遇到了一些麻烦的问题。

我正在按照Kotlin路径学习本教程 https://developer.android.com/training/basics/firstapp/

尝试运行应用程序时出现这些错误

  

e:C:\ Users \ Dan \ Downloads \ Whackamole-toking-v1.0 \ MyFirstApp \ app \ src \ main \ java \ com \ example \ dan \ myfirstapp \ DisplayMessageActivity.kt:(19,52 ):未解决的参考:textView

     

e:C:\ Users \ Dan \ Downloads \ Whackamole-toking-v1.0 \ MyFirstApp \ app \ src \ main \ java \ com \ example \ dan \ myfirstapp \ MainActivity.kt:(20,22 ):类型为“ Intent!”的表达式“ intent”不能作为函数调用。找不到函数“ invoke()”

     

e:C:\ Users \ Dan \ Downloads \ Whackamole-toking-v1.0 \ MyFirstApp \ app \ src \ main \ java \ com \ example \ dan \ myfirstapp \ MainActivity.kt:(21,13 ):未解决的参考:putExtra

据我所知,我已经正确输入了代码。非常感谢您的帮助

编辑:这是代码

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
tools:context="com.example.myfirstapp.MainActivity">

<EditText
    android:id="@+id/editText"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginLeft="16dp"
    android:layout_marginTop="16dp"
    android:ems="10"
    android:hint="@string/edit_message"
    android:inputType="textPersonName"
    app:layout_constraintEnd_toStartOf="@+id/button"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="16dp"
    android:layout_marginStart="16dp"
    android:onClick="sendMessage"
    android:text="@string/button_send"
    app:layout_constraintBaseline_toBaselineOf="@+id/editText"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toEndOf="@+id/editText" />
</android.support.constraint.ConstraintLayout>

MainActivity.kt

package com.example.dan.myfirstapp

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.EditText

const val EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE"

class MainActivity : AppCompatActivity() {

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

activity_display_message.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
tools:context=".DisplayMessageActivity">

<EditText
    android:id="@+id/editText2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:ems="10"
    android:inputType="textPersonName"
    android:text="Name"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

DisplayMessageActivity.kt

package com.example.dan.myfirstapp

import android.content.Intent
import android.support.v7.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 it's text
    val textView = findViewById<TextView>(R.id.textView).apply {
        text = message
    }
}
}

1 个答案:

答案 0 :(得分:2)

  1. textViewactivity_display_message.xml中不存在
  2. val intent = Intent(..) 而不是val intent = intent(..)

第一次:将TextView的{​​{1}}放在textView
对于第二个问题:您将变量activity_display_message.xml实例化为类intent的实例。

编辑:我发现了您正在学习的课程,并且发现您错过了标题为
“添加文本视图”的部分