Error opening new Activity

Posted on

Question :

I’m picking up here to get you to click a button to open a new Activity.
I looked at the Android documentation on Activity, Intent, methods to create and yet, it does not work.

Basically, I did the following steps:

  • I created a new Android XML Layout File with the structure of the new Activity
  • I created a new class to control this new Activity
  • I went to AndroidManifest.xml and added this new Activity there.
  • I went to Main.java and set up the .onClickListener to fire Intent and open the screen.

But when you click on the button that triggers the ‘Second Activity’ call, the emulator hits.

Where is the problem?

Follow files for review:

MainActivity.java

package com.emanuel.teste;

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

public class MainActivity extends Activity {
 TextView lblSaldo;
 Button btSoma;
 Button btSub;
 Button btAjuda;
 int saldo;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    lblSaldo = (TextView) findViewById(R.id.tvSaldo);
    btSoma = (Button) findViewById(R.id.btSoma);
    btSub = (Button) findViewById(R.id.btSub);
    btAjuda = (Button) findViewById(R.id.btAjuda);
    btSoma.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
        saldo++;
        lblSaldo.setText("O saldo é: " +saldo);

        }
    });

    btSub.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
        saldo--;
        lblSaldo.setText("O saldo é: " +saldo); 

        }
    });

    btAjuda.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            Intent itAjuda = new Intent("com.emanuel.teste.ajuda");
            startActivity(itAjuda);
        }
    });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

help.java

This is the class that controls the screen I want to open

package com.emanuel.teste;

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

public class ajuda extends Activity{

Button btAjudaVoltar;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.ajuda);
    btAjudaVoltar = (Button)  findViewById(R.id.btAjudaVoltar);

    btAjudaVoltar.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            finish();

        }
    });
}
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns_android="http://schemas.android.com/apk/res/android"
    package="com.emanuel.teste"
    android_versionCode="1"
    android_versionName="1.0" >

<uses-sdk
    android_minSdkVersion="8"
    android_targetSdkVersion="18" />

<application
    android_allowBackup="true"
    android_icon="@drawable/ic_launcher"
    android_label="@string/app_name"
    android_theme="@style/AppTheme" >
    <activity
        android_name="com.emanuel.teste.MainActivity"
        android_label="@string/app_name" >
        <intent-filter>
            <action android_name="android.intent.action.MAIN" />

            <category android_name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity 
        android_name=".ajuda"
        android_label="@string/app_name">

        <intent-filter>
            <action android_name="com.emanuel.teste.AJUDA"/>
            <category android_name="android.intent.category.DEFAULT"/>

        </intent-filter>
    </activity>
</application>

</manifest>

Logcat

05-30 01:24:11.402: D/AndroidRuntime(333): Shutting down VM
05-30 01:24:11.402: W/dalvikvm(333): threadid=1: thread exiting with uncaught exception        (group=0x40015560)
05-30 01:24:11.423: E/AndroidRuntime(333): FATAL EXCEPTION: main
05-30 01:24:11.423: E/AndroidRuntime(333): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.emanuel.teste.ajuda }
05-30 01:24:11.423: E/AndroidRuntime(333):  at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1409)
05-30 01:24:11.423: E/AndroidRuntime(333):  at android.app.Instrumentation.execStartActivity(Instrumentation.java:1379)
05-30 01:24:11.423: E/AndroidRuntime(333):  at android.app.Activity.startActivityForResult(Activity.java:2827)
05-30 01:24:11.423: E/AndroidRuntime(333):  at android.app.Activity.startActivity(Activity.java:2933)
05-30 01:24:11.423: E/AndroidRuntime(333):  at com.emanuel.teste.MainActivity$3.onClick(MainActivity.java:53)
05-30 01:24:11.423: E/AndroidRuntime(333):  at android.view.View.performClick(View.java:2485)
05-30 01:24:11.423: E/AndroidRuntime(333):  at android.view.View$PerformClick.run(View.java:9080)
05-30 01:24:11.423: E/AndroidRuntime(333):  at android.os.Handler.handleCallback(Handler.java:587)
05-30 01:24:11.423: E/AndroidRuntime(333):  at android.os.Handler.dispatchMessage(Handler.java:92)
05-30 01:24:11.423: E/AndroidRuntime(333):  at android.os.Looper.loop(Looper.java:123)
05-30 01:24:11.423: E/AndroidRuntime(333):  at android.app.ActivityThread.main(ActivityThread.java:3683)
05-30 01:24:11.423: E/AndroidRuntime(333):  at java.lang.reflect.Method.invokeNative(Native Method)
05-30 01:24:11.423: E/AndroidRuntime(333):  at java.lang.reflect.Method.invoke(Method.java:507)
05-30 01:24:11.423: E/AndroidRuntime(333):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-30 01:24:11.423: E/AndroidRuntime(333):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-30 01:24:11.423: E/AndroidRuntime(333):  at dalvik.system.NativeStart.main(Native Method)

    

Answer :

Try changing the line:

Intent itAjuda = new Intent("com.emanuel.teste.ajuda");

for

Intent itAjuda = new Intent("com.emanuel.teste.AJUDA");

OR then call it like this:

Intent openStartingPoint = new Intent(MainActivity.this, AJUDA.class);

    

Leave a Reply

Your email address will not be published. Required fields are marked *