Home > Software design >  Application crashing after pressing button
Application crashing after pressing button

Time:04-23

So i'm new to all this coding in mobile programming I was just following some tutorials on how to do things here and there I'm trying to make a button that makes it so the screen goes to the next activity Heres the MainActivity.java

    package com.test1;


import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                openSecondActivity();
            }
        });
    }
    public void openSecondActivity()
    {
        Intent i = new Intent(MainActivity.this,SecondActivity.class);
        startActivity(i);
    }
}

Then here is the activity_main.xml file

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.test1.MainActivity">

    <TextView
        android:id="@ id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="178dp"
        android:layout_marginEnd="174dp"
        android:layout_marginBottom="704dp"
        android:text="First Activity" />

    <Button
        android:id="@ id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="158dp"
        android:layout_marginEnd="159dp"
        android:layout_marginBottom="622dp"
        android:backgroundTint="#DEDFE5"
        android:text="Submit" />
</RelativeLayout>

The 2nd activity.java

package com.testing1;


import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;

public class SecondActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
    }
}

and lastly the 2nd activity.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.test1.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="181dp"
        android:layout_marginTop="333dp"
        android:layout_marginEnd="172dp"
        android:layout_marginBottom="379dp"
        android:textSize="22sp"
        android:text="Second Activity" />

</RelativeLayout>

As for the logcat

2022-04-22 20:51:32.034 29305-29305/? I/com.test1: Late-enabling -Xcheck:jni
2022-04-22 20:51:32.053 29305-29305/? E/com.test1: Unknown bits set in runtime_flags: 0x8000
2022-04-22 20:51:32.238 29305-29305/com.test1 I/Perf: Connecting to perf service.
2022-04-22 20:51:32.256 29305-29305/com.test1 I/FeatureParser: can't find beryllium.xml in assets/device_features/,it may be in /system/etc/device_features
2022-04-22 20:51:32.269 29305-29305/com.test1 E/libc: Access denied finding property "ro.vendor.df.effect.conflict"
2022-04-22 20:51:32.271 29305-29369/com.test1 E/Perf: Fail to get file list com.test1
2022-04-22 20:51:32.271 29305-29369/com.test1 E/Perf: getFolderSize() : Exception_1 = java.lang.NullPointerException: Attempt to get length of null array
2022-04-22 20:51:32.272 29305-29369/com.test1 E/Perf: Fail to get file list com.test1
2022-04-22 20:51:32.272 29305-29369/com.test1 E/Perf: getFolderSize() : Exception_1 = java.lang.NullPointerException: Attempt to get length of null array
2022-04-22 20:51:32.272 29305-29369/com.test1 E/Perf: Fail to get file list oat
2022-04-22 20:51:32.272 29305-29369/com.test1 E/Perf: getFolderSize() : Exception_1 = java.lang.NullPointerException: Attempt to get length of null array
2022-04-22 20:51:32.316 29305-29305/com.test1 D/ForceDarkHelper: updateByCheckExcludeList: pkg: com.test1 activity: com.test1.MainActivity@a644dc6
2022-04-22 20:51:32.317 29305-29305/com.test1 D/ForceDarkHelper: updateByCheckExcludeList: pkg: com.test1 activity: com.test1.MainActivity@a644dc6
2022-04-22 20:51:32.321 29305-29305/com.test1 D/ForceDarkHelper: updateByCheckExcludeList: pkg: com.test1 activity: com.test1.MainActivity@a644dc6
2022-04-22 20:51:32.322 29305-29305/com.test1 I/chatty: uid=10414(com.test1) identical 1 line
2022-04-22 20:51:32.323 29305-29305/com.test1 D/ForceDarkHelper: updateByCheckExcludeList: pkg: com.test1 activity: com.test1.MainActivity@a644dc6
2022-04-22 20:51:32.325 29305-29305/com.test1 W/com.test1: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (greylist, reflection, allowed)
2022-04-22 20:51:32.325 29305-29305/com.test1 W/com.test1: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (greylist, reflection, allowed)
2022-04-22 20:51:32.327 29305-29305/com.test1 D/ForceDarkHelper: updateByCheckExcludeList: pkg: com.test1 activity: com.test1.MainActivity@a644dc6
2022-04-22 20:51:32.329 29305-29305/com.test1m D/ForceDarkHelper: updateByCheckExcludeList: pkg: com.test1 activity: com.test1.MainActivity@a644dc6
2022-04-22 20:51:32.338 29305-29305/com.test1 D/ForceDarkHelper: updateByCheckExcludeList: pkg: com.test1 activity: com.test1.MainActivity@a644dc6
2022-04-22 20:51:32.391 29305-29371/com.test1 I/AdrenoGLES: QUALCOMM build                   : e541a88, I20154638fb
    Build Date                       : 09/15/20
    OpenGL ES Shader Compiler Version: EV031.27.05.01
    Local Branch                     : 
    Remote Branch                    : refs/tags/AU_LINUX_ANDROID_LA.UM.8.3.R1.10.00.00.520.058
    Remote Branch                    : NONE
    Reconstruct Branch               : NOTHING
2022-04-22 20:51:32.391 29305-29371/com.test1 I/AdrenoGLES: Build Config                     : S P 8.0.11 AArch64
2022-04-22 20:51:32.394 29305-29371/com.test1 I/AdrenoGLES: PFP: 0x016ee187, ME: 0x00000000
2022-04-22 20:51:32.395 29305-29371/com.test1 W/AdrenoUtils: <ReadGpuID_from_sysfs:194>: Failed to open /sys/class/kgsl/kgsl-3d0/gpu_model
2022-04-22 20:51:32.395 29305-29371/com.test1 W/AdrenoUtils: <ReadGpuID:218>: Failed to read chip ID from gpu_model. Fallback to use the GSL path
2022-04-22 20:51:32.411 29305-29371/com.test1 W/Gralloc3: mapper 3.x is not supported
2022-04-22 20:51:34.978 29305-29305/com.test1 I/Timeline: Timeline: Activity_launch_request time:198697094
2022-04-22 20:51:34.980 29305-29305/com.test1 D/AndroidRuntime: Shutting down VM
2022-04-22 20:51:34.982 29305-29305/com.test1 E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.test1, PID: 29305
    android.content.ActivityNotFoundException: Unable to find explicit activity class {com.test1/com.test1.SecondActivity}; have you declared this activity in your AndroidManifest.xml?
        at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:2064)
        at android.app.Instrumentation.execStartActivity(Instrumentation.java:1720)
        at android.app.Activity.startActivityForResult(Activity.java:5258)
        at androidx.activity.ComponentActivity.startActivityForResult(ComponentActivity.java:597)
        at android.app.Activity.startActivityForResult(Activity.java:5216)
        at androidx.activity.ComponentActivity.startActivityForResult(ComponentActivity.java:583)
        at android.app.Activity.startActivity(Activity.java:5587)
        at android.app.Activity.startActivity(Activity.java:5555)
        at com.test1.MainActivity.openSecondActivity(MainActivity.java:30)
        at com.test1.MainActivity$1.onClick(MainActivity.java:23)
        at android.view.View.performClick(View.java:7185)
        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1131)
        at android.view.View.performClickInternal(View.java:7162)
        at android.view.View.access$3500(View.java:819)
        at android.view.View$PerformClick.run(View.java:27684)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:224)
        at android.app.ActivityThread.main(ActivityThread.java:7562)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
2022-04-22 20:51:35.015 29305-29305/com.test1 I/Process: Sending signal. PID: 29305 SIG: 9

as for the AndroidManifest.xml

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

    <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.test1">
        <activity
            android:name=".MainActivity"
            android:exported="true"
            android:label="@string/app_name"
            android:theme="@style/Theme.test1.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

CodePudding user response:

As the stacktrace shows you, you need to register your Activity to the AndroidManifest.

In the application tag you need to add a new entry, something like this:

<activity
     android:name=".SecondActivity"
     android:label="some title"
     android:theme="@style/AppTheme.NoActionBar" />

For more info, look at these links:

Manifest overview
Activity options

CodePudding user response:

Check this code

package com.test1;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
   Button button;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      button = (Button) findViewById(R.id.button);
      button.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            openNewActivity();
         }
      });
   }
   public void openNewActivity(){
      Intent intent = new Intent(this, SecondActivity.class);
      startActivity(intent);
   }
}

SecondActivity

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class SecondActivity extends AppCompatActivity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_second);
   }
}
  • Related