提问人:Cruz 提问时间:6/18/2023 最后编辑:Cruz 更新时间:6/18/2023 访问量:505
Android Studio - “无法找到显式活动类”,我已经在 Manifest 中声明了活动,这是建议的问题
Android Studio - "Unable to find explicit activity class" and I have already declared the activities in Manifest, which is the suggested problem
问:
当我尝试单击应用程序中的按钮时,logcat 中会出现此错误,并且它崩溃了:
```
FATAL EXCEPTION: main
Process: com.example.savedtrial, PID: 16090
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.savedtrial/com.google.android.material.button.MaterialButton}; have you declared this activity in your AndroidManifest.xml, or does your intent not match its declared <intent-filter>?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:2197)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1839)
at android.app.Activity.startActivityForResult(Activity.java:5471)
at androidx.activity.ComponentActivity.startActivityForResult(ComponentActivity.java:728)
at android.app.Activity.startActivityForResult(Activity.java:5429)
at androidx.activity.ComponentActivity.startActivityForResult(ComponentActivity.java:709)
at android.app.Activity.startActivity(Activity.java:5927)
at android.app.Activity.startActivity(Activity.java:5894)
at com.example.savedtrial.MainActivity.onCreate$lambda$0(MainActivity.kt:23)
at com.example.savedtrial.MainActivity.$r8$lambda$47nZOKobj58xqg2t-Q-x4Lw-Afk(Unknown Source:0)
at com.example.savedtrial.MainActivity$$ExternalSyntheticLambda0.onClick(Unknown Source:4)
at android.view.View.performClick(View.java:7506)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1131)
at android.view.View.performClickInternal(View.java:7483)
at android.view.View.-$$Nest$mperformClickInternal(Unknown Source:0)
at android.view.View$PerformClick.run(View.java:29334)
at android.os.Handler.handleCallback(Handler.java:942)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7872)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
```
这是我的 Mainfest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package = "com.example.savedtrial"
xmlns:tools="http://schemas.android.com/tools">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.SAVEDTRIAL"
tools:targetApi="31">
<activity
android:name="com.example.savedtrial.contacting"
android:exported="false"
android:parentActivityName="com.example.savedtrial.MainActivity"/>
<activity
android:name="com.example.savedtrial.Collab"
android:exported="false"
android:parentActivityName="com.example.savedtrial.MainActivity"/>
<activity
android:name="com.example.savedtrial.notes"
android:exported="false"
android:parentActivityName="com.example.savedtrial.MainActivity" />
<activity
android:name="com.example.savedtrial.Calender"
android:exported="false"
android:parentActivityName="com.example.savedtrial.MainActivity" />
<activity
android:name="com.example.savedtrial.MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
带有按钮代码的主 Kotlin 文件
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val actionBar = supportActionBar
supportActionBar!!.setTitle("PROductivity")
val calender: Button = findViewById(R.id.calender)
calender.setOnClickListener {
val intent = Intent(this, calender::class.java)
startActivity(intent)
}
val notes: Button = findViewById(R.id.notes)
notes.setOnClickListener {
val intent = Intent(this, notes::class.java)
startActivity(intent)
}
val contacting: Button = findViewById(R.id.contacting)
contacting.setOnClickListener {
val intent = Intent(this, contacting::class.java)
startActivity(intent)
}
val collab: Button = findViewById(R.id.collab)
collab.setOnClickListener {
val intent = Intent(this, collab::class.java)
startActivity(intent) }
}
}
这是其中一个文件的代码,该文件应该在单击其各自的按钮时显示(只是一个带有后退按钮的操作栏)
package com.example.savedtrial
import android.content.Intent
import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
class contacting : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_contacting)
val actionBar = supportActionBar
supportActionBar!!.setTitle("Contacting")
supportActionBar!!.setDisplayHomeAsUpEnabled(true)
}
}
我的主要活动布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivity">
<Button
android:id="@+id/calender"
android:layout_width="126dp"
android:layout_height="53dp"
android:text="Calender"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.021"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.3" />
<Button
android:id="@+id/notes"
android:layout_width="127dp"
android:layout_height="54dp"
android:text="Notes"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.021"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.473" />
<Button
android:id="@+id/contacting"
android:layout_width="129dp"
android:layout_height="51dp"
android:text="Contacting"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.02"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.672" />
<Button
android:id="@+id/collab"
android:layout_width="129dp"
android:layout_height="51dp"
android:text="Collab. Whiteboard"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.019"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.844" />
<TextView
android:id="@+id/productivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="PROductivity"
android:textColor="#EA1616"
android:textSize="34sp"
android:textStyle="bold|italic"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
我是 Android Studio 的新手,所以如果可能的话,尽量不要深入研究极其复杂的东西,但如果必须的话,请不要担心 - 我会弄清楚的。
答:
1赞
ianhanniballake
6/18/2023
#1
错误消息的重要部分是以下部分:
无法找到显式活动类 {com.example.savedtrial/com.google.android.material.button.MaterialButton}
这意味着您在调用 时传递了一个 Button,而不是 Activity。事实上,我们可以在你的第一个中看到这一点,你在构建你的意图时引用了错误的类:startActivity
setOnClickListener
val intent = Intent(this, calender::class.java)
您正在使用为您的类命名的实例。它应该与你的清单类相匹配 - 注意大写字母来引用你的活动类,而不是你的局部变量:Button
calendar
Calendar
C
val intent = Intent(this, Calender::class.java)
对于其他所有点击侦听器也是如此(除了 activity,您也将其设置为小写,这使得区分局部变量和类名变得更加困难 - 您应该始终使用大写字母命名您的类)。notes
如果将变量名称与类名分开,您会发现事情更容易理解。这就是为什么您通常会看到名为 、 等的类,您可以在其中更轻松地确定什么是活动实例,哪些是启动该活动的按钮(、 等)。CalendarActivity
CollabActivity
calendar
collab
评论
0赞
Cruz
6/18/2023
非常感谢ianhanniballake!你真的救了我的命,也给了我天堂般的建议。我真的非常感谢你的答案,以及它几乎是即时的!
0赞
ianhanniballake
6/18/2023
乐于助人 - 祝你好运!
评论