Activity is not displayed

2

Good morning, I am learning to program in java with the Android Studio tool ... I am trying to call another activity through a button but in the emulator, when I press the button, the application closes and the IDE gives me this error

FATAL EXCEPTION: main
Process: andresk21.com.speedex, PID: 3161
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:5198)
   at android.view.View$PerformClick.run(View.java:21147)
   at android.os.Handler.handleCallback(Handler.java:739)
   at android.os.Handler.dispatchMessage(Handler.java:95)
   at android.os.Looper.loop(Looper.java:148)
   at android.app.ActivityThread.main(ActivityThread.java:5417)
   at java.lang.reflect.Method.invoke(Native Method)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
 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:5198) 
   at android.view.View$PerformClick.run(View.java:21147) 
   at android.os.Handler.handleCallback(Handler.java:739) 
   at android.os.Handler.dispatchMessage(Handler.java:95) 
   at android.os.Looper.loop(Looper.java:148) 
   at android.app.ActivityThread.main(ActivityThread.java:5417) 
   at java.lang.reflect.Method.invoke(Native Method) 
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
 Caused by: java.lang.NumberFormatException: Invalid int: ""
   at java.lang.Integer.invalidInt(Integer.java:138)
   at java.lang.Integer.parseInt(Integer.java:358)
   at java.lang.Integer.parseInt(Integer.java:334)
   at andresk21.com.speedex.MainActivity.llamarSegundoActiviy(MainActivity.java:29)
   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:5198) 
   at android.view.View$PerformClick.run(View.java:21147) 
   at android.os.Handler.handleCallback(Handler.java:739) 
   at android.os.Handler.dispatchMessage(Handler.java:95) 
   at android.os.Looper.loop(Looper.java:148) 
   at android.app.ActivityThread.main(ActivityThread.java:5417) 
   at java.lang.reflect.Method.invoke(Native Method) 
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

And I used the following code in the class:

public class MainActivity extends AppCompatActivity{

    Button boton;
    EditText paquete;
    Integer numPaquete;
    String texto;

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

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

I send it to call in the property of the button onClick :

  

android: onClick="callSecondActiviy"

As I already mentioned, I am in the first steps of learning in this tool and platform, thank you in advance.

I have already declared it in the manifest

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".ScrollingActivity"
            android:label="@string/title_activity_scrolling"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN2" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

I add the ScrollingActivity ...

package andresk21.com.speedex;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;

public class ScrollingActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scrolling);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);

    }
}
    
asked by Andres Henriquez 11.09.2017 в 01:42
source

2 answers

4

The call of your method from the view is correct:

android:onClick="llamarSegundoActiviy"

This method performs an Intent for cargarScrollingActivity :

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

but an error arises and the message is:

java.lang.IllegalStateException: Could not execute method for android:onClick

In this case, it is not the definition of the method or the method call itself, in this case it is not necessary to define an intent-filter in the class ScrollingActivity :

    <activity android:name=".ScrollingActivity"
        android:label="@string/title_activity_scrolling"
        android:theme="@style/AppTheme.NoActionBar">
    </activity>

Also ensures ScrollingActivity extend of Activity, AppCompatActivity, etc ...

    
answered by 11.09.2017 / 18:51
source
2

The error is in <intent-filter> of activity ScrollingActivity , delete it. To open an activity that label is not necessary. It has other more specific uses, but it is not necessary to open an activity. For example, in the MainActivity you use it to define it as the default activity, and there can not be two activities by default. That is the origin of the error.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.example">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

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

    </application>

</manifest>
    
answered by 11.09.2017 в 02:11