Android application = Unfortunately project_name has stopped

1

I'm a beginner on Android and have read some similar questions but could not solve my problem.

Running my app gives the error:

  

Unfortunately project_name has stopped.

MainActivity.java

package br.com.xxxxxxx.chatxxxxxx;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity extends AppCompatActivity {

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

        FloatingActionButton btnAdd = (FloatingActionButton) findViewById(R.id.btnAdd);
        btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent it = new Intent(MainActivity.this, CadastroActivity.class);
                startActivity(it);

            }
        });
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="br.com.xxxxxx.chatxxxxxx.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/btnAdd"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <EditText
            android:id="@+id/txtLoginEmail"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/textView"
            android:layout_marginTop="28dp"
            android:ems="10"
            android:hint="Digite seu E-mail"
            android:inputType="textEmailAddress" />

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="76dp"
            android:text="Acesso ao Chat" />

        <EditText
            android:id="@+id/txtLoginSenha"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/txtLoginEmail"
            android:layout_marginTop="21dp"
            android:ems="10"
            android:hint="Digite sua Senha"
            android:inputType="textPassword" />

        <Button
            android:id="@+id/btnLogar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentStart="true"
            android:layout_centerVertical="true"
            android:text="Entrar" />
    </RelativeLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        app:srcCompat="@android:drawable/ic_input_add" />

</android.support.design.widget.CoordinatorLayout>

CadastroActivity.java

package br.com.xxxxxxx.chatxxxxxx;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

public class CadastroActivity extends AppCompatActivity {

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

    }

}

activity_cadastro.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="br.com.xxxxxx.chatxxxxxx.CadastroActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>


    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:context="br.com.xxxxxx.chatxxxxxx.CadastroActivity"
        tools:showIn="@layout/activity_cadastro">

        <EditText
            android:id="@+id/txtNome"
            android:layout_width="357dp"
            android:layout_height="50dp"
            android:ems="10"
            android:hint="Nome"
            android:inputType="textPersonName"
            tools:layout_editor_absoluteX="9dp"
            tools:layout_editor_absoluteY="16dp" />

        <EditText
            android:id="@+id/txtEmail"
            android:layout_width="357dp"
            android:layout_height="50dp"
            android:ems="10"
            android:hint="E-mail"
            android:inputType="textEmailAddress"
            tools:layout_editor_absoluteX="9dp"
            tools:layout_editor_absoluteY="77dp" />

        <EditText
            android:id="@+id/txtSenha"
            android:layout_width="357dp"
            android:layout_height="50dp"
            android:ems="10"
            android:hint="Senha"
            android:inputType="numberPassword"
            tools:layout_editor_absoluteX="9dp"
            tools:layout_editor_absoluteY="143dp" />

        <EditText
            android:id="@+id/txtSenha2"
            android:layout_width="357dp"
            android:layout_height="50dp"
            android:ems="10"
            android:hint="Confirme a sua Senha"
            android:inputType="numberPassword"
            tools:layout_editor_absoluteX="9dp"
            tools:layout_editor_absoluteY="201dp" />

        <Button
            android:id="@+id/btnCasdastar"
            android:layout_width="357dp"
            android:layout_height="40dp"
            android:text="Cadastrar"
            tools:layout_editor_absoluteX="14dp"
            tools:layout_editor_absoluteY="404dp" />

        <ImageView
            android:id="@+id/imgFoto"
            android:layout_width="105dp"
            android:layout_height="126dp"
            app:srcCompat="@android:drawable/sym_def_app_icon"
            tools:layout_editor_absoluteX="16dp"
            tools:layout_editor_absoluteY="268dp" />
    </android.support.constraint.ConstraintLayout>


</android.support.design.widget.CoordinatorLayout>

AndroidManifest.xml

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

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

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".CadastroActivity"
            android:label="@string/title_activity_cadastro"
            android:theme="@style/AppTheme.NoActionBar"></activity>
    </application>

</manifest>

Log

11-04 20:04:12.902 1672-1688/system_process W/libprocessgroup: failed to open /acct/uid_10008/pid_2303/cgroup.procs: No such file or directory
11-04 20:04:12.905 1672-1740/system_process E/ConnectivityService: RemoteException caught trying to send a callback msg for NetworkRequest [ id=4, legacyType=-1, [ Capabilities: INTERNET&NOT_RESTRICTED&TRUSTED&NOT_VPN] ]
11-04 20:04:12.915 1672-1688/system_process I/ActivityManager: Start proc 5935:com.google.android.gms/u0a8 for broadcast com.google.android.gms/.stats.service.DropBoxEntryAddedReceiver
11-04 20:04:12.917 1672-1739/system_process D/WifiService: Client connection lost with reason: 4
11-04 20:04:12.970 1672-2884/system_process I/OpenGLRenderer: Initialized EGL, version 1.4
11-04 20:04:12.981 1672-2884/system_process D/EGL_emulation: eglCreateContext: 0x9b6eb100: maj 2 min 0 rcv 2
11-04 20:04:12.985 5935-5935/com.google.android.gms W/System: ClassLoader referenced unknown path: /system/priv-app/PrebuiltGmsCore/lib/x86
11-04 20:04:12.990 1672-2884/system_process D/EGL_emulation: eglMakeCurrent: 0x9b6eb100: ver 2 0 (tinfo 0x9d73f8a0)
11-04 20:04:13.026 5935-5935/com.google.android.gms I/LoadedApk: No resource references to update in package FFFFFFFFFFFFFFFFFFFFFF
11-04 20:04:13.084 2037-2206/com.android.launcher3 D/EGL_emulation: eglMakeCurrent: 0xae4537e0: ver 2 0 (tinfo 0xae452680)
11-04 20:04:13.111 1672-2884/system_process D/EGL_emulation: eglMakeCurrent: 0x9b6eb100: ver 2 0 (tinfo 0x9d73f8a0)
11-04 20:04:13.115 4798-4798/com.google.android.googlequicksearchbox:search I/BackgroundMemoryTrimmer: Trimming objects from memory, since app is in the background.
11-04 20:04:13.174 5935-5935/com.google.android.gms W/System: ClassLoader referenced unknown path: /system/priv-app/PrebuiltGmsCore/lib/x86
11-04 20:04:13.177 5935-5935/com.google.android.gms W/System: ClassLoader referenced unknown path: /system/priv-app/PrebuiltGmsCore/lib/x86
11-04 20:04:13.197 5935-5935/com.google.android.gms W/System: ClassLoader referenced unknown path: /system/priv-app/PrebuiltGmsCore/lib/x86
11-04 20:04:13.198 5935-5935/com.google.android.gms W/System: ClassLoader referenced unknown path: /system/priv-app/PrebuiltGmsCore/lib/x86
11-04 20:04:13.199 5935-5935/com.google.android.gms W/linker: /data/data/com.google.android.gms/app_extracted_libs/x86/libconscrypt_gmscore_jni.so: unused DT entry: type 0x1d arg 0x91
11-04 20:04:13.204 5935-5935/com.google.android.gms V/NativeCrypto: Registering com/google/android/gms/org/conscrypt/NativeCrypto's 259 native methods...
11-04 20:04:13.238 5935-5935/com.google.android.gms I/art: Rejecting re-init on previously-failed class java.lang.Class<com.google.android.gms.org.conscrypt.OpenSSLExtendedSessionImpl>
11-04 20:04:13.238 5935-5935/com.google.android.gms I/art: Rejecting re-init on previously-failed class java.lang.Class<com.google.android.gms.org.conscrypt.OpenSSLExtendedSessionImpl>
11-04 20:04:13.238 5935-5935/com.google.android.gms I/art: Rejecting re-init on previously-failed class java.lang.Class<com.google.android.gms.org.conscrypt.OpenSSLExtendedSessionImpl>
11-04 20:04:13.257 5935-5935/com.google.android.gms I/ProviderInstaller: Installed default security provider GmsCore_OpenSSL
11-04 20:04:13.346 1672-1688/system_process W/ActivityManager: Activity pause timeout for ActivityRecord{139a9b3 u0 br.com.xxxxxx.chatxxxxxx/.MainActivity t23 f}
11-04 20:04:13.536 1672-2256/system_process W/art: Long monitor contention event with owner method=void com.android.server.am.ActivityStack$ActivityStackHandler.handleMessage(android.os.Message) from ActivityStack.java:283 waiters=0 for 185ms
11-04 20:04:13.853 1672-1686/system_process I/AccountManagerService: getTypesVisibleToCaller: isPermitted? true
11-04 20:04:13.877 2037-2206/com.android.launcher3 W/OpenGLRenderer: Incorrectly called buildLayer on View: ShortcutAndWidgetContainer, destroying layer...
11-04 20:04:13.877 2037-2206/com.android.launcher3 W/OpenGLRenderer: Incorrectly called buildLayer on View: ShortcutAndWidgetContainer, destroying layer...
11-04 20:04:23.553 1672-1688/system_process W/ActivityManager: Activity destroy timeout for ActivityRecord{139a9b3 u0 br.com.xxxxxx.chatxxxxxx/.MainActivity t23 f}
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: br.com.bycelso.chatbycelso, PID: 6623
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{br.com.bycelso.chatbycelso/br.com.bycelso.chatbycelso.MainActivity}: java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.support.design.widget.FloatingActionButton
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                      at android.app.ActivityThread.-wrap11(ActivityThread.java)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      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.ClassCastException: android.widget.RelativeLayout cannot be cast to android.support.design.widget.FloatingActionButton
                      at br.com.bycelso.chatbycelso.MainActivity.onCreate(MainActivity.java:22)
                      at android.app.Activity.performCreate(Activity.java:6237)
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                      at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      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)
    
asked by anonymous 04.11.2017 / 19:51

1 answer

0

The problem is with your activity_main.xml

In your main class you are making the following reference to the floating button:

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

However this id in xml references a RelativeLayout :

//activity_main.xml    
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/btnAdd"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

The floating button has the following id :

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    app:srcCompat="@android:drawable/ic_input_add" />

To resolve, change your activity_main.xml code to this in the RelativeLayout section:

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

        <EditText
            android:id="@+id/txtLoginEmail"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/textView"
            android:layout_marginTop="28dp"
            android:ems="10"
            android:hint="Digite seu E-mail"
            android:inputType="textEmailAddress" />

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="76dp"
            android:text="Acesso ao Chat" />

        <EditText
            android:id="@+id/txtLoginSenha"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/txtLoginEmail"
            android:layout_marginTop="21dp"
            android:ems="10"
            android:hint="Digite sua Senha"
            android:inputType="textPassword" />

        <Button
            android:id="@+id/btnLogar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentStart="true"
            android:layout_centerVertical="true"
            android:text="Entrar" />
    </RelativeLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/btnAdd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        app:srcCompat="@android:drawable/ic_input_add" />

</android.support.design.widget.CoordinatorLayout>

Keep MainActivity.java your class as it was

    
04.11.2017 / 20:44