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
.