Unable to find explicit activity class in android - android

I have an android project with several packages. The structure of the packages in this case is com.siva.restorative is the package that contains the activity I want to run.
My activities are declared in my manifest as
<activity android:theme="#style/YtdTheme" android:name="com.siva.restorativecare.RestorativeCare">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
The mainScreen activity displays fine, since it is inside the com.WAPP package. But when I try to run the setLocationActivity, I get the unable to find explicit class error. Here is how I have the intent parameters:
Intent i = new Intent(this,SecondActivity.class);

I guess first you should change the action string in manifest as:
<action android:name="com.siva.restorativecare.RestorativeCare" />
and then start running your activity as:
Intent i = new Intent("com.siva.restorativecare.RestorativeCare");
startActivity(i);

Related

start an application from another

I'm developing two applications, AppA and AppB, and I want to start AppB from AppA.
In AppA, I am using
Intent initIntent = getPackageManager().getLaunchIntentForPackage("com.example.appB.ActivityB");
in AppB, I am adding an intent filter to the manifest file :
<activity
android:name="com.example.appB.ActivityB"
android:label="#string/title_activity_init" >
<intent-filter>
<action android:name="com.example.appB.ActivityB" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
But I got a java.lang.NullPointerException on the intent...
Any ideas would be welcome.
Your intent filter isn't complete for this API:
The current implementation looks first for a main activity in the category CATEGORY_INFO, and next for a main activity in the category CATEGORY_LAUNCHER. Returns null if neither are found.
So you need to modify Activity B's intent filter to include:
<action android:name="android.intent.action.MAIN" />
You are trying to specify an Activity in the Intent, even though you are creating a Intent meant to launch a package; this is not neccesary. Simply remove the Activity from your Intent like so:
Intent initIntent = getPackageManager().getLaunchIntentForPackage("com.example.appB");
And then just make sure that you have the following two lines in whichever Activity you wish to start when the application is launched:
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
They should be already be in there for your main Activity, although it looks like you were missing the android.intent.action.MAIN intent filter in your ActivityB.

Intent does not work in same way all the time in Android

There are 2-3 ways to use Intent to start New Activity.
Mostly, I am using
Intent openStartingPoint = new Intent("com.Example.Jeeten.Connection");
startActivity(openStartingPoint);
But sometimes, It does not work, It shows error Activity not found and in that case If I use
Intent openStartingPoint = new Intent(Connection.this, Hello.class);
startActivity(openStartingPoint);
then It works fine. What can be the issue with this ?
When u make an activity, u have to register it in your AndroidManifest.xml file.
<activity
android:name=".Connection"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.Example.Jeeten.CONNECTION" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
In the above code <activity android:name represents your activity class(Case sensitive),in your case its Connection and Hello.Next is <action android:name in this you specify by what name are you going to refer to the activity(its a good practice to put the last word all in uppercase CONNECTION).
So make sure you have two such activities in your xml file.In case one of the activity is your starting point of your app,modify the <intent-filter> like this
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />

When i use Activity in camel case it gives error but works fine in upper case spelled

Hy! Expets i have two activities
1- MainActivity
2- Startup
I am starting my MainActivity activity through new intent after starting Startup activity using Thread. but when when i call new intent by passing MainActivity spelled in camel case and within inten-filter tag <action android:name="com.example.test.MainActivity" as shown below
Thread timer =new Thread(){
public void run(){
try{
sleep(5000);
}catch(InterruptedException e){
e.printStackTrace();
}finally{
Intent startUpIntent = new Intent("com.example.test.MainActivity");
startActivity(startUpIntent);
}
}
};
and here is AndroidMaifest.xml File code
<activity
android:name="com.example.test.Startup"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.newboston.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.test.MainActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
it gives error.
But when I use code as
Intent startUpIntent = new Intent("com.example.test.MainActivity");
AndroidManifest.xml code
<intent-filter>
<action android:name="com.example.test.MainActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
then it works fine as i want.
I want to know reason to use Upper case spelled instead of camel case.???
Thanx...
You probably want to be defining an intent for a specific component:
Intent startUpIntent = new Intent(Startup.this, MainActivity.class);
Then you don't need to bother with an <intent-filter> for MainActivity in your manifest.
There are a number of action constants defined in the Intent class. Some of them are:
ACTION_MAIN: Start up as the initial activity of a task, with no data input and no returned output.
ACTION_CALL: Initiate a phone call.
ACTION_EDIT: Display data for the user to edit.
To use ACTION MAIN in your manifest, you will replace the ACTION_ part with android.intent.action.. Since they are constants defined in a class you are using, they need to be used as defined.
For example, the following two declarations are not the same. They define and instantiate two different variables:
int myVariable = 1;
int MYVARIABLE = 1;
So, you cannot write:
<action android:name="android.intent.action.MAIN" />
as
<action android:name="android.intent.action.main" />
or anything other that the former.
From the android developers resource page on <action>:
To assign one of [standard actions defined in the Intent class] to
this attribute, prepend "android.intent.action." to the string that
follows ACTION_. For example, for ACTION_MAIN, use
"android.intent.action.MAIN" and for ACTION_WEB_SEARCH, use
"android.intent.action.WEB_SEARCH".
To read up more on this:
Intent
action

ActivityNotFoundException?

I am getting an ActivityNotFoundException in the following code:
Main.java
Intent intent = new Intent();
intent.setAction("com.test.app.TEST");
startActivity(intent); // ActivityNotFoundException
Manifest.xml
<activity android:name=".MainActivity" android:theme="#android:style/Theme.Dialog">
<intent-filter>
<action android:name="com.test.app.TEST" />
</intent-filter>
</activity>
I've had this issue too, as perfectly concisely described by jpahn.
the period at the front did not give any help to me.
even with exactly this (a copy of the original question including edits), I would still get ActivityNotFoundException.
Main.java
Intent intent = new Intent();
intent.setAction("com.test.app.TEST");
startActivity(intent); // ActivityNotFoundException
Manifest.xml
<activity android:name=".MainActivity" android:theme="#android:style/Theme.Dialog">
<intent-filter>
<action android:name="com.test.app.TEST" />
</intent-filter>
</activity>
This was resolved, after much trial-and-error, by simply adding this to the intent-filter in the manifest:
<category android:name="android.intent.category.DEFAULT" />
So the final manifest file contained:
<activity android:name=".MainActivity" android:theme="#android:style/Theme.Dialog">
<intent-filter>
<action android:name="com.test.app.TEST" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
I got this error after moving an activity class from one package to another.
Clean build solved it (Project -> Clean).
Be sure to declare your activity in the manifest.xml within the aplication:
<application>
<activity android:name=".YourNewActivity"/>
</application>
To start the new Activity:
Intent intent = new Intent(main.this, YourNewActivity.class);
startActivity(intent);
Where main stands for the current activity,
Add a . (dot) before your activity name in Android Manifest. So it should be android:name=".WordsToSpeakMainActivity"
I have some addition to the #Tom Pace answer. The answer is completely right, but to make it more clear:
ActivityNotFoundException occurs because of absence of
<category android:name="android.intent.category.DEFAULT" />
Because when Android OS see this in the manifest file, understands that this activity can receive intent.
The point ActivityNotFoundException thrown is that, when activity(intent-creator-activity) tries to create intent for other activity(intent-receiver-activity), Android OS sees there is intent for receiver activity but receiver activity does not receive anyone. Then Android OS returns null or empty intent to intent-creator-activity. And startActivity throws that exception.
I have found a code from android developers to avoid this exception:
// Verify the original intent will resolve to at least one activity
if (sendIntent.resolveActivity(getPackageManager()) != null) {
startActivity(chooser);
}
Android Developers: Intent Filters
To be safe you can also call your new activity like this:
Intent intent = new Intent();
intent.setClass(this, THECLASSNAME);
startActivity(intent); //
However, you must add the activity to the androidmanifest - and write a . in front of it, e.g.
<activity android:name=".YOURACTIVITYNAME"></activity>
There two types of intents in android framework,
1-Implicit intents that you are using,
<activity android:name=".MainActivity" android:theme="#android:style/Theme.Dialog">
<intent-filter>
<action android:name="com.test.app.TEST" />
</intent-filter>
</activity>
just add one line in intent filter
<intent-filter>
<action android:name="com.test.app.TEST" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
2- Explicit Intents
Intent i=new Intent(CurrentActivity.this,WhereWeWantToGoActivity.class);
startActivity(i);
To launch an activity by a string definition, use:
Intent intent = new Intent();
intent.setComponent(
new ComponentName("com.app", "com.app.activity.TheActivity"));
startActivity(intent);
At the very top of your AndroidManifest.xml, you'll see the package attribute
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.example"
and then, in the activity tag, you'll see the name attribute:
<activity
android:name=".Something"
Make sure that the package name and activity name, when joined together, make the full package specification of your Activity i.e.
com.android.example + .Something = com.android.example.Something
Otherwise, you'll get a ActivityNotFoundException.
I found a solution to this problem... I´m using 2 modules in a android studio project, the thing here is that I needed to add the activity to the main manifest file
<activity android:name="com.HeadApp.ARTry.UnityPlayerActivity"
android:clearTaskOnLaunch="false" android:label="#string/app_name"
android:screenOrientation="portrait"
android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen"
/>
I had that in the unity activity manifest, I just copied the activity and paste it in the main manifest and that was it, hope it helps, eve been struggling a lot with this for the past 3 weeks

launch activities from different package

I have activity A in package one, and I want to run an intent which will up an activity B which is in package two.
How can I do this? Any samples will be welcome.
this is what ive done, and the error i get:
first activity ("MainActivity") in a package: com.abelski.currencyclient
and second activity("SecondActivity" in a diffrent package: com.idan.second
now i wanna call from MainActivity to SecondActivity.
ive added this line at the manifest of the MainActivity:
<activity android:name="com.idan.second.SecondApplicationActivity"></activity>
now in main Activity i got this button which run this line:
Intent intent = new Intent(MainActivity.this,SecondApplicationActivity.class);
and this is the rror:
04-29 09:20:59.197: ERROR/AndroidRuntime(399): Uncaught handler: thread main exiting due to uncaught exception
04-29 09:20:59.276: ERROR/AndroidRuntime(399): java.lang.NoClassDefFoundError: com.idan.second.SecondApplicationActivity
04-29 09:20:59.276: ERROR/AndroidRuntime(399):
I am assuming that by "packages" you mean applications.
We have:
- ApplicationA with FirstActivity
- ApplicationB with SecondActivity
If, in the ApplicationB's AndroidManifest.xml file, in the declaration of SecondActivity you add an intent filter such as:
<activity android:name=".SecondActivity">
<intent-filter>
<action android:name="applicationB.intent.action.Launch" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
You can create an Intent to launch this SecondActivity from the FirstActivity with:
Intent intent = new Intent("applicationB.intent.action.Launch");
startActivity(intent);
What this all means is:
The SecondActivity has a filter for the intent action of "applicationB.intent.action.Launch"
When you create an intent with that action and call 'startActivity' the system will find the activity (if any) that responds to it
The documentation for this is at: https://developer.android.com/reference/android/content/Intent.html
I had the same problem and the solution was another level in the root of your package name.
If you have two packages "com.first...." and "com.second...", and the manifest is referencing "com.first". Then you can refactor both packages in order to reuse the first part. For instance, "com.package.first" and "com.package.second". Then your manifest has to be also updated
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.package">
...
<activity android:name=".first.FirstPackageActivity" android:label="FirstPackageActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".second.SecondPackageActivity" android:label="SecondPackageActivity">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
Your java code can create an intent and start the activity in the usual way:
Intent intent = new Intent(this,ActivityClassName.class);
startActivity(intent);
If the package you mentioned here is same to application,I think the answer in the question Android: Starting An Activity For A Different Third Party App is simpler。With the first answer to that question, you don't need to make any modification to your second application.
Use explicit intents:
Intent intent = new Intent(context,ClassName.class);
where ClassName is from another package.
Sometimes, you will not know the name of the class in such cases you will have to rely on the Intent that the target class advertises to handle.
First, you need to declare both packages and activities on Manifest :
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
and for the second activities, on android:name --> .Packages name.activity, assuming your second packages name com.iden.second :
<activity android:name=".second.SecondActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
<intent-filter>
<action android:name="android.intent.action.SECOND" />
<category android:name="android.intent.category.SECOND" />
</intent-filter>
</activity>
then on the MAINACTIVITY JAVA CLASS, asuming you will start second activity using a button :
public class MainActivity extends AppCompatActivity {
private Button mButtonSecond;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButtonSecond = findViewById(R.id.btn_second);
mButtonSecond.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent newActivity = new Intent(MainActivity.this,com.iden.second.SECOND.class);
startActivity(newActivity);
}
});
}
}
hope that can help, since i am not clear with the question structure

Categories

Resources