kotlin 按钮无法在Android Studio上打开新活动

k97glaaz  于 2022-12-04  发布在  Kotlin
关注(0)|答案(1)|浏览(178)

我是Android Studio的初学者,我从互联网上获得了这段代码,我已经了解它是如何工作的,但当我在模拟器中按下按钮时,我的应用程序崩溃。我希望我的按钮打开我创建的第二个Activity。以下是我的主Activity和第一个Activity的代码

package com.example.plaques

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.Button
import androidx.activity.ComponentActivity



class MainActivity : ComponentActivity() {

@Override
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.application1)

}

fun ouvrir(view: View) {
    val intent = Intent(this, MainActivity2::class.java )
    startActivity(intent)
}

}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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">

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="open next activity"
        android:onClick="ouvrir"
        tools:ignore="MissingConstraints,UsingOnClickInXml" />

</androidx.constraintlayout.widget.ConstraintLayout>

顺便说一句,我用Kotlin编写代码
以下是崩溃日志:

--------- beginning of crash
2022-12-01 14:31:10.752 2297-2297/com.example.plaques E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.plaques, PID: 2297
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.plaques/com.example.plaques.MainActivity2}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3676)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3813)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:101)
        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:2308)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loopOnce(Looper.java:201)
        at android.os.Looper.loop(Looper.java:288)
        at android.app.ActivityThread.main(ActivityThread.java:7898)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
     Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
        at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:852)
        at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:815)
        at androidx.appcompat.app.AppCompatDelegateImpl.initWindowDecorActionBar(AppCompatDelegateImpl.java:556)
        at androidx.appcompat.app.AppCompatDelegateImpl.getSupportActionBar(AppCompatDelegateImpl.java:543)
        at androidx.appcompat.app.AppCompatDelegateImpl.invalidateOptionsMenu(AppCompatDelegateImpl.java:1226)
        at androidx.appcompat.app.AppCompatActivity.invalidateOptionsMenu(AppCompatActivity.java:314)
        at androidx.activity.ComponentActivity.invalidateMenu(ComponentActivity.java:553)
        at androidx.activity.ComponentActivity$$ExternalSyntheticLambda0.run(Unknown Source:2)
        at androidx.core.view.MenuHostHelper.addMenuProvider(MenuHostHelper.java:116)
        at androidx.activity.ComponentActivity.addMenuProvider(ComponentActivity.java:531)
        at androidx.fragment.app.FragmentActivity$HostCallbacks.addMenuProvider(FragmentActivity.java:736)
        at androidx.fragment.app.FragmentManager.attachController(FragmentManager.java:2793)
        at androidx.fragment.app.FragmentController.attachHost(FragmentController.java:117)
        at androidx.fragment.app.FragmentActivity.lambda$init$3$androidx-fragment-app-FragmentActivity(FragmentActivity.java:140)
        at androidx.fragment.app.FragmentActivity$$ExternalSyntheticLambda3.onContextAvailable(Unknown Source:2)
        at androidx.activity.contextaware.ContextAwareHelper.dispatchOnContextAvailable(ContextAwareHelper.java:99)
        at androidx.activity.ComponentActivity.onCreate(ComponentActivity.java:352)
        at androidx.fragment.app.FragmentActivity.onCreate(FragmentActivity.java:217)
        at com.example.plaques.MainActivity2.onCreate(MainActivity2.kt:8)
        at android.app.Activity.performCreate(Activity.java:8290)
        at android.app.Activity.performCreate(Activity.java:8269)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1384)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3657)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3813) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:101) 
        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:2308) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loopOnce(Looper.java:201) 
        at android.os.Looper.loop(Looper.java:288) 
        at android.app.ActivityThread.main(ActivityThread.java:7898) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936) 
2022-12-01 14:31:10.868 2297-2297/com.example.plaques I/Process: Sending signal. PID: 2297 SIG: 9

谢谢你的帮助!
我试图改变教程,但每次出现新的错误,所以我被阻止

6bc51xsx

6bc51xsx1#

将xml更改为

<Button
    android:id="@+id/mybtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="open next activity"/>

和来自

fun ouvrir(view: View) {
    val intent = Intent(this, MainActivity2::class.java )
    startActivity(intent)
}

至:

val mybtn: Button = findViewById(R.id.mybtn)

mybtn.setOnClickListener{
    val intent = Intent(this, MainActivity2::class.java )
    startActivity(intent)
}

相关问题