我正在为一个业余项目制作一个应用程序,我自己对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>
启动屏幕活动. 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>
逻辑猫:
您好,请检查您正在使用的主题。您使用的是材料按钮,主题应该是材料主题,否则它会崩溃。如果您使用的是正确的材料主题,并且想要隐藏或不需要操作栏,只需在样式中使用NoActionBar主题并将其应用于main。
在我的项目出现通用C运行时问题之前…重启Android后,它没有问题地构建,只是为了单独运行启动屏幕…我确定这是运行时。
请查看https://support.microsoft.com/en-us/topic/update-for-universal-c-runtime-in-windows-c0514201-7fe6-95a3-b0a5-287930f3560c