Error opening a new Activity on mobile - Android Studio

1

Good morning everyone !!! So I'm new to programming for Android, I happen to have created 3 screens:

  • Home
  • Categories
  • Attitude

The main one is just 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 phrases that will be available.

I can not open a virtual device inside Android Studio, so I can not log in while running, it's directly on my phone!

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

The main code below

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

CategoryScreen

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

    }
}

Attitude Screen

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 someone can help me, because I have 2 days that I have this problem and I can not 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 Category

<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 Attitude

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

    
asked by anonymous 19.07.2018 / 17:43

1 answer

1

So I noticed you are trying to open an activity with a "setContentView" inside the OnClick method, to open an activity you should:

Your error:

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 one to open an activity:

Intent intent = new Intent(getApplicationContext(), SuaActivity.class);
startActivity(intent); //inicia a activity
finish(); //finaliza a activity atual
    
26.07.2018 / 14:54