首页 > 解决方案 > 单击按钮时如何从一个活动切换到另一个活动

问题描述

我正在做一个教程来学习使用 Kotlin 构建 android 应用程序,但我发现了一个问题,我一直在调试、删除代码、添加代码,然后我云来查找问题所在

android.content.ActivityNotFoundException:找不到显式活动类 {com.application.todolist/android.support.design.button.MaterialButton};您是否在 AndroidManifest.xml 中声明了此活动?

我的 AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          package="com.application.todolist">

    <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"
            tools:ignore="GoogleAppIndexingWarning">
        <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=".CreateAccount"/>
        <activity android:name=".AddEmail"></activity>
    </application>

</manifest>

我的activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:background="@color/colorPrimary"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:paddingLeft="24dp"
            android:paddingRight="24dp"
            android:gravity="center">

            <TextView
                android:id="@+id/intro_welcome"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:textSize="26sp"
                android:textColor="@color/colorText"
                android:text="@string/intro_text_welcome"/>

            <!-- create account button -->
            <android.support.design.button.MaterialButton
                    android:id="@+id/CreateAccount"
                    android:layout_marginTop="30dp"
                    android:layout_gravity="center"
                    android:padding="15dp"
                    android:textColor="#FFF"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/create_account"
                    app:cornerRadius="50dp"
                    android:drawableRight="@drawable/ic_arrow_forward_black_24dp"
                    android:textSize="20sp"/>

            <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="25dp"
                    android:gravity="center"
                    android:textSize="16sp"
                    android:textColor="@color/colorText"
                    android:text="@string/have_account"/>

            <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="25sp"
                    android:textColor="@color/colorText"
                    android:text="@string/policy_and_terms"/>

    </LinearLayout>

</android.support.design.widget.CoordinatorLayout>

我的 activity_create_account.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimary"
    tools:context=".CreateAccount">

    <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="511dp"
            android:layout_gravity="center_horizontal"
            android:paddingLeft="24dp"
            android:paddingRight="24dp"
            android:gravity="center">

        <TextView
                android:id="@+id/intro_welcome"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="left"
                android:textSize="26sp"
                android:layout_marginBottom="24dp"
                android:textColor="@color/colorText"
                android:text="@string/ask_for_name"/>

        <!-- Name label -->
        <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="14dp"
                android:layout_marginTop="14dp"
                style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox"
                app:hintAnimationEnabled="true">

            <android.support.design.widget.TextInputEditText
                    android:id="@+id/input_name"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:inputType="text"
                    android:drawableLeft="@drawable/ic_account_box_black_24dp"
                    android:hint="@string/first_name"/>
        </android.support.design.widget.TextInputLayout>


        <!-- Last name label -->
        <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="14dp"
                android:layout_marginTop="14dp"
                style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox"
                app:hintAnimationEnabled="true">

            <android.support.design.widget.TextInputEditText
                    android:id="@+id/input_lastname"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:inputType="text"
                    android:hint="@string/last_name"/>

        </android.support.design.widget.TextInputLayout>

        <android.support.design.widget.FloatingActionButton
                android:id="@+id/step_2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="end|bottom"
                android:src="@drawable/ic_arrow_forward_black_24dp"
                android:layout_margin="16dp"
                app:fabSize="normal"/>
    </LinearLayout>

</android.support.design.widget.CoordinatorLayout>

我的 MainActivity.kt

package com.application.todolist

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
import android.support.design.button.MaterialButton
import android.util.Log

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        supportActionBar?.hide()

        val btnFirstStep : MaterialButton = findViewById(R.id.CreateAccount)

        btnFirstStep.setOnClickListener {
            val intent = Intent(
                this,
                CreateAccount::class.java
            )
            startActivity(intent)
            Log.i("MainActivity", "Button was clicked")
        }
    }
}

标签: androidandroid-activitykotlinmaterial-design

解决方案


该错误与缺少的活动有关,但也提到android.support.design.button.MaterialButton 了这很奇怪。
还有很奇怪的是,MaterialButton你按打开活动的id是 ,和你要打开的活动CreateAccount的类名一样。
也许更改按钮的 id 并重试。


推荐阅读