我有一个主要活动,另一个活动。当我单击发送按钮时,应用程序停止。日志显示运行时异常。这是我的代码,请帮助解决此问题。我刚刚开始学习构建android应用。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.dinesh.mdk">
<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=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</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>
</manifest>
这是主要活动
package com.example.dinesh.mdk
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.content.Intent
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.editText3)
val message = editText.text.toString()
val intent = Intent(this, DisplayMessageActivity::class.java).apply {
putExtra(EXTRA_MESSAGE, message)
}
startActivity(intent)
}
}
这是显示消息活动的代码。
package com.example.dinesh.mdk
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
import android.content.Intent
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
}
}
我不知道是什么问题。这是我的日志。我正在使用Ubuntu
10-20 19:53:00.634 2526-2526/android.process.media E/libprocessgroup: failed to make and chown /acct/uid_10005: Read-only file system
10-20 19:53:00.634 2526-2526/android.process.media W/Zygote: createProcessGroup failed, kernel missing CONFIG_CGROUP_CPUACCT?
10-20 19:53:03.941 2526-2714/android.process.media E/SQLiteLog: (283) recovered 60 frames from WAL file /data/user/0/com.android.providers.media/databases/internal.db-wal
10-20 19:53:04.352 2526-2714/android.process.media W/MediaScanner: Error opening directory '/oem/media/', skipping: No such file or directory.
10-20 19:53:04.395 2526-2714/android.process.media W/MediaProvider: no database for scanned volume external
10-20 19:53:04.613 2526-2714/android.process.media E/SQLiteLog: (283) recovered 30 frames from WAL file /data/user/0/com.android.providers.media/databases/external.db-wal
10-20 19:53:04.746 2526-2714/android.process.media D/MediaProvider: object removed 10
10-20 19:53:04.747 2526-2714/android.process.media D/MediaProvider: object removed 20
object removed 21
object removed 22
object removed 23
object removed 24
object removed 25
object removed 26
object removed 27
10-20 19:53:05.221 2526-2714/android.process.media V/MediaScanner: pruneDeadThumbnailFiles... android.database.sqlite.SQLiteCursor@315e15bc
10-20 19:53:05.222 2526-2714/android.process.media V/MediaScanner: /pruneDeadThumbnailFiles... android.database.sqlite.SQLiteCursor@315e15bc
10-20 19:53:11.137 2526-2932/android.process.media D/DownloadManager: [5] Starting
10-20 19:53:11.417 2526-2932/android.process.media W/ActivityThread: ClassLoader.loadClass: The class loader returned by Thread.getContextClassLoader() may fail for processes that host multiple applications. You should explicitly specify a context class loader. For example: Thread.setContextClassLoader(getClass().getClassLoader());
10-20 19:53:14.595 2526-2932/android.process.media D/DownloadManager: [5] Finished with status SUCCESS
10-20 19:53:16.139 2526-3062/android.process.media D/DownloadManager: [6] Starting
10-20 19:53:16.488 2526-3062/android.process.media D/DownloadManager: [6] Finished with status SUCCESS
答案 0 :(得分:0)
使用此
fun sendMessage(view: View) {
val editText = findViewById<EditText>(R.id.editText3)
val message = editText.text.toString()
val intent = Intent(this, DisplayMessageActivity::class.java)
intent.putExtra(EXTRA_MESSAGE, message)
startActivity(intent)
}