Home > front end >  Button not working for opening new activity on Android Studio
Button not working for opening new activity on Android Studio

Time:12-02

I am a beginner in Android Studio, i have taken this code from the internet and i have understood how it works but when i press the button in the emulator, my app crashes. 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

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

CodePudding user response:

change the xml to

    <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)
}
  • Related