gpt4 book ai didi

android - 启动画面和崩溃后应用程序未打开下一个 Activity

转载 作者:行者123 更新时间:2023-12-05 00:00:40 25 4
gpt4 key购买 nike

我正在为一个副项目制作一个应用程序,并且我自己对 kotlin 编程还很陌生,我不知道我面临的问题的最佳解决方案。当我运行我的应用程序时,启动画面出现但没有进入登录页面,而是关闭了应用程序本身。令人惊讶的是,它没有给出任何错误并自行关闭 gradle。代码如下。感谢您提前提供帮助。

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:background="@drawable/background"
android:padding="32dp"
tools:context=".MainActivity">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="vertical">

<EditText
android:id="@+id/username_et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="110dp"
android:layout_marginBottom="50dp"
android:hint="@string/username_in"/>

<EditText
android:id="@+id/password_et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/password_in"
android:inputType="textPassword" />

<com.google.android.material.button.MaterialButton
android:id="@+id/login_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="50dp"
android:layout_marginBottom="50dp"
android:backgroundTint="@color/black"
android:padding="12dp"
android:text="@string/login_in"
android:textSize="20sp"
android:textAllCaps="false"
android:textColor="@color/white"
android:textStyle="bold"
app:cornerRadius="10dp"/>
</LinearLayout>

<TextView
android:id="@+id/app_heading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/welcome"
android:textAppearance="@style/TextAppearance.AppCompat.Headline"
android:textColor="@color/black"
android:textSize="40sp"
android:textStyle="bold" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="580dp"
android:layout_marginBottom="580dp"
android:text="@string/learnr"
android:textAppearance="@style/TextAppearance.AppCompat.Headline"
android:textColor="@color/bright_yellow"
android:textSize="40sp"
android:textStyle="bold" />

</RelativeLayout>

MainActivity.kt:

package com.example.learnr

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import android.widget.Toast.makeText

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
supportActionBar?.hide()
makeText(this, "Welcome", Toast.LENGTH_SHORT).show()

val userName = findViewById<EditText>(R.id.username_et)
val passWord = findViewById<EditText>(R.id.password_et)
val loginButton = findViewById<Button>(R.id.login_btn)

loginButton.setOnClickListener {
val status=if (userName.text.toString() == "DevangSahani"
&& passWord.text.toString() == "devangs"
) "Logged in Successfully" else "Login failed, please try again"
makeText(this, status, Toast.LENGTH_SHORT).show()
}
}
}

activity_splash_screen.xml:

<?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"
android:background="@color/black"
tools:context=".SplashScreenActivity">

<TextView
android:id="@+id/splash_heading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/learnr"
android:textAppearance="@style/TextAppearance.AppCompat.Headline"
android:textColor="@color/bright_yellow"
android:textSize="60sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.499" />

</androidx.constraintlayout.widget.ConstraintLayout>

SplashScreenActivity.kt:

package com.example.learnr

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler

class SplashScreenActivity : AppCompatActivity() {

lateinit var handler: Handler
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_splash_screen)
supportActionBar?.hide()

handler = Handler()
handler.postDelayed({

val intent = Intent(this,MainActivity::class.java)
startActivity(intent)
finish()

}, 3000)

}
}

AndroidManifest.xml:

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

<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/Theme.Learnr">
<activity
android:name=".MainActivity"
android:exported="true"
android:theme="@style/DemoThemeMain"/>
<activity
android:name=".SplashScreenActivity"
android:exported="true"
android:theme="@style/DemoTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

strings.xml:

<resources>
<string name="app_name">Learnr</string>
<string name="welcome">Welcome.</string>
<string name="learnr">Learnr.™</string>
<string name="username">Username:</string>
<string name="password">Password:</string>
<string name="username_in">Username</string>
<string name="password_in">Password</string>
<string name="login_in">Login</string>

<!-- Defined a new style with three items of color. -->
<style name="DemoTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/black</item>

<!-- Defining that new color in ColorPrimaryDark -->
<item name="colorPrimaryDark">@color/black</item>
<item name="colorAccent">@color/black</item>
</style>

<!-- Defined a new style with three items of color. -->
<style name="DemoThemeMain" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/bright_yellow</item>

<!-- Defining that new color in ColorPrimaryDark -->
<item name="colorPrimaryDark">@color/bright_yellow</item>
<item name="colorAccent">@color/bright_yellow</item>
</style>

日志:

2022-01-11 12:18:20.600 11350-11350/com.example.learnrE/AndroidRuntime: FATAL EXCEPTION: mainProcess: com.example.learnr, PID: 11350java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.learnr/com.example.learnr.MainActivity}:android.view.InflateException: Binary XML file line #46 incom.example.learnr:layout/activity_main: Binary XML file line #46 incom.example.learnr:layout/activity_main: Error inflating classcom.google.android.material.button.MaterialButtonat android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)at android.os.Handler.dispatchMessage(Handler.java:106)at android.os.Looper.loop(Looper.java:223)at android.app.ActivityThread.main(ActivityThread.java:7656)at java.lang.reflect.Method.invoke(Native Method)at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)Caused by: android.view.InflateException: Binary XML file line #46 in com.example.learnr:layout/activity_main: Binary XML file line #46 in com.example.learnr:layout/activity_main: Error inflating class com.google.android.material.button.MaterialButtonCaused by: android.view.InflateException: Binary XML file line #46 in com.example.learnr:layout/activity_main: Error inflating class com.google.android.material.button.MaterialButtonCaused by: java.lang.reflect.InvocationTargetExceptionat java.lang.reflect.Constructor.newInstance0(Native Method)at java.lang.reflect.Constructor.newInstance(Constructor.java:343)at android.view.LayoutInflater.createView(LayoutInflater.java:852)at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1004)at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:959)at android.view.LayoutInflater.rInflate(LayoutInflater.java:1121)at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082)at android.view.LayoutInflater.rInflate(LayoutInflater.java:1124)at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082)at android.view.LayoutInflater.inflate(LayoutInflater.java:680)at android.view.LayoutInflater.inflate(LayoutInflater.java:532)at android.view.LayoutInflater.inflate(LayoutInflater.java:479)at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:706)at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:195)at com.example.learnr.MainActivity.onCreate(MainActivity.kt:13)at android.app.Activity.performCreate(Activity.java:8000)at android.app.Activity.performCreate(Activity.java:7984)at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)at android.os.Handler.dispatchMessage(Handler.java:106)at android.os.Looper.loop(Looper.java:223)at android.app.ActivityThread.main(ActivityThread.java:7656)at java.lang.reflect.Method.invoke(Native Method)at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)2022-01-11 12:18:20.601 11350-11350/com.example.learnrE/AndroidRuntime: Caused by: java.lang.IllegalArgumentException: Thestyle on this component requires your app theme to beTheme.MaterialComponents (or a descendant).at com.google.android.material.internal.ThemeEnforcement.checkTheme(ThemeEnforcement.java:243)at com.google.android.material.internal.ThemeEnforcement.checkMaterialTheme(ThemeEnforcement.java:217)at com.google.android.material.internal.ThemeEnforcement.checkCompatibleTheme(ThemeEnforcement.java:145)at com.google.android.material.internal.ThemeEnforcement.obtainStyledAttributes(ThemeEnforcement.java:76)at com.google.android.material.button.MaterialButton.(MaterialButton.java:229)at com.google.android.material.button.MaterialButton.(MaterialButton.java:220)... 30 more 2022-01-11 12:18:20.660 11350-11350/com.example.learnr I/Process: Sending signal. PID: 11350SIG: 9

最佳答案

您好,请检查您使用的主题。您使用了 materialButton 并且主题应该是 Material 主题,否则它会崩溃。如果您使用的是正确的 Material 主题并且想要隐藏或者您不需要操作栏,只需在样式中使用 NoActionBar 主题并将其应用于 main。

关于android - 启动画面和崩溃后应用程序未打开下一个 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70646799/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com