Error opening a new Activity on mobile - Android Studio

Asked

Viewed 214 times

1

Good morning, everyone!! So, I’m new in programming for Android, it turns out I created 3 screens:

  • Leading
  • Categories
  • Attitude

The main one is only for it to go either in Categories or Exit.

The category he will choose in which of the categories of phrases he wants to see.

Attitude is one of the categories of sentences that will be available.

I’m not able to open on a virtual device inside Android Studio, so I’m not getting the log while running, it’s directly on mobile!

It opens from Main to the Category screen, but the Attitude Category stops working and closes the app

Below is the code of the main

package com.example.ensapp.ensfrases;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btn_Categorias = findViewById(R.id.btn_Categorias);
        btn_Categorias.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                setContentView(R.layout.activity_main_categorias);
            }
        });
    }
    protected void finish (View view){
        finish();
    }
}

Telacategory

package com.example.ensapp.ensfrases;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainCategorias extends AppCompatActivity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_categorias);
    }
    public void Main(View v)
    {
        setContentView(R.layout.activity_tela_atitude);

    }
}

Tela Atitude

package com.example.ensapp.ensfrases;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class telaAtitude extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tela_atitude);
    }
    public void mainCategoria(View v)
    {
        setContentView(R.layout.activity_main_categorias);
    }
}

If anyone can help me, because it’s been two days that I have this problem and I can’t fix it...

Thank you in advance!

Main Activity

<TextView
    android:id="@+id/sample_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<Button
    android:id="@+id/btn_Categorias"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:onClick="mainCategorias"
    android:text="Categorias"
    app:layout_constraintBottom_toTopOf="@+id/sample_text"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.501"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.824" />

<Button
    android:id="@+id/btn_Sair"
    android:layout_width="107dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="136dp"
    android:layout_marginStart="136dp"
    android:onClick="finish"
    android:text="Sair"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/sample_text"
    app:layout_constraintVertical_bias="0.232" />

Activity Categoria

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:onClick="Main"
    android:text="Button"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.313" />

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:onClick="Main"
    app:layout_constraintBottom_toTopOf="@+id/button"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.69"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.442" />

Activity Atitude

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:onClick="mainCategoria"
    android:text="Button"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.29" />

Android Manifest XML

<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">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

  • I would advise in this case the use of Fragments, can post the full code of activitys ? and if possible the XML files as an example because the presented code are apparently identical.

  • How are you running the app on mobile? If you install via usb and keep the cable connected, you can follow the error logs in Android Studio.

1 answer

1


From what I noticed you are trying to open an Activity with a "setContentView" inside the Onclick method, to open an Activity you must:

Your mistake:

public class Mainactivity extends Appcompatactivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button btn_Categorias = findViewById(R.id.btn_Categorias);
    btn_Categorias.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            setContentView(R.layout.activity_main_categorias);
        }
    });
}
protected void finish (View view){
    finish();
}

}

The correct to open an Activity:

Intent intent = new Intent(getApplicationContext(), SuaActivity.class);
startActivity(intent); //inicia a activity
finish(); //finaliza a activity atual
  • Oops, thank you very much, solved the problem!!! : ) Thanks a lot

  • Good that it helped, marks the answer as accepted that so others can see that it worked!

Browser other questions tagged

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