单击按钮时如何从一种活动切换到另一种活动

时间:2018-10-21 14:54:37

标签: android android-activity kotlin material-design

我正在做一个教程来学习如何用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")
        }
    }
}

2 个答案:

答案 0 :(得分:1)

该错误与活动丢失有关,但也提到了android.support.design.button.MaterialButton,这很奇怪。
同样奇怪的是,您按下以打开活动的MaterialButton的ID是CreateAccount,与您要打开的活动的类名相同。
也许更改按钮的ID,然后重试。

答案 1 :(得分:0)

解决方案:如下所示,尝试使用Material库而不是为Design使用MaterialButton库:

代替使用:

<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"/>

使用:

<com.google.android.material.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"/>

尝试一下。希望这会有所帮助。