-1

I am very new to android development and i have designed a very simple app from which when i press one button it takes me to another activity but whenever i press the button my app crashes. i dont know what is wrong i know my code is right because its same everywhere on the internet. Kindly help Thanks in advance. here is my code

ActivityMain.java

    package com.dreamfighter.myapplication;

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


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        b1 = (Button) findViewById(R.id.button);
    }

    public void clickme(View view)
    {
        Intent intent = new Intent(MainActivity.this,second.class);
        startActivity(intent);
    }

}

Activity_Main.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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.dreamfighter.myapplication.MainActivity"
    android:background="#8e2424"
    >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button"
        android:layout_below="@+id/textView"
        android:layout_toRightOf="@+id/textView"
        android:layout_toEndOf="@+id/textView"
        android:layout_marginTop="134dp"
    android:onClick="clickme"
        />
</RelativeLayout>

second.java

    package com.dreamfighter.myapplication;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class second extends AppCompatActivity{
Button b2;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        b2 = (Button) findViewById(R.id.button2);
    }
    public void clck(View view){
        Intent intent = new Intent(second.this,MainActivity.class);

        startActivity(intent);
    }
}

asd.xml

<RelativeLayout

xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#26b891" >

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Click me Now"
    android:id="@+id/button2"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="332dp"
    android:onClick="clck" />

I am getting this error

08-21 14:05:12.814 19829-19829/com.dreamfighter.myapplication I/Process: Sending signal. PID: 19829 SIG: 9 08-21 14:05:57.944 20224-20224/com.dreamfighter.myapplication I/art: Late-enabling -Xcheck:jni 08-21 14:05:57.998 20224-20224/com.dreamfighter.myapplication W/System: ClassLoader referenced unknown path: /data/app/com.dreamfighter.myapplication-1/lib/arm64 08-21 14:05:58.149 20224-20224/com.dreamfighter.myapplication W/System: ClassLoader referenced unknown path: /data/app/com.dreamfighter.myapplication-1/lib/arm64 08-21 14:05:58.157 20224-20224/com.dreamfighter.myapplication D/ActivityThread: BIND_APPLICATION handled : 0 / AppBindData{appInfo=ApplicationInfo{2b96c39 com.dreamfighter.myapplication}} 08-21 14:05:58.157 20224-20224/com.dreamfighter.myapplication V/ActivityThread: Handling launch of ActivityRecord{502a27e token=android.os.BinderProxy@b15c3df {com.dreamfighter.myapplication/com.dreamfighter.myapplication.MainActivity}} startsNotResumed=false 08-21 14:05:58.212 20224-20233/com.dreamfighter.myapplication I/System: FinalizerDaemon: finalize objects = 1 08-21 14:05:58.266 20224-20224/com.dreamfighter.myapplication V/ActivityThread: ActivityRecord{502a27e token=android.os.BinderProxy@b15c3df {com.dreamfighter.myapplication/com.dreamfighter.myapplication.MainActivity}}: app=android.app.Application@8bb92f5, appName=com.dreamfighter.myapplication, pkg=com.dreamfighter.myapplication, comp={com.dreamfighter.myapplication/com.dreamfighter.myapplication.MainActivity}, dir=/data/app/com.dreamfighter.myapplication-1/base.apk 08-21 14:05:58.346 20224-20224/com.dreamfighter.myapplication W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable 08-21 14:05:58.353 20224-20224/com.dreamfighter.myapplication D/Resources: resEntryNameFinal = com_dreamfighter_myapplication_abc_vector_test 08-21 14:05:58.370 20224-20224/com.dreamfighter.myapplication E/MultiWindowProxy: getServiceInstance failed! 08-21 14:05:58.370 20224-20224/com.dreamfighter.myapplication D/PhoneWindow: XUI_SystemUI isAutoChangeXUI:true, mStatusBarColor: -13615201, mNavigationBarColor: -16777216 08-21 14:05:58.370 20224-20224/com.dreamfighter.myapplication D/PhoneWindow: XUI_SystemUI ,Color.brightness(mStatusBarColor):false,Color.brightness(mNavigationBarColor) :false 08-21 14:05:58.370 20224-20224/com.dreamfighter.myapplication D/PhoneWindow: XUI_SystemUI isDarkStatusBar:false, isDarkNavBar: false,View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR :16 08-21 14:05:58.420 20224-20224/com.dreamfighter.myapplication D/Resources: resEntryNameFinal = com_dreamfighter_myapplication_colorPrimary 08-21 14:05:58.421 20224-20224/com.dreamfighter.myapplication D/Resources: resEntryNameFinal = com_dreamfighter_myapplication_colorPrimary 08-21 14:05:58.505 20224-20224/com.dreamfighter.myapplication D/Resources: Add /mnt/sdcard/BuildRes.apk cookie = 4 08-21 14:05:58.515 20224-20224/com.dreamfighter.myapplication V/ActivityThread: Performing resume of ActivityRecord{502a27e token=android.os.BinderProxy@b15c3df {com.dreamfighter.myapplication/com.dreamfighter.myapplication.MainActivity}} 08-21 14:05:58.533 20224-20224/com.dreamfighter.myapplication D/ActivityThread: ACT-AM_ON_RESUME_CALLED ActivityRecord{502a27e token=android.os.BinderProxy@b15c3df {com.dreamfighter.myapplication/com.dreamfighter.myapplication.MainActivity}} 08-21 14:05:58.534 20224-20224/com.dreamfighter.myapplication V/ActivityThread: Resume ActivityRecord{502a27e token=android.os.BinderProxy@b15c3df {com.dreamfighter.myapplication/com.dreamfighter.myapplication.MainActivity}} started activity: false, hideForNow: false, finished: false 08-21 14:05:58.534 20224-20224/com.dreamfighter.myapplication V/PhoneWindow: DecorView setVisiblity: visibility = 4 ,Parent =null, this =com.android.internal.policy.PhoneWindow$DecorView{52ffb18 I.E...... R.....ID 0,0-0,0} 08-21 14:05:58.539 20224-20224/com.dreamfighter.myapplication D/WindowClient: Add to mViews: com.android.internal.policy.PhoneWindow$DecorView{52ffb18 I.E...... R.....ID 0,0-0,0}, this = android.view.WindowManagerGlobal@f2d548e 08-21 14:05:58.540 20224-20224/com.dreamfighter.myapplication D/OpenGLRenderer: Dumper init 4 threads <0x7f722dab40> 08-21 14:05:58.540 20224-20224/com.dreamfighter.myapplication D/OpenGLRenderer: is running. 08-21 14:05:58.542 20224-20247/com.dreamfighter.myapplication D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: false 08-21 14:05:58.542 20224-20247/com.dreamfighter.myapplication D/OpenGLRenderer: CanvasContext() 0x7f6b015000 08-21 14:05:58.553 20224-20224/com.dreamfighter.myapplication I/[MALI][Gralloc]: [+]r_hnd(0x7f63aab8c0), client(31), share_fd(30) 08-21 14:05:58.553 20224-20224/com.dreamfighter.myapplication D/GraphicBuffer: register, handle(0x7f63aab8c0) (w:1216 h:2048 s:1216 f:0x1 u:0x000100) 08-21 14:05:58.555 20224-20224/com.dreamfighter.myapplication D/ViewRootImpl: hardware acceleration is enabled, this = ViewRoot{9e474af com.dreamfighter.myapplication/com.dreamfighter.myapplication.MainActivity,ident = 0} 08-21 14:05:58.560 20224-20224/com.dreamfighter.myapplication V/ActivityThread: Resuming ActivityRecord{502a27e token=android.os.BinderProxy@b15c3df {com.dreamfighter.myapplication/com.dreamfighter.myapplication.MainActivity}} with isForward=true 08-21 14:05:58.560 20224-20224/com.dreamfighter.myapplication V/PhoneWindow: DecorView setVisiblity: visibility = 0 ,Parent =ViewRoot{9e474af com.dreamfighter.myapplication/com.dreamfighter.myapplication.MainActivity,ident = 0}, this =com.android.internal.policy.PhoneWindow$DecorView{52ffb18 V.E...... R.....ID 0,0-0,0} 08-21 14:05:58.560 20224-20224/com.dreamfighter.myapplication V/ActivityThread: Scheduling idle handler for ActivityRecord{502a27e token=android.os.BinderProxy@b15c3df {com.dreamfighter.myapplication/com.dreamfighter.myapplication.MainActivity}} 08-21 14:05:58.560 20224-20224/com.dreamfighter.myapplication D/ActivityThread: ACT-LAUNCH_ACTIVITY handled : 0 / ActivityRecord{502a27e token=android.os.BinderProxy@b15c3df {com.dreamfighter.myapplication/com.dreamfighter.myapplication.MainActivity}} 08-21 14:05:58.583 20224-20247/com.dreamfighter.myapplication D/OpenGLRenderer: CanvasContext() 0x7f6b015000 initialize window=0x7f786df010, title=com.dreamfighter.myapplication/com.dreamfighter.myapplication.MainActivity 08-21 14:05:58.583 20224-20224/com.dreamfighter.myapplication D/Surface: Surface::allocateBuffers(this=0x7f786df000) 08-21 14:05:58.587 20224-20247/com.dreamfighter.myapplication I/OpenGLRenderer: Initialized EGL, version 1.4 08-21 14:05:58.590 20224-20247/com.dreamfighter.myapplication D/OpenGLRenderer: Created EGL context (0x7f7257d200) 08-21 14:05:58.592 20224-20247/com.dreamfighter.myapplication I/OpenGLRenderer: Get enable program binary service property (1) 08-21 14:05:58.592 20224-20247/com.dreamfighter.myapplication I/OpenGLRenderer: Initializing program atlas... 08-21 14:05:58.592 20224-20247/com.dreamfighter.myapplication D/ProgramBinary/Service: BpProgramBinaryService.getFileDescriptor 08-21 14:05:58.593 20224-20247/com.dreamfighter.myapplication D/ProgramBinary/Service: BpProgramBinaryService.getProgramMapLen 08-21 14:05:58.593 20224-20247/com.dreamfighter.myapplication D/ProgramBinary/Service: BpProgramBinaryService.getProgramMapArray 08-21 14:05:58.593 20224-20247/com.dreamfighter.myapplication D/ProgramBinary/Service: BpProgramBinaryService.getProgramBinaryLen 08-21 14:05:58.593 20224-20247/com.dreamfighter.myapplication I/OpenGLRenderer: Program binary detail: Binary length is 169616, program map length is 152. 08-21 14:05:58.594 20224-20247/com.dreamfighter.myapplication I/OpenGLRenderer: Succeeded to mmap program binaries. File descriptor is 38, and path is /dev/ashmemc. 08-21 14:05:58.594 20224-20247/com.dreamfighter.myapplication I/OpenGLRenderer: No need to use file discriptor anymore, close fd(38). 08-21 14:05:58.594 20224-20247/com.dreamfighter.myapplication D/OpenGLRenderer: Initializing program cache from 0x7f7c18bbb8, size = 4 08-21 14:05:58.596 20224-20247/com.dreamfighter.myapplication D/OpenGLRenderer: -- init (key = 0x0000000000000000) 08-21 14:05:58.598 20224-20247/com.dreamfighter.myapplication D/OpenGLRenderer: -- init (key = 0x0000000800000003) 08-21 14:05:58.599 20224-20247/com.dreamfighter.myapplication D/OpenGLRenderer: -- init (key = 0x0000001000500040) 08-21 14:05:58.600 20224-20247/com.dreamfighter.myapplication D/OpenGLRenderer: -- init (key = 0x0000003800000000) 08-21 14:05:58.602 20224-20247/com.dreamfighter.myapplication D/Surface: Surface::connect(this=0x7f786df000,api=1) 08-21 14:05:58.603 20224-20247/com.dreamfighter.myapplication W/libEGL: [ANDROID_RECORDABLE] format: 1 08-21 14:05:58.604 20224-20247/com.dreamfighter.myapplication D/mali_winsys: new_window_surface returns 0x3000 08-21 14:05:58.647 20224-20247/com.dreamfighter.myapplication D/OpenGLRenderer: CacheTexture 2 upload: x, y, width height = 0, 0, 145, 180 08-21 14:05:58.651 20224-20247/com.dreamfighter.myapplication I/[MALI][Gralloc]: [+]r_hnd(0x7f61e963c0), client(31), share_fd(39) 08-21 14:05:58.651 20224-20247/com.dreamfighter.myapplication D/GraphicBuffer: register, handle(0x7f61e963c0) (w:720 h:1280 s:720 f:0x1 u:0x000b00) 08-21 14:05:58.653 20224-20247/com.dreamfighter.myapplication D/OpenGLRenderer: ProgramCache save to disk, size = 4 08-21 14:05:58.657 20224-20224/com.dreamfighter.myapplication V/InputMethodManager: onWindowFocus: null softInputMode=288 first=true flags=#81810100 08-21 14:05:58.657 20224-20224/com.dreamfighter.myapplication V/InputMethodManager: START INPUT: com.android.internal.policy.PhoneWindow$DecorView{52ffb18 V.E...... R.....ID 0,0-720,1280} ic=null tba=android.view.inputmethod.EditorInfo@b24ea43 controlFlags=#104 08-21 14:05:58.671 20224-20247/com.dreamfighter.myapplication I/[MALI][Gralloc]: [+]r_hnd(0x7f61e96460), client(31), share_fd(41) 08-21 14:05:58.671 20224-20247/com.dreamfighter.myapplication D/GraphicBuffer: register, handle(0x7f61e96460) (w:720 h:1280 s:720 f:0x1 u:0x000b00) 08-21 14:06:00.489 20224-20247/com.dreamfighter.myapplication I/[MALI][Gralloc]: [+]r_hnd(0x7f61e96500), client(31), share_fd(42) 08-21 14:06:00.489 20224-20247/com.dreamfighter.myapplication D/GraphicBuffer: register, handle(0x7f61e96500) (w:720 h:1280 s:720 f:0x1 u:0x000b00) 08-21 14:06:00.505 20224-20247/com.dreamfighter.myapplication I/[MALI][Gralloc]: [+]r_hnd(0x7f61e96640), client(31), share_fd(43) 08-21 14:06:00.505 20224-20247/com.dreamfighter.myapplication D/GraphicBuffer: register, handle(0x7f61e96640) (w:720 h:1280 s:720 f:0x1 u:0x000b00) 08-21 14:06:00.519 20224-20224/com.dreamfighter.myapplication V/SettingsInterface: invalidate [system]: current 306 != cached 0 08-21 14:06:00.521 20224-20224/com.dreamfighter.myapplication D/ActivityThread: holder:android.app.IActivityManager$ContentProviderHolder@f38af4a, holder.provider:android.content.ContentProviderProxy@dc75ebb 08-21 14:06:00.525 20224-20224/com.dreamfighter.myapplication D/AndroidRuntime: Shutting down VM 08-21 14:06:00.526 20224-20224/com.dreamfighter.myapplication E/AndroidRuntime: FATAL EXCEPTION: main Process: com.dreamfighter.myapplication, PID: 20224 java.lang.IllegalStateException: Could not execute method for android:onClick at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293) at android.view.View.performClick(View.java:5274) at android.view.View$PerformClick.run(View.java:21543) at android.os.Handler.handleCallback(Handler.java:815) at android.os.Handler.dispatchMessage(Handler.java:104) at android.os.Looper.loop(Looper.java:207) at android.app.ActivityThread.main(ActivityThread.java:5728) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679) Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invoke(Native Method) at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) at android.view.View.performClick(View.java:5274)  at android.view.View$PerformClick.run(View.java:21543)  at android.os.Handler.handleCallback(Handler.java:815)  at android.os.Handler.dispatchMessage(Handler.java:104)  at android.os.Looper.loop(Looper.java:207)  at android.app.ActivityThread.main(ActivityThread.java:5728)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)  Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.dreamfighter.myapplication/com.dreamfighter.myapplication.second}; have you declared this activity in your AndroidManifest.xml? at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1805) at android.app.Instrumentation.execStartActivity(Instrumentation.java:1523) at android.app.Activity.startActivityForResult(Activity.java:3968) at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:48) at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:75) at android.app.Activity.startActivityForResult(Activity.java:3920) at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:856) at android.app.Activity.startActivity(Activity.java:4259) at android.app.Activity.startActivity(Activity.java:4227) at com.dreamfighter.myapplication.MainActivity.clickme(MainActivity.java:21) at java.lang.reflect.Method.invoke(Native Method)  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)  at android.view.View.performClick(View.java:5274)  at android.view.View$PerformClick.run(View.java:21543)  at android.os.Handler.handleCallback(Handler.java:815)  at android.os.Handler.dispatchMessage(Handler.java:104)  at android.os.Looper.loop(Looper.java:207)  at android.app.ActivityThread.main(ActivityThread.java:5728)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)  08-21 14:06:02.510 20224-20224/com.dreamfighter.myapplication I/Process: Sending signal. PID: 20224 SIG: 9

Vishal Yadav
  • 3,642
  • 3
  • 25
  • 42
  • 1
    change method name `click` to `clickme` as you declare in layout and also check `SecondActivty` is registered or not on `menofest.xml`. – D.J Aug 21 '17 at 09:26
  • have you declared second activity in your AndroidManifest.xml? – King of Masses Aug 21 '17 at 09:26
  • 2
    read this line in your logcat Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.dreamfighter.myapplication/com.dreamfighter.myapplication.second}; have you declared this activity in your AndroidManifest.xml? – Rihan Husain Aug 21 '17 at 09:33
  • Possible duplicate of [...have you declared this activity in your AndroidManifest.xml](https://stackoverflow.com/questions/15699192/have-you-declared-this-activity-in-your-androidmanifest-xml) – John Joe Aug 21 '17 at 09:40
  • Possible duplicate of [Activity Declaration in AndroidManifest.xml](https://stackoverflow.com/questions/19122386/activity-declaration-in-androidmanifest-xml) – Vishal Yadav Aug 21 '17 at 10:54
  • https://stackoverflow.com/questions/36502277/getting-illegalstateexception-on-button-click check this – kunal manocha Sep 21 '19 at 11:46

7 Answers7

1

Add your second.java in your Menifest file;

 <activity
        android:name=".second"
        android:label="@string/second"
        />
Vishal Vaishnav
  • 3,346
  • 3
  • 26
  • 57
1

Make sure you have declared the activity in your manifest:

<activity
    android:name=".second"
    android:theme="@style/AppTheme.NoActionBar" />

Use the theme which you are using in your app.

Vishal Yadav
  • 3,642
  • 3
  • 25
  • 42
Rihan Husain
  • 88
  • 10
1

Change this button layout with your layout, or you can replace only
android:onClick="clicme" with android:onClick="clickme":

<Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Button"
            android:id="@+id/button"
            android:layout_below="@+id/textView"
            android:layout_toRightOf="@+id/textView"
            android:layout_toEndOf="@+id/textView"
            android:layout_marginTop="134dp"
            android:onClick="clickme"
            />
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Amit Verma
  • 391
  • 2
  • 18
0

Check AndroidManifest.xml file. need to register each activity in AndroidManifest.xml file before use.

Here you need to register second.java in AndroidManifest.xml

Ganesh G
  • 1
  • 2
0

change this in your second activity by mistake your set the setContentView of main activity in your second activity

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);// change here
    b2 = (Button) findViewById(R.id.button2);
}

and make sure your added your activity in manifest file

 <activity
    android:name=".second"
    android:label="@string/second"
    />
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
0

Use Interface OnClickListener try this public class second extends AppCompatActivity instant of use public class second extends AppCompatActivity implements View.OnClickListener

     public class second extends AppCompatActivity  implements View.OnClickListener {
        Button b2;

            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                b2 = (Button) findViewById(R.id.button2);
                b2.setOnClickListener(this);

            }
          }

         @Override
            public void onClick(View view) {

                switch (view.getId())
                {
                    case R.id.button2:
                        Intent intent = new Intent(second.this,MainActivity.class);            
                        startActivity(intent);

                }

}

I hope this will help you,

Gowthaman M
  • 8,057
  • 8
  • 35
  • 54
0

Declare your second activity in manifest.

<activity android:name=".SecondActivity"/>
Francis Nduba Numbi
  • 2,499
  • 1
  • 11
  • 22