-2

I'm trying to make it so when the user presses a button on main activity screen, it will take them to another activity screen (called create). but when I run my app and click the button, the app just goes black then goes to my phone home screen.

Here's my Main activity code:

 package com.app.supermarketaislefinder;

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

public class MainActivity extends AppCompatActivity {


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

        Button btn = (Button)findViewById(R.id.buttonCreate);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, create.class));
            }
        });

        }

        }

This is the XML Code here:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:background="@drawable/paper">


    <Button
        android:id="@+id/buttonSettings"
        android:layout_width="155dp"
        android:layout_height="62dp"
        android:layout_marginBottom="16dp"
        android:layout_marginTop="344dp"
        android:background="@drawable/settingsbutton_border"
        android:fontFamily="@font/bree_serif"
        android:gravity="center"
        android:text="Settings"
        android:textSize="18sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.502"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/linearLayout"
        app:layout_constraintVertical_bias="0.0" />

    <Button
        android:layout_width="155dp"
        android:layout_height="66dp"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp"
        android:background="@drawable/viewbutton_border"
        android:fontFamily="@font/bree_serif"
        android:gravity="center"
        android:text="View your Shopping LIst"
        android:textSize="18sp"
        app:layout_constraintBottom_toTopOf="@+id/buttonSettings"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.502"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/buttonCreate" />

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="384dp"
        android:layout_height="105dp"
        android:layout_marginEnd="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginStart="16dp"
        android:background="@color/colorMain"
        android:orientation="vertical"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:id="@+id/appTitle"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fontFamily="@font/bree_serif"
            android:gravity="center"
            android:text="Supermarket Aisle Finder"
            android:textColor="@android:color/black"
            android:textSize="36sp" />
    </LinearLayout>

    <Button
        android:id="@+id/buttonCreate"
        android:layout_width="155dp"
        android:layout_height="66dp"
        android:layout_marginTop="72dp"
        android:background="@drawable/button_border"
        android:fontFamily="@font/bree_serif"
        android:gravity="center"
        android:text="Create your Shopping List"
        android:textSize="18sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.502"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/linearLayout" />
</android.support.constraint.ConstraintLayout>

Here's the manifest code.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.app.supermarketaislefinder">

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

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".create" />
        <activity android:name=".view" />
        <activity android:name=".settings"></activity>
    </application>

</manifest>

2 Answers2

0

Try this code and change the name of button or class as per your project.

public class MainActivity extends AppCompatActivity {
Button buttonCreate;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    buttonCreate = (Button)findViewById(R.id.buttonCreate);

    buttonCreate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(MainActivity.this, Create.class));
        }
    });

}
}

Replace your XML code with this in this code I am not getting any error .

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
>


<Button
    android:id="@+id/buttonSettings"
    android:layout_width="155dp"
    android:layout_height="62dp"
    android:layout_marginBottom="16dp"
    android:layout_marginTop="344dp"
    android:gravity="center"
    android:text="Settings"
    android:textSize="18sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.502"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/linearLayout"
    app:layout_constraintVertical_bias="0.0" />

<Button
    android:layout_width="155dp"
    android:layout_height="66dp"
    android:layout_marginBottom="8dp"
    android:layout_marginTop="8dp"
    android:gravity="center"
    android:text="View your Shopping LIst"
    android:textSize="18sp"
    app:layout_constraintBottom_toTopOf="@+id/buttonSettings"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.502"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/buttonCreate" />

<LinearLayout
    android:id="@+id/linearLayout"
    android:layout_width="384dp"
    android:layout_height="105dp"
    android:layout_marginEnd="16dp"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:layout_marginStart="16dp"
    android:orientation="vertical"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <TextView
        android:id="@+id/appTitle"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="Supermarket Aisle Finder"
        android:textColor="@android:color/black"
        android:textSize="36sp" />
</LinearLayout>

<Button
    android:id="@+id/buttonCreate"
    android:layout_width="155dp"
    android:layout_height="66dp"
    android:layout_marginTop="72dp"
    android:gravity="center"
    android:text="Create your Shopping List"
    android:textSize="18sp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.502"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/linearLayout" />

niya panchal
  • 91
  • 1
  • 8
  • Hi. I tried that, but still got this error '06-06 17:16:15.864 9316-9316/com.app.supermarketaislefinder A/libc: Fatal signal 11 (SIGSEGV) at 0x00000000 (code=1), thread 9316 (rketaislefinder) ' –  Jun 06 '18 at 05:16
  • Yes thank you. But I'm trying to add the colours to the buttons again –  Jun 06 '18 at 06:12
-1

Is the activity declared in your manifest file?

TomH
  • 2,581
  • 1
  • 15
  • 30