Android app => Unfortunately name_project has stopped

Asked

Viewed 321 times

1

I’m a beginner on Android and I’ve read some similar questions but I couldn’t solve my problem.

While running my app gives 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_cadastre.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)
  • Show your Manifest!

  • I’ll see where I find it.

  • Opa found. I inserted at the end of the code.

  • Now I’m going to need you to show the log in the log log in logcat... which generated the error...

  • I’ll try to see that.

  • 17:00 Executing tasks: [:app:assembleDebug] 17:00 Gradle build finished in 9s 6ms 17:07 Executing tasks: [:app:assembleDebug] 17:07 Gradle build finished in 7s 789ms

  • See error image: http://tinypic.com/r/x282uv/9

  • The monitor that is used to see the errors and the logcat, there you can filter and other things more... in it will have in detail what caused the error...

  • Do not paste the codes and log all mixed together, always organize your posts (I’ve already organized this one), formatting tips: https://answall.com/help/formatting

Show 4 more comments

1 answer

0


The problem is in your activity_main.xml

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

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

However that id in xml refers to 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 code from activity_main.xml for this, in the Relativelayout:

<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

  • I made the corrections:

  • Error has appeared yet?

  • Yes the same error, now I was able to enter the log. It is just below the published code.

  • I updated the log.

  • I believe you did not perform the update correctly see the error 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... is indicating that it was not possible to convert a Relativelayout for a Floatactionbutton

  • @Celsor updated the answer...

  • Boy you managed to make it work.... What joy... heheheheheh. Congratulations...

  • Funny that the registration page appears normal from the visual studio preview. But when I run it appears all the components in the same line, encavalando all.

  • I redid the Registration screen. Now it’s legal. Thank you.

  • @Celsor mark the answer as right, to close the question!

Show 5 more comments

Browser other questions tagged

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