简体   繁体   English

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

[英]Button not working for opening new activity on Android Studio

I am a beginner in Android Studio, i have taken this code from the inte.net and i have understood how it works but when i press the button in the emulator, my app crashes.我是 Android Studio 的初学者,我从 inte.net 获取了这段代码,我已经了解它是如何工作的,但是当我按下模拟器中的按钮时,我的应用程序崩溃了。 I want my button to open the second activity i created.我想让我的按钮打开我创建的第二个活动。 here are my codes for the main and first 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>

I code in kotlin btw我在 kotlin 顺便说一句

Here is the crash log:这是崩溃日志:

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

thanks for your help !感谢您的帮助 !

i tried to change tutorials but everytime a new error appears so i am blocked我试图更改教程,但每次出现新错误时我都被阻止了

change the xml to将 xml 更改为

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

and the code from和代码来自

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

to:到:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM