Search code examples
androideclipseandroid-4.2-jelly-bean

Crashing application using Intents


I am trying to create an activity that has a button , clicking on which another activity starts.But when i run the app on android AVD it show an error "Unforunately your app has stopped".

package com.example.myapp;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.content.Intent;

public class MainActivity extends Activity implements View.OnClickListener {

    Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn=(Button)findViewById(R.id.button1);
        btn.setOnClickListener(this);
    }

    public void onClick(View view){
        startActivity(new Intent("com.example.myapp.SecondActivity.java"));
    }
}

And the second activity has the following code:

package com.example.myapp;

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

public class SecondActivity extends Activity {

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

LogCat

FATAL EXCEPTION: main >android.content.ActivityNotFoundException: 
    No Activity found to handle Intent
    { act=com.example.simpleapp.SecondActivity.java } 
android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1622) 
android.app.Instrumentation.execStartActivity(Instrumentation.java:1417) 
android.app.Activity.startActivityForResult(Activity.java:3370) 
com.example.simpleapp.MainActivity.onClick(MainActivity.java:22) 
android.view.View.performClick(View.java:4204) 
android.view.View$PerformClick.run(View.java:17355)

I think i might be missing some statement ..... Please advise


Solution

  • From the documentation :

    public Intent (Context packageContext, Class<?> cls)

    Create an intent for a specific component. All other fields (action, data, type, class) are null, though they can be modified later with explicit calls. This provides a convenient way to create an intent that is intended to execute a hard-coded class name, rather than relying on the system to find an appropriate class for you; see setComponent(ComponentName) for more information on the repercussions of this.

    Parameters

    packageContext
    A Context of the application package implementing this class.
    
    cls
    The component class that is to be used for the intent.
    

    Code :

    public void onClick(View view){
                 startActivity(new Intent(MainActivity.this, SecondActivity.class));
                  }