Error opening new Activity

Asked

Viewed 1,651 times

3

I’m picking up here to be able to make the click on a button, open a new Activity. I looked at the Android documentation on Activity, Intent, the methods to create and yet yes, it doesn’t 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 add this new Activity there.
  • I went to Main.java and configured . onClickListener to fire the Intent and open the screen.

But, by clicking the button that triggers the call of 'second Activity', the emulator takes.

Where is the problem?

Follow files for analysis:

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

}

java help.

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)

1 answer

8


Try changing the line:

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

for

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

OR so call it:

Intent openStartingPoint = new Intent(MainActivity.this, AJUDA.class);
  • Man. I tested here with these changes suggested by you and it worked right. Thank you so much for your help!!

  • Wonder! Mark as an answer to help those who have the same problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.