Chanuka Sandeepa
Chanuka Sandeepa

Reputation: 720

Error "unable to find explicit activity class" when starting new Activity

I'm trying to start a activity from another activity. Finally, it doesn't work and fails with error of "Unable to find explicit activity class". So far, i was trying to find out a solution from previous questions but i was unable to find out a correct answer for my question.

This is my code,

activity_main.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="com.example.acer.explicitintent.MainActivity">

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Button" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</LinearLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {

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

        btn =(Button)findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent(MainActivity.this,ActivityTwo.class);
                i.putExtra("value","chanuka");
                startActivity(i);
            }
        });
    }
}

activity_two.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="TextView" />
</LinearLayout>

ActivityTwo.java

    public class ActivityTwo extends AppCompatActivity {
    TextView textView;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_two);

        textView = (TextView)findViewById(R.id.tv);
        Bundle bundle = getIntent().getExtras();
        textView.setText(bundle.getString("value"));
    }
}

Any help is appreciated. I'm newly to android.

Upvotes: 0

Views: 256

Answers (1)

Sachin Rajput
Sachin Rajput

Reputation: 4344

you have not defined your ActivityTwo inside your Manifest , thats why you are getting this error ,

define your ActivityTwo in your Manifest File.

Sample Code

 <application
        android:allowBackup="true"
        android:hardwareAccelerated="true"
        android:icon="@mipmap/ic_launcher"
        android:largeHeap="true"
        android:supportsRtl="true"
        android:theme="@style/AppTheme.NoActionBar">
        <!-- Other code for your activities etc-->
        <activity
            android:name=".activity.ActivityTwo"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme.NoActionBar"
            android:windowSoftInputMode="stateHidden" />

    </application>

Upvotes: 1

Related Questions