Reputation: 31
I have a project with two package first.pack and second.pack, with two different Activity. I'm trying to start the second Activity from the first one with that code
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setComponent(new ComponentName("second.pack", "second.pack.SecondActivity"));
startActivity(intent);
The strange thing is that this code had worked for a while, but after i unistalled and reinstalled the application, it's started to return me the following error:
Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {second.pack/second.pack.SecondActivity}; have you declared this activity in your AndroidManifest.xml?
Where am i wrong?
Upvotes: 1
Views: 1962
Reputation: 79
When there are different packages in an application, do the following first:
<activity android:name=".Kunal.Android" />
Declare that activity in the manifest.xml.
Intent intent = new Intent(MainActivity.this,Second package name.class );
Following is my code:
switch (position) {
case 0:
Intent newActivity = new Intent(MainActivity.this,com.example.owner.listview_1.Kunal.Android.class);
startActivity(newActivity);
break;
case 1:
Intent newActivity1 = new Intent(MainActivity.this, com.example.owner.listview_1.Kunal.Iphone.class);
startActivity(newActivity1);
break;
case 2:
Intent newActivity2 = new Intent(MainActivity.this,com.example.owner.listview_1.Kunal.Window.class);
startActivity(newActivity2);
break;
}
Upvotes: 0
Reputation: 1768
You have one project with 2 packages, but how many applications are there in the project? Let's assume there is only one application. The first parameter of ComponentName is the application's package name, irregarding the package name of the activity you want to call.
So if you have only one application whose package is "first.pack", from which you want to call an activity of a package "second.pack", then the correct call is:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setComponent(new ComponentName("first.pack", "second.pack.SecondActivity"));
startActivity(intent);
One possible reason for it stopped working may be that you changed the application's package (e.g. from "second.pack" to "first.pack").
Upvotes: 0
Reputation: 473
Make sure in the AndroidManifest.xml file that the activity is already defined like this:
<activity
android:name="second.pack.SecondActivity"
android:label="@string/yor_title"/>
Upvotes: 1