gpt4 book ai didi

Android Studio 3.1.4 问题

转载 作者:行者123 更新时间:2023-11-29 18:45:39 24 4
gpt4 key购买 nike

我是 AS 的新手,我遇到了一些初期问题。

我正在使用 kotlin 路径学习本教程 https://developer.android.com/training/basics/firstapp/

我在尝试运行应用程序时遇到这些错误

e: C:\Users\Dan\Downloads\Whackamole-Making-toast-v1.0\MyFirstApp\app\src\main\java\com\example\dan\myfirstapp\DisplayMessageActivity.kt: (19, 52): Unresolved reference: textView

e: C:\Users\Dan\Downloads\Whackamole-Making-toast-v1.0\MyFirstApp\app\src\main\java\com\example\dan\myfirstapp\MainActivity.kt: (20, 22): Expression 'intent' of type 'Intent!' cannot be invoked as a function. The function 'invoke()' is not found

e: C:\Users\Dan\Downloads\Whackamole-Making-toast-v1.0\MyFirstApp\app\src\main\java\com\example\dan\myfirstapp\MainActivity.kt: (21, 13): Unresolved reference: 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. textView 不存在于 activity_display_message.xml
  2. val intent = Intent(..)而不是 val intent = intent(..)

对于第一个:在 activity_display_message.xml 中放置一个 ID 为 textViewTextView
对于第二个:您将变量 intent 实例化为类 Intent 的实例。

编辑:我找到了您正在学习的类(class),我发现您错过了标题为:
“添加 TextView ”的部分

关于Android Studio 3.1.4 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52047170/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com