在安装最新的Android Studio(3.0.1)后,我开始使用构建您的第一个应用指南。我正在使用Kotlin
,因为它似乎是建议的方式。我设法在平板电脑上运行第一个Hello World应用程序。然后,继续下面的例子
https://developer.android.com/training/basics/firstapp/starting-activity.html#kotlin
我指到了“在属性窗口中,找到onClick属性并从下拉列表中选择sendMessage [MainActivity]”。 问题是,下拉列表中没有任何内容。
我尝试在按钮的onClick属性中手动键入sendMessage
,但是当我按下按钮时应用程序崩溃了。还尝试了sendMessage()
,sendMessage(this)
和sendMessage [MainActivity]
,但所有这些都会导致应用崩溃。如果我删除它,按下按钮确实 - 没有 - 没有,但也不会崩溃...还尝试按照另一个回复中的说明将属性添加到 .xml ,但没有帮助。 / p>
那么调用sendMessage
函数的正确方法是什么?哪个View object
(?)我应该给它以及如何?
我的MainActivity
看起来像这样:
package com.example.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)
}
/** 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)
}
}
和DisplayMessageActivity:
package com.example.myfirstapp
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 its text
val textView = findViewById<TextView>(R.id.textView).apply {
text = message
}
}
我会用整个筹码加注:
03-13 10:19:50.620 20646-20646/com.example.myfirstapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myfirstapp, PID: 20646
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.myfirstapp/com.example.myfirstapp.DisplayMessageActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Intent.getStringExtra(java.lang.String)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2488)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1499)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5765)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
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
at com.example.myfirstapp.DisplayMessageActivity.<init>(DisplayMessageActivity.kt:15)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1072)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2478)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1499)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5765)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
答案 0 :(得分:0)
您的sendMessage方法是正确的。
打字&#34; sendMessage&#34;在onClick字段(方法名称)就足够了。
或者在xml中添加
机器人:的onClick =&#34;的sendMessage&#34;
到按钮属性。
答案 1 :(得分:0)
您可以在android:onClick="sendMessage"
中添加setOnClickListener
或添加MainActivity
。
YourButtonID.setOnClickListener { v ->
sendMessage(v)
}