【问题标题】:How can I switch from one activity to another when I click a button单击按钮时如何从一个活动切换到另一个活动
【发布时间】:2018-10-21 14:54:37
【问题描述】:

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

【问题讨论】:

  • 您的错误信息和您的代码似乎不匹配。确保您正在运行此代码,而不是您的代码的某个先前版本。
  • 尝试构建 > clean 然后再次运行。您的构建可能存在一些问题。 @CommonsWare 关于代码和错误是正确的。
  • 你能显示你的 Gradle 依赖关系吗

标签: android android-activity kotlin material-design


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    解决方案:尝试使用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"/>
    

    试试看。希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-08
      • 2017-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多