0

I was trying to make small application of two activities but it gives me a code error...............................................................................................................................................................................................................................................

the error

Main activity Java code:

package com.example.amr.startnewactivity;

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
{
    private Button op_btn;


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        onClickButtonListener();
    }
    public void onClickButtonListener()
    {
        op_btn= (Button)findViewById(R.id.button);
        op_btn.setOnClickListener(
                new View.OnClickListener()
                {
                    @Override
                    public void onClick(View v)
                    {
                        Intent intent = new Intent(".secondActivity");
                        startActivity(intent);

                    }
                }
        );
    }
}

Second Activity Java code:

package com.example.amr.startnewactivity;

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

public class secondActivity extends AppCompatActivity {

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

Android manifest.xml

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

    <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=".secondActivity"></activity>
        <intent-filter>
            <action android:name=".secondActivity" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </application>

</manifest>

Main activity.xml

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

    <Button
        android:id="@+id/button"
        android:layout_width="152dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="116dp"
        android:layout_marginLeft="116dp"
        android:layout_marginTop="248dp"
        android:text="open"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

Second activity .xml

<?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=".secondActivity">

</android.support.constraint.ConstraintLayout>
Amr Hassan
  • 31
  • 7
  • Check out this documentation on the official Android Developer docs which covers this very topic of starting another activity: https://developer.android.com/training/basics/firstapp/starting-activity – Edric Oct 02 '18 at 07:58

3 Answers3

1

add this code to your button click

Intent intent = new Intent(MainActivity.this,secondActivity.class);
startActivity(intent);

OR

startActivity(new Intent(MainActivity.this,secondActivity.class))

And try to follow the naming conventions in Java. Look at here

Sniffer
  • 1,495
  • 2
  • 14
  • 30
1

did you try this?

Intent intent = new Intent(MainActivity.this,secondActivity.class);
startActivity(intent);
Farrokh
  • 1,167
  • 1
  • 7
  • 18
0

What you're looking for is Intent(Context context, Class<?> class):

startActivity(new Intent(MainActivity.this, SecondActivity.class);

Or:

Intent secondActivityIntent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(secondActivityIntent);

For more information, check out the documentation ("Start another activity").


A second note: Consider removing the <intent-filter> from your AndroidManifest.xml file for secondActivity. This declares a filter for other apps to access programmatically.

In this case, you don't actually need it if you're using an Activity. This is typically used for Intents.

Edric
  • 24,639
  • 13
  • 81
  • 91