I am getting this exception while I am trying to call an activity from another one. The complete exception is
android.content.ActivityNotFoundException:Unable to find explicit activity class {com.x.y/com.x.y.class};
I am doing an intent.setClass("com.x.y","com.x.y.className") where className is the name of my activity class and com.x.y is the package it resides in.
My AndroidManifest.xml has the following content:
<activity android:name="com.x.y.className" android:label="#string/app_name">
Am I missing anything?
Maybe you need to check that you added the new activity to the manifest.xml file
Example:
<activity
android:name=".className"
android:label="#string/app_name" >
</activity>
If other people are encountering something similar and arrive at this post, an issue I had may save you some time. May not be related to the OP's problem but def related to the ActivityNotFound exception.
I was trying to load an activity by using:
Intent intent = new Intent( this, class );
However I continuously kept getting the ActivityNotFoundException even though I had checked and rechecked the code multiple times.
This exception I was getting wasn't actually being caused by the intent but some code I was running inside the loaded activity throwing a RuntimeException. (my issue was caused by Typeface.createFromAsset())
It is possible you are running into a similar RuntimeException in your activity.
To see if this is the case, put your intent code in try catch blocks. Like so:
try {
/* your code */
...
} catch ( ActivityNotFoundException e) {
e.printStackTrace();
}
Run your app again, and check your LogCat, if it's the same issue, you'll get a RuntimeException with a "Caused By:" entry pointing to your actual issue.
I spent a good hour trying to figure this out. Hopefully this may save someone some time.
The activity you are calling should appear not only in the Manifest for its own package, but in the Manifest for the CALLING package, too.
Delete your activity from the manifest and then add it again. This type do not write type the XML directly. Instead, go to Application > Application nodes > add, choose the Activity, and then browse for the file source.
This worked for me.
intent.setClass takes parameters as "Package Context" and "Class".
an example would be:
intent.setClass(CurrentActivity.this, TargetActivity.class);
also you need to check if the activity is registered in manifest file.
Added a new activity and defined it in manifest.xml, but I was still getting "Unable to find explicit activity class" error. I am using Eclipse. Solution for my problem was "cleaning" the project. From the main menu in Eclipse: Project|Clean.... Then you select your project and clean it.
Hey, you need to use another form of Intent constructor. This will surely solve your issue within a second:
Example:
Intent inte=new Intent(getBaseContext(),"your class name with .class extension ");
startActivity(inte);
This works perfectly and I checked this code, its working properly.
I had an ActivityNotFoundException when I implemented the Activity inside another class (as an inner class):
//... inside the utility class Pref
public static class Activity extends PreferenceActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
}
}
//...
Declared as the following inside the manifest:
<activity android:name=".Pref.Activity"
...
After declaring this as a normal class (public class PrefActicity) and changing manifest accordingly, it worked as usual.
I was using getActivityContext() (instead of Activity.this) for the menu code to save some work, and copy-and-paste it to each activity without editing each time.
I replaced them with Activity.this, and the issue is gone.
I have a feeling a smarter Android guy could work-around not having to do that. Would like to hear what it would be.
Looking at the documentation here what you want is:
intent.setClassName("com.x.y", "className");
Restart the Eclipse and check your Manifestfile again. If you find missing the respective Activity, then add it and try again. It solved my similar issue.
In addition to Mina's answer.
When you declare activity as inner static class then you should write your activity into manifest like ...
<activity android:name=".app.FragmentLayoutSupport$DetailsActivity" />
here .app comes from your package name , it can be .helpers.afdfa$afda
My solution to this error was to add a package name in front of the name in manifest.
I had the following activities:
id.scanner.main.A1
id.scanner.main.gallery.A2
My manifest contained the following:
<activity android:name=".A1" ....></activity>
<activity android:name=".A2" ....></activity>
This solved the problem:
<activity android:name=".A1" ....></activity>
<activity android:name="gallery.A2" ....></activity>
Yeah I got this problem too. I refreshed the project. And then, everything works fine.
when i have same issue. if you are using library class files and writing it into android manifest files write it like and then remove the library projects manifest files this portion>>
then it will work absolutely..
This exception also occurs if you include a library in your app and if the library is calling an activity defined in the library project. In this case we need to merge library's manifest with calling app's manifest.
With ADT version 20, we can do this by adding the below statement in project.properties of calling app.
manifestmerger.enabled=true
Check out the content of the Android Manifest File in the bin folder of the project. When your app is compiled and packaged the Manifest File is copied to the bin folder. In my case the Manifest in the bin folder did not agree with the original Manifest. This is probably a mistake of Eclipse. I manually copied the Manifest to the bin folder and it worked.
you can add this code in manifiest.xml file
action android:name="com.kaushalam.activity101activity.SecondActivity"
category android:name="android.intent.category.DEFAULT"
I got the same case too. After reading thepearson's answer, I revised my Activity and found out that I wrote
public void onCreate(Bundle s)
But in fact it should be
protected void onCreate(Bundle s)
And it works now!
This works if you have an Activity object (which you need to launch):
intent.setClassName(CallingActivity.this, activityToLaunch.getComponentName().getClassName());
Activity you're calling sholdn't contain "sheme" and contain intent-filter:
<activity android:name=".SecondActivity">
<intent-filter>
<action android:name="com.example.sj.myapplication.SecondActivity"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
so in calling code:
Intent intent=new Intent("com.example.sj.myapplication.SecondActivity");
startActivity(intent);
Try using the following:
intent.setClassName("com.x.y", "com.x.y.className");
This works for me
I also ran into ActivityNotFoundException by passing the wrong view into setContentView(), each activity's class file must correspond with the layout xml file this way.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.wrongView);
}
as opposed to
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.correctView);
}
I had the same issue. I tried everything but the error, which I sorted out later, was that there was a space left between double quotes and my class name. It has to be:
intent.setClassName("com.x.y","com.x.y.className")
not
intent.setClassName("com.x.y"," com.x.y.className")
Related
I noticed that even though I made a new xml named drink, there is no ".java" file with it. Do I need to make one or do i add it on to MainActivity.java?
This code is in my MainActivity.java and the Intent line is saying that drink cannot be resolved to a type.
public void calculate(View view){
Log.i("clicks","You Clicked B1");
Intent i=new Intent(MainActivity.this, drink.class);
startActivity(i);
}
This is in my manifest:
<activity
android:name=".drink">
</activity>
This is a post before I knew android. I didn't realize you have to make the java file and xml file.
This code is in my MainActivity.java and the Intent line is saying
that drink cannot be resolved to a type.
This means that you don't have a "drink" class. you have to create a new class called "drink" that extends Activityin the application package.
Without any further code to go on (including knowing what your drink class and your manifest.xml looks like), I would check to make sure that the package name specified in the manifest tag of your manifest file matches the package name of your drink class.
I am not sure I understand you correctly, but to go to a different 'page', which is in your case an Activity, you have have to create one yourself.
If you do not see a .java file representing your Activity you should make one like this (note the uppercase Drink for naming conventions):
public class Drink extends Activity{
protected void onCreate(Bundle savedInstance){
this.setContentView(R.layout.drink.xml)
}
}
I am assuming you already made a xml file for the layout of the Activity. Please read the helpfull documentation of Android.
This means that you did not create the class yet. Right - click on your package name in Package Explorer and select Class. Then, create your activity with your desired class name. Also, check if you declared your android:onClick = calculate in your XML file if you get a yellow attention sign that says that your method is not used.
Attempts to switch activities between two Android Fragments is failing.
This happens in spite of using the Android doco example verbatim:
Android Fragment doco
Attempts to add the target Fragment to the AndroidManifest.xml does not compile with the same message that is in the Title of this question.
If I hack the same process using only Views and straight Activities the all is well.
If I leave the AndroidManifest.xml unchanged then I get a run-time exception with the question:
android.content.ActivityNotFoundException: Unable to find explicit activity class {name.davidwbrown.actionbartabs/name.davidwbrown.actionbartabs.UserDetailsFragment}; have you declared this activity in your AndroidManifest.xml?
Try defining an xml layout containing the fragment instead of assigning it to the <activity> tag on the manifest. Then use findViewById(R.id.frameId) to find it in code and then attach it to the activity. In the manifest, keep the activity tag something similar to this and it should work:
<activity
android:name="name.davidwbrown.actionbartabs.UserDetailsFragment"
android:label="#string/activity_name" >
</activity>
Maybe you should try changing your Fragment class to a FragmentActivity class.
I'm trying to embed listactivity inside Tab. but when i run the application. It stop working and force me to close the app. Take a look to program in below link:
http://dl.dropbox.com/u/16910648/RazzilCity.zip
I saw your code. Just two mistakes I found, In your Manifest.xml and in Places.java.
In your Manifest file you have defined City Activity two times.
Just remove the the line
<activity android:name=".City" android:label="#string/app_name" android:theme="#android:style/Theme.NoTitleBar"></activity>
and in your Place.java,
just replace the line
setListAdapter(new ArrayAdapter<String>(this,R.layout.places_list,PLACES));
by
setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,PLACES));
I am creating an Android app in which I'm drawing a view on a canvas. When the device's orientation changes, the activity restarts. I don't want it to.
How can I avoid restarting the activity when the orientation changes?
There are various ways to do it, but as given here, using
android:configChanges="keyboardHidden|orientation|screenSize"
allows you to listen for the config changes. You then respond to these changes by overriding onConfigurationChanged and calling setContentView.
This is the way I've been doing it, but I'd be interested to know other people's thoughts.
Define your activity in the AndroidManifest.xml like this:
<activity
android:name="com.name.SampleActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:icon="#drawable/sample_icon"
android:label="#string/sample_title"
android:screenOrientation="portrait" >
</activity>
Check in your android manifest file that you have written android:configChanges="orientation" on the activity..
I tried to write android:configChanges="keyboardHidden|orientation|screenSize" in activity tag but in not works.
I tried a lot of methods but nothing works until I added android:configChanges="keyboardHidden|orientation|screenSize" for all the app activities and it works perfectly.
For xamarin users,
To avoid the application restart on orientation change in Android, add this
ConfigurationChanges = Android.Content.PM.ConfigChanges.Orientation | Android.Content.PM.ConfigChanges.ScreenSize
to Activity attribute of all Activity classes.
For example, below is my demo code
[Activity(Label = "DemoApp", ConfigurationChanges = Android.Content.PM.ConfigChanges.Orientation | Android.Content.PM.ConfigChanges.ScreenSize)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
{
protected override void OnCreate(Bundle bundle)
{
//Some code here
}
}
Add android:configChanges="keyboardHidden|orientation" to your activity
I would recommend using Fragments. You can simply use setRetainInstance(true) to notify that you want to keep your fragment.
Add this to all of your activities in the manifest.
android:configChanges="orientation|screenSize"
Example:
<activity android:name=".activity.ViewActivity"
android:label="#string/app_name"
android:configChanges="orientation|screenSize"/>
TO avoid restart on keyboardHidden|orientation - How to disable orientation change in Android?Please follow Android API guide - Handling Runtime Changes Using the Application Class - Activity restart on rotation Android
Declare this in your AndroidManifest.xml
<activity android:name=".complex_examples.VideoPlayerActivity"
android:configChanges="keyboard|keyboardHidden|orientation
|screenSize|screenLayout|smallestScreenSize|uiMode"
android:launchMode="singleTop"/>
But take care, Android Developers Documentation says that you should do it only if there is no better options left.
Note: Using this attribute should be avoided and used only as a last
resort. Please read Handling Runtime Changes for more information
about how to properly handle a restart due to a configuration change.
If you are sure about doing it, you can handle the configuration changes by your self in onConfigurationChanged() method.
To stop from destroying the activity on rotation
`android:configChanges="keyboardHidden|orientation|screenSize"`
just add android:configChanges="keyboardHidden|orientation|screenSize" for all the app activities in the manifest file
For me occur when change the night mode, only write this in the manifest:
android:configChanges="uiMode"
Put this under AndroidManifest.xml
<activity android:name=".MainActivity"android:configChanges="orientation|screenSize">
please let me know if it worked(It worked for me, I'm new to Android studio)
I saw this code on web.
If you are using a "TextView" field to output answers it will reset on orientation changes.
Take note that "EditText" fields do not reset on orientation changes. with no additional code needed.
However, "android:inputType="none" does not work.
"android:editable="false"" works but its depreciated.
I'm using the latest version of Android Studio (Bumblebee)
If you use this code in your Android Manifest file in Android Studio Bumblebee:
<activity android:name=".MainActivity"android:configChanges="keyboardHidden|orientation|screenSize">
You need additional code as this code above messes your layout up when you change orientation. However, it keeps the text/numbers in your choice of text input fields respectively.
This question already has answers here:
Best way to add Activity to an Android project in Eclipse?
(8 answers)
Closed 9 years ago.
Easy one.
I've gone through a few guides and tutorials, and they're quite clear on how to start an activity (with intent).
However, how do I create a new activity in Eclipse? I can probably do this by hand, but then I have to modify the R file, which is auto-generated, and add a new xml layout.
Ok. Being a newbie myself I think the above two answers are thinking too much. He's asking very simply how to create a new activity in Eclipse.. I think this is what he wants:
A new Activity in Eclipse is actually a Class.
You would doubleclick 'src' on the left side in the Package Explorer, then highlight your 'com.' name, right click, select 'New' and then select 'Class'. Enter the Name as NewActivity and set the Superclass to android.app.Activity, then hit Finish.
When the NewActivity.java file opens up it should look like this:
package com.example.yourappname;
import android.app.Activity;
public class NewActivity extends Activity {
}
You can leave the Superclass blank and add extends Activity to the code itself if you prefer.
The final step is adding the Activity to your Manifest. So doubleclick AndroidManifest.xml to open it up and then click the 'Application' tab on the bottom. Next to the 'Application Nodes' box, click 'Add'. Highlight 'Activity' (the square box with a capital A) and click 'Ok'. Now look for the 'Attributes for Activity' box and enter a Name for the Activity and precede it by a period. In this example you'd enter '.NewActivity'.
And then you can add your onCreate() code so it looks like this:
public class NewActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_view);
//rest of the code
}
}
main_view would be your main view xml file, main_view.xml, that you would create in your layout directory.
To call the new Activity, your Intent in the code (in a different Activity) to start a new Activity looks something like this:
Intent startNewActivityOpen = new Intent(PresentActivity.this, NewActivity.class);
startActivityForResult(startNewActivityOpen, 0);
And that's it, you have the code to call the new activity and you created it. I hope this helps someone.
I know this is an old question, but I know there are still people with this same question(I did up until today)
If you add a new activity to your manifest file, there's a special link to click on to automatically create the new Activity, complete with the onCreate() method ready to be filled in.
Open the AndroidManifest.xml, and go to the 'Application' tab. Under 'Application Nodes', find and click the 'Add' button. You will likely create a new element at the top level, so select that option, highlight 'Activity', and press OK.
Once you've created the Activity, go to the 'Attributes for Activity' and fill in the name. Once you've filled in the name you want, click on the blue 'Name*' link next to the field. The new file wizard will show up, and all you have to do is press OK.
Voila! New Activity, registered in the manifest and as a ready-to-go Java class.
You create the activity by extending the activity class . Once you have creatd the activity class , You need to add the activity in the androidmanifest file specifying the properties for the activity...
A sample one would be something like this ...
<activity android:name=".JsonActivity" android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
The action here indicates that it is the one that starts first ..
I dont think you need to modify the R.java file ... Once you add these in the android manifest file and save it automatically gets updated. Also the things that u added like the layouts, menus, strings, id's etcc.... in the various xml files also get automatically updated...
Correct me if i am wrong ...
I tried searching for this question on Google and haven't seen this solution yet, so I thought I'd post it here.
In Eclipse, you can click on the "New" button on the toolbar. Under Android, select Android Activity, and run through the wizard. This is the best solution by far, since it lets you set up a layout and an Activity all in one, while also updating the Manifest for you.
How to add New Activity Eclipse step by Step:
Stpe1:Double click on the androidManifest
Step2:on the Menu bar click Aplication
Step3:Scroll down to application node and CLick add button
Step 4:click select Activity and Ok
step 5:clik on the the Texte(Name* Note:make sur u clik on the texte
not into the textbox )
step6:there a new Java Class dialog
## Heading ##write the classe name
## Heading ## check checkbox construct from the super classe and and ok..
There is also the tried and tested method of starting with one of the samples and going from there.
The Hello tutorial is as good a starting point is any, just select the create from existing sample option.
The latest update to the eclipse plugin even includes a tool to rename your package should you change your mind though I haven't used it yet so can't say if it works. (Right click on the package then select Android Tools, Rename Application Package).
It is important to say that if you type the desired name for the new Activity on Name box, a dot must be put before the new name. Otherwise, the window to complete the creation of Java code will not open when you click on names link.