【发布时间】:2020-04-11 13:42:16
【问题描述】:
我有一个可以在视图之间切换的简单项目。我有一个连接到main_actvity.xml 的mainActvity.kt 文件,然后我有一个连接到fragment_profile.xml 的profile.kt 文件。我需要将profile.kt 添加到androidManfist.xml 文件有人知道该怎么做吗?因此,当我按下button 时,textView 的text 应该变成“Hi how are you doing?"。我有不同的文件,因为当我尝试在MainActvity.kt 中查找具有id 的元素时,它会给我错误null . 结论我面临的问题是android不会运行连接到fragment_profile.xml的文件。我几乎尝试了所有方法。
这是我的AndroidManfist.xml 文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.lecture">
<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=".Profile"/>
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
这是我的profile.kt 文件
package com.example.lecture
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
class Profile(): AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.fragment_notifications)
val update = findViewById<Button>(R.id.updateButton)
update.setOnClickListener{ toaster() }
}
private fun toaster() {
val password = findViewById<TextView>(R.id.passwordAddressText)
password.text = "Hi how are you doing?"
}
}
【问题讨论】:
-
"我有一个 profile.kt 文件连接到 fragment_profile.xml。" – 如果
profile确实是Fragment,则不要在清单中列出它。清单中仅列出了Activity、Service、BroadcastReceiver和ContentProvider子类。 -
它仍然不起作用我删除了使文件成为片段的命令并运行它。还是不行。
-
这里没有足够的信息让我们能够确定您到底想要做什么,或者为什么它不起作用。请edit您的问题更好地解释问题,并包含任何相关代码和 XML。
-
好的,我添加了代码并添加了更多关于我的问题的信息。这足够了吗,还是您希望我添加更多内容?