ActivityNotFoundException: No Activity found to handle Intent - android

In fear of being that guy that asks the question that has been asked already.....*I have looked through the various solutions to this problem, and none seem to solve my problem.
I simply have a button that I would like to start a map activity.
Below is the activity:
public void OnclickButtonListener() {
button_map = (Button)findViewById(R.id.button);
button_map.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent mapintent = new Intent("mf.tutorial.MapsActivity");
startActivity(mapintent);
}
});
and the intent being handled in my manifest file:
<activity
android:name=".MapsActivity"
android:label="#string/title_activity_maps">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
The error returned looks like this:
ActivityNotFoundException: No Activity found to handle Intent { act=mf.tutorial.MapsActivity }
Any help is really appreciated as it has taken me ages to get this far and this is just the latest stumbling block. I would be happy to provide any other parts of the code that might be relevant for a solution!
Thank you very much!

I'm assuming MapsActivity is an activity in your apk? In that case, you would use Intent intent = new Intent(context, MapsActivity.class); The version of Intent you used is for running an activity that has an intent filter for a specific action. Usually used to access system apps like the camera app or the dialer.

When you try to launch your Activity, you do this:
Intent mapintent = new Intent("mf.tutorial.MapsActivity");
This creates an Intent and sets the ACTION in that Intent to "mf.tutorial.MapsActivity".
When you call startActivity(), Android tries to find an Activity in an installed application that knows how to deal with the ACTION "mf.tutorial.MapsActivity". There aren't any, so you get the ActivityNotFoundException.
What you are want to do is to launch the Activity by explicit COMPONENT, and NOT by implicit ACTION. Do that like this:
Intent mapintent = new Intent(context, MapsActivity.class);
startActivity(mapintent);
Using the 2-argument constructor allows you to set the COMPONENT (in this case, MapsActivity) explicitly. The parameter context should be a reference to the calling Activity.

Related

Android Starting an Activity from different project

I have 2 projects in Eclipse. When I click on a button in project1, I want it to launch an activity from project2.
how can I do that ?
i tried the following code
Intent intent = new Intent(this,com.project2.tp02.MainActivity.class);
intent.setClassName("com.project2.tp02", "com.project2.tp02.MainActivity.class");
startActivity(intent);
I get an error message telling that first application stopped. How can I lauch it properly without such error?
Thanks in advance to any helper.
I got it work using this :
Intent intent = new Intent();
String pkg ="com.project2.tp02";
String clazz =pkg + ".MainActivity";
intent.setComponent(new ComponentName(pkg, clazz));
startActivity(intent);
I don't understand why that way it works and not previous one, if someone can explain I will be thankfull, but anyway I got my problem solved that way.
This is working code to invoke an activity which is in another project.
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.my.package", "com.my.package.MyClass"));
startActivity(intent);
I think your first code did not work because you provide your current activity as context of invoked activity where it is in another application.
<intent-filter>
<action android:name="com.project2.tp02.MainActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
Adding the above lines in the Manifest of your first activity might solve your problem.
It would be interesting what the error message is saying exactly... you do need the intent-filter as described by Swayam on the activity you want to call.
Try
new Intent("com.project2.tp02.MainActivity");
to get the intent, instead of setting the class name.

Launch Main Activity with Implicit Intent in Service

I have an implicit intent in a service that sends information to my main activity, as well as to another class. I also now want that intent to launch my main activity. I've looked at the myriad posts related to this, and tried lots of different things--addCategory, setAction(MAIN; the activity's name; you name it, I've tried it...), category.DEFAULT in the manifest, and several other things that either resulted in ActivityNotFoundExceptions (most commonly) or behavior that was otherwise undesirable.
Here's where the intent is set up and the relevant part of the manifest. The receiver for the intent is registered in the main activity.
final String NEW_DOSES = "changed to protect the innocent";
Intent bluetoothBroadcast = new Intent();
several putExtra lines here
bluetoothBroadcast.setAction(NEW_DOSES);
sendBroadcast(bluetoothBroadcast);
<activity
android:name=".AsthmaAppActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Is it possible to get this intent to launch my main activity with relatively minor changes? Thanks in advance.
Yes it is possible but no with sendBroadcast(bluetoothBroadcast); sendBroadcast does not launch an activity. You must use startActivity to achieve this. For example here is what a launcher application will do in order to launch an application:
public static void LaunchApplication(Context cx, String packagename) {
PackageManager pm = cx.getPackageManager();
Intent i = pm.getLaunchIntentForPackage(ai.packageName);
if (i != null) cx.startActivity(i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
}
You can easily adjust the extras and the data needed in order to launch the activity. For example if your activity is named myActivity then you can go like this:
Intent i = new Intent(cx, myActivity.class);
//Put the extras and the data you want here...
//If you are launching the activity from a receiver component you must use
//i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
cx.startActivity(i);
Hope this helps...

Android intent syntax

In my attempts to find out how to start a new intent in my app, I've come across several ways of phrasing it.
This syntax returns a runtime error, namely a ActivityNotFound exception
Intent in = new Intent("com.something.something");
Of course my android manifest contains an action within the intent filter:
<activity
android:name=".SecondActivity"
android:label="#string/title_activity_second" >
<intent-filter>
<action android:name="com.something.something" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
This formatting works:
Intent in = new Intent(MainActivity.this, SecondActivity.class);
I also tried the following:
Intent in = new Intent(this, SomeActivity.class);
that was recommended in a book I'm reading. This returns a runtime error, activitynotfound
This one makes Eclipse throw me back and forth between setClass and setClassName infinitely:
Intent in = new Intent().setClass(this, SecondActivity.class);
I'm using it in an onclick method:
ok.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Intent in = new Intent(MainActivity.this, SecondActivity.class);
startActivity(in);
}
});
}
What's the difference between these and why is only one of them working for me?
Regards
/M
Basic information about Intent resolution
Intents can contain the following basic information:
ACTION
CATEGORY
DATA
COMPONENT
There are 2 ways that Intents are resolved by the system:
Explicit (uses COMPONENT if it is specified)
Implicit (uses ACTION, CATEGORY and DATA to find suitable activity)
If you specify the component (package name and class name) then this is used to explicitly find the activity you have specified and the Intent is sent to that activity. The other Intent data is not used (although it is passed to the called activity in the Intent). This is called "explicit Intent resolution".
If you don't specify the component, then the ACTION, CATEGORY and DATA fields are used to locate one or more activities that advertise (via intent-filter) that they can accept the Intent. This is called "implicit Intent resolution".
To your specific questions
When you do this:
Intent in = new Intent("com.something.something");
You are creating an implicit Intent and setting the ACTION to "com.something.something". If you then call startActivity() with this Intent, you get ActivityNotFoundException because Android cannot find an activity that can accept an Intent with ACTION="com.something.something". The reason is because you have provided an intent-filter with ACTION="com.something.something" and CATEGORY="android.intent.category.LAUNCHER" but you haven't specified the CATEGORY in your Intent (Android automatically adds the CATEGORY "DEFAULT" to an Intent if there isn't any CATEGORY specified when using startActivity()). To make this work you should either
Replace CATEGORY="android.intent.category.LAUNCHER" with CATEGORY="android.intent.category.DEFAULT" or
Add <category android:name="android.intent.category.DEFAULT" />
to the intent-filter for SecondActivity
When you do this:
Intent in = new Intent(MainActivity.this, SecondActivity.class);
You are creating an explicit Intent specifying the component SecondActivity. The signature for this method is Intent(Context packageContext, Class clas). It uses the package name from packageContext and the class name from clas to create an explicit Intent for that component. If you use this constructor inside an Activity, you can just use this as the first parameter, because Activity extends Context. If you use this constructor from another class (like an OnClickListener) you need to specify MyActivity.this as the first parameter to pass the instance of the Activity and not of the OnClickListener (because OnClickListener does not extend Context).
When you do this:
Intent in = new Intent().setClass(this, SecondActivity.class);
you are creating an explicit Intent as above. This is exactly the same as using:
Intent in = new Intent(this, SecondActivity.class);
You can't do this inside an OnClickListener because the first parameter needs to be a Context (or a class that extends Context, like Activity).
If you want to create an explicit Intent you can also use this:
Intent in = new Intent().setClassName("com.something", "com.something.SecondActivity");
This creates an explicit intent, but you don't need a Context for this. You can just pass the package name and the class name as Strings (if you know them).
For more information about Intent resolution, see:
http://developer.android.com/guide/components/intents-filters.html
http://developer.android.com/reference/android/content/Intent.html
http://developer.android.com/reference/android/content/IntentFilter.html
Please note, when you use "this" key word in "Intent in = new Intent(this, SomeActivity.class); " make sure you are not in a sub class of MainActivity this can set the wrong Activity as part of the Explicit intent Component.strong text

Start Activity Using Custom Action

I am looking to start an activity in my app using a custom action. I have found a few answers but everything I try it throws java.lang.RuntimeException saying No Activity found to handle Intent { act=com.example.foo.bar.YOUR_ACTION }.
This is the activity in my manifest file:
<activity
android:name=".FeedbackActivity" >
<intent-filter>
<action android:name="com.example.foo.bar.YOUR_ACTION" />
</intent-filter>
</activity>
And this is how I'm starting the activity:
Intent intent = new Intent("com.example.foo.bar.YOUR_ACTION");
startActivity(intent);
Any help would be greatly appreciated.
I think what you need is to add a default category to your intent-filter,
eg.
<activity
android:name=".FeedbackActivity" >
<intent-filter>
<action android:name="com.example.foo.bar.YOUR_ACTION" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
see this answer for more info.
I think you are creating your intent wrong. Try like this:
String CUSTOM_ACTION = "com.example.foo.bar.YOUR_ACTION";
//Intent i = new Intent(this, FeedBackActivity.class); // <--- You might need to do it this way.
Intent i = new Intent();
i.setAction(CUSTOM_ACTION);
startActivity(i);
Just add and intent-filter category as Default.
Implicit intent works perfectly and in many cases its better to use a implicit intent with Intent-action to call a service/Activity than using class-name.
Before startActivty() / startService() with proper context you cane use this method 'queryIntentActivities(Intent intent, int flags)' from package manager class.
It helps the ActivityManager (responsible for launching activities) to check whether the Android system is getting any match with you Intent.
If it doesn't it returns a list size 0 or else >0.
By this you can also check if your app is getting the call,and in this case even if your app is not installed / has got some problem, it will not crash but will throw a warning in Log. Users will face no big trouble apart from app not being launched.
(users will never forgive you if tour app crashes).
Hope this will help !!!
Happy Coding. :)
I faced the same problem when trying to launch the activity residing in the dynamic feature module and starting through action String as the Activity is not resolvable by name at compile time.
So I set the action but the activity crashes every time (No Activity found to handle intent bla bla.. ) until I set the correct package name.
Context c = getApplicationContext();// flag would be require Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag
Intent i = new Intent(action_string);
i.setPackage(context.getPackageName());//this did the trick actually
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
In the Manifest : add catrgory default to the intent filters
from google docs:
<category android:name="android.intent.category.DEFAULT"/>
Note: In order to receive implicit intents, you must include the CATEGORY_DEFAULT category in the intent filter. The methods startActivity() and startActivityForResult() treat all intents as if they declared the CATEGORY_DEFAULT category. If you do not declare it in your intent filter, no implicit intents will resolve to your activity.

Android Redirect from Java Class to Android Activity

I wanna to redirect to the Android Activity from java class..
here my code
class A {
getContext().startActivity(
new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com")));
}
from the code,it's redirected to webview instead i need it to redirect it into Android Activity something like this..
getContext().startActivity(
new Intent(A.this,My.class));
IF you want to start an activity, and send and url to it, you can do something like this:
Intent myIntent = new Intent(this, YourActivityName.class);
Bundle sendInfo = new Bundle();
sendInfo.putString("YOUR.IDENTIFIER.YOURURL","http://www.google.com");
myIntent.putExtras(sendInfo);
this.startActivity(myIntent);
Now if you want to do this from outside, like your activity catching url intents, you should add an intent-filter to your manifest, so you can handle those (after you get the "which program do you want to use to open this dialog). That would look something like this:.
<activity android:name=".YourActivityName">
<intent-filter>
<action android:name="android.intent.action.VIEW"></action>
<category android:name="android.intent.category.DEFAULT"></category>
<category android:name="android.intent.category.BROWSABLE"></category>
<data android:scheme ="http"
android:host ="www.hosttofilter.tld"
android:pathPrefix="/view">
</data>
</intent-filter>
This wil open all url requests for "http://www.hosttofilter.tld" with your app
To address your question about how to start a specific Activity check out this article. It provides several examples of exactly what you want to do. The second question is where are you calling this code? Is it from an Activity class or is it from some plain old java object? Hint: the code you posted won't work from a POJO.
Maybe Extra will fit your need:
Intent myIntent = new Intent(this,myActivity.class);
myIntent.putExtra("URL","www.google.de");
startActivity(myIntent);
In myActivity you would do:
String url = getIntent().getExtras().getString("URL");
Cheers.

Categories

Resources