MrZed411 MrZed411 - 6 months ago 16
Java Question

Cannot switch activities in Android Studio except one activity

So I have been making a simple four button app for my IT 1 class and have been having a problem with some code. I have four buttons that are supposed to call a startActivity and change to a different screen. The first button, labeled fire, works perfectly fine and works 100% of the time. The next button, labeled Earthquake doesn't work, even though the code is the same (without the parts that need to change). The other 2 buttons also do not work. I have tried making a new project, copying the working xml, copying the exact code. I cannot figure out why it doesn't work.

MainActivity XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#81848b"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.carrieowen.testproject.MainActivity">

<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true">

<TextView
android:id="@+id/myText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#eff2f9"
android:text="Emergency Drills"
android:gravity="center"
android:textSize="32sp"
/>

<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:padding="40dp"
android:text="Fire"
android:background="#e71e1e"
android:id="@+id/button"
android:onClick="Fire"/>

<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#009c12"
android:padding="40dp"
android:text="Earthquake"
android:id="@+id/button2"
android:onClick="Earthquake"/>

<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#eef035"
android:padding="40dp"
android:text="Tornado"
android:id="@+id/button3"
android:onClick="Tornado"/>

<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#1b4fdd"
android:padding="40dp"
android:text="Code Blue"
android:id="@+id/button4"
android:onClick="CodeBlue"/>

</LinearLayout>
</RelativeLayout>


The MainActivity Java

package com.example.carrieowen.testproject;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity {

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


public void Fire (View view){
Intent changeScreenFire = new Intent(this, firescreen.class);
startActivity(changeScreenFire);
}

public void Earthquake (View view){
Intent changeScreenEarthquake = new Intent(this, earthquake.class);
startActivity(changeScreenEarthquake);
}
public void Tornado (View view){
Intent changeScreenTornado = new Intent(this, tornado.class);
startActivity(changeScreenTornado);
}
public void CodeBlue (View view){
Intent changeScreenCodeBlue = new Intent(this, codeblue.class);
startActivity(changeScreenCodeBlue);
}

}


The FireScreen Java

package com.example.carrieowen.testproject;

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

public class firescreen extends AppCompatActivity {

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


The Earthquake Java

package com.example.carrieowen.testproject;

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

public class earthquake extends AppCompatActivity {

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


The XML for the Fire and Earthquake are the same other than the Text boxes being different. If needed will add.

Answer

Make sure your activities are listed in the manifest? The manifest file is called AndroidManifest.xml and is listed in the project at the top, in android studio. This is what the content should look like:

<?xml version="1.0" encoding="utf-8"?>

<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=".Tornado">
    </activity>
    <activity android:name=".CodeBlue">
    </activity>
</application>