janu
janu

Reputation: 1

Activities are not working

I am new to android programming.i started working with multiple activities and run the app, when I click on the "Open Second Activity"/"Open Third Activity" Buttons, the Genymotion emulator says: Unfortunately, FirstApp has stopped. And returns to the android menu..why?i am getting error like this

java.lang.IllegalStateException: Could not execute method of the activity
  at android.view.View$1.onClick(View.java:4020)
  at android.view.View.performClick(View.java:4780)
  at android.view.View$PerformClick.run(View.java:19866)
  at android.os.Handler.handleCallback(Handler.java:739)
  at android.os.Handler.dispatchMessage(Handler.java:95)
  at android.os.Looper.loop(Looper.java:135)
  at android.app.ActivityThread.main(ActivityThread.java:5257)
  at java.lang.reflect.Method.invoke(Native Method)
  at java.lang.reflect.Method.invoke(Method.java:372)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.reflect.InvocationTargetException
  at java.lang.reflect.Method.invoke(Native Method)
  at java.lang.reflect.Method.invoke(Method.java:372)
  at android.view.View$1.onClick(View.java:4015)
  at android.view.View.performClick(View.java:4780) 
  at android.view.View$PerformClick.run(View.java:19866) 
  at android.os.Handler.handleCallback(Handler.java:739) 
  at android.os.Handler.dispatchMessage(Handler.java:95) 
  at android.os.Looper.loop(Looper.java:135) 
  at android.app.ActivityThread.main(ActivityThread.java:5257) 
  at java.lang.reflect.Method.invoke(Native Method) 
  at java.lang.reflect.Method.invoke(Method.java:372) 
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.jahnaviswaroop11gmail.firstapp/com.jahnaviswaroop11gmail.firstapp.SecondActivity}; have you declared this activity in your AndroidManifest.xml?
  at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1777)
  at android.app.Instrumentation.execStartActivity(Instrumentation.java:1501)
  at android.app.Activity.startActivityForResult(Activity.java:3745)
  at android.app.Activity.startActivityForResult(Activity.java:3706)
  at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:820)
  at android.app.Activity.startActivity(Activity.java:4016)
  at android.app.Activity.startActivity(Activity.java:3984)
  at com.jahnaviswaroop11gmail.firstapp.FirstActivity.showGreetings(FirstActivity.java:25)
  at java.lang.reflect.Method.invoke(Native Method) 
  at java.lang.reflect.Method.invoke(Method.java:372) 
  at android.view.View$1.onClick(View.java:4015) 
  at android.view.View.performClick(View.java:4780) 
  at android.view.View$PerformClick.run(View.java:19866) 
  at android.os.Handler.handleCallback(Handler.java:739) 
  at android.os.Handler.dispatchMessage(Handler.java:95) 
  at android.os.Looper.loop(Looper.java:135) 
  at android.app.ActivityThread.main(ActivityThread.java:5257) 
  at java.lang.reflect.Method.invoke(Native Method) 
  at java.lang.reflect.Method.invoke(Method.java:372) 
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

my FirstActivity.java

public class FirstActivity extends AppCompatActivity {
    TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.first_layout);
        textView = (TextView) findViewById(R.id.greetings_text_view);
    }
    public void showGreetings(View view){
       String button_text = ( (Button)  view).getText().toString();
        if (button_text.equals("Open Second Activity"))
        {
            Intent intent = new Intent(this,SecondActivity.class);

            startActivity(intent);
        }

        else if (button_text.equals("Open Third Activity"))
        {
            Intent intent = new Intent(this,ThirdActivity.class);
            startActivity(intent);

        }
    }

}

my SecondActvity.java

import android.app.Activity;
import android.os.Bundle;

public class SecondActivity extends Activity {
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_layout);
    }
}

my ThirdActivity.java

import android.app.Activity;
import android.os.Bundle;

public class ThirdActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.third_layout);

    }
}

my firstlayout.xml file

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="greetings appear here"
android:id="@+id/greetings_text_view"/>
<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    android:text="Open Second Activity"
    android:onClick="showGreetings"
    android:layout_below="@+id/greetings_text_view"

    android:id="@+id/button" />

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Open Third Activity"
    android:onClick="showGreetings"
    android:id="@+id/button2"
    android:layout_below="@+id/button"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="71dp" />

my secondlyout xml file

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Welcome to Second Activity"
    android:id="@+id/textView"
    android:layout_gravity="center_horizontal" />

my third layout xml file

 <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Welcome to third Activity"
    android:id="@+id/textView2"
    android:layout_gravity="center_horizontal" />

Upvotes: 0

Views: 136

Answers (4)

Vyacheslav
Vyacheslav

Reputation: 27211

Androidmanifest.xml has got incorrect data, you have to add information about second and third activities

    <activity 
        android:name=".SecondActivity"/>
    <activity
        android:name=".ThirdActivity"/>

Upvotes: 0

sadiqmc
sadiqmc

Reputation: 171

Please verify the Exception stack trace before posting a question. The exception clearly mentions the cause for the issue..

Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.jahnaviswaroop11gmail.firstapp/com.jahnaviswaroop11gmail.firstapp.SecondActivity}; have you declared this activity in your AndroidManifest.xml?

Add the SecondActvity to your AndroidManifest.xml inside <application> tag

<activity android:name="com.jahnaviswaroop11gmail.firstapp.SecondActivity" />

Upvotes: 1

Arjun saini
Arjun saini

Reputation: 4182

You need to put the both First and second activity to you manifest ...

<activity android:name=".SecondActivity"/>

<activity android:name=".ThirdActivity"/>

and also get text

Button button = (Button)view;

String button_text= button.getText().toString();

and also add the permission

 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

Upvotes: 0

SripadRaj
SripadRaj

Reputation: 1735

Add this to your manifest inside <application> tag. It should solve your problem.

<activity android:name="com.jahnaviswaroop11gmail.firstapp.SecondActivity"/>

Upvotes: 0

Related Questions