0

I'm trying to make an app that has multiple screens. I've made 2 XML files and 2 java files, but I can't find out where it's going wrong. Could anyone help me?

Here is xml file 1 (called activity_main).

    <ImageView
        android:id="@+id/marsfoto1"
        android:src="@drawable/marsfoto1"
        android:scaleType="centerCrop"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />


    <ImageView
        android:id="@+id/marsfoto2"
        android:src="@drawable/marsfoto2"
        android:scaleType="centerCrop"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/marsfoto1"/>

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/marsfoto1"
        android:padding="60dp"
        android:background="@null"/>

</RelativeLayout>  

Here is xml file 2 (called activity_main2).

<ImageView
    android:src="@drawable/marsgroot2"
    android:scaleType="centerCrop"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<TextView
    android:text="Mast Camera"
    android:textColor="@android:color/white"
    android:textSize="32sp"
    android:padding="8dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />



</RelativeLayout>

Here is java file 1 (called MainActivity)
package com.example.nasaroverapp;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends Activity {

    Button button;

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

    public void addListenerOnButton() {

        final Context context = this;

        button = (Button) findViewById(R.id.button1);

        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                Intent intent = new Intent(context, MainActivity2.class);
                startActivity(intent);

            }

        });

    }

}  

Here is java file 2 (called MainActivity2).
package com.example.nasaroverapp;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;

public class MainActivity2 extends MainActivity {

    Button button;

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

}

Could anyone please tell me what I'm doing wrong? I've been puzzling for over an hour and my app just keeps crashing when I press the button on activity_main.

Morteza Jalambadani
  • 2,190
  • 6
  • 21
  • 35

1 Answers1

0

Have you declared in your AndroidManifest.xml MainActivity2? Probably that is causing the crash

See documentation: https://developer.android.com/guide/topics/manifest/manifest-intro

You should have something like this in your AndroidManifest.xml

<manifest package="com.example.myapp" ... >
    <application ... >
        <activity android:name=".MainActivity2" ... >
            ...
        </activity>
        <activity android:name=".MainActivity" ... >
            ...
        </activity>
    </application>
</manifest>
denis_lor
  • 6,212
  • 4
  • 31
  • 55
  • Mark it as accepted answer so anyone can benefit :) I used to run into this problem often in my beginning in Android Development and it's now impressed in my DNA – denis_lor Mar 14 '19 at 11:06
  • 1
    Oh sorry I'm new to this website, so I didn't know I had to mark it. Thanks again! – Aydin Emanet Mar 14 '19 at 11:12