Please help me how to call activity from a jar file in android:
My steps:
I had a AndroidLibrary project with a MainActivity.java
I had a AndroidApp project with a SplashActivity.java.
I imported AndroidLibrary.jar to libs folder of AndroidApp project and add build path
I added path MainActivity.java in AndroidManifest.xml file of AndroidApp project
In class SplashActivity.java i used code like this to call MainActivity.java from AndroidLibrary.jar
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
startActivity(intent);
However, when I call an activity from jar file , it immediately fails with ClassNotFound exception.
To call just an Activity from an external .jar, you need to:
Create an Intent
Call a setClassName with two String parameters: The package containing your class, and the entire path of the class that you need to call
Start the activity.
For example:
final Intent it = new Intent();
it.setClassName("com.example.test", "com.example.test.NameOfYourActivity");
startActivity(it);
If you want to know how to convert Java bytecode to Dalvik bytecode, visit my blog
Thanks.
Alernate piece of code that works
final Intent it = new Intent(android.content.Intent.ACTION_VIEW);
it.setClassName(getApplicationContext(), "<complete path of the Activity class>");
startActivity(it);
Android Calling Library activity in another new project
Intent callIntent = new Intent(MainActivity.this, com.example.testinglib.Testing.class);
startActivityForResult(callIntent, 0);
Note: startActivity(intent); is not working.
follow steps,
Create an Intent.
Call a setClassName with two String parameters: The package
containing your class, and the entire path of the class that you
need to call.
Start the activity.
That's it.
Related
I just following a tutorial to start a basic intent, and I get an error. Doing exact the same as the tutorial, created an empty Activity and just passed as argument to the intent. What is the problem?
Using a video tutorial: Lynda.com - Developing Android Apps Essential Training (2015)
The error you've got in IDE usually happens when you try to use Activity Context (keyword this) inside some callback, listener or anonymous function. In such situation this does not refer to the Context of the Activity.
That's why solution provided by #Vishal Patoliya should fix your problem because you're explicitly referring to the Context of the concrete Activity as follows:
Intent intent = new Intent(YourActivity.this,ItemUserSettingRattingActivity.class);
Try this
Intent intent = new Intent(YourActivity.this,ItemUserSettingRattingActivity.class);
startActivity(intent);
In case of Activtiy, you are going to another activity:--
Intent intent = new
Intent(YourActivity.this,ItemUserSettingRattingActivity.class);
startActivity(intent);
If you are passing intent from a fragment to open a activity then
Intent intent = new Intent(getActivity(),ItemUserSettingRattingActivity.class);
startActivity(intent);
Have you already created a class named ItemUserSettingRattingActivity.java? maybe you get an error it's because the intent you created didn't detect the class.
I have been trying to invoke another activity from the mainactivity class with the code:
startActivity(new Intent("intent filter name here"));
However when I debug the application when it gets to this line I get a source not found message in the class file editor that says the source attachment does not contain the source for the file ClassLoader.class. There is an option to change the attached source "android.jar" but there is only one such file under the_targetAPI folder. How can i fix this?
Try this:
Intent intent = new Intent(this, secondactivity.class);
startActivity(intent);
And don't forget to add it to manifest.xml.
By upper error also try to clean or rebuild the project.
Intent from one activity to another:
Intent ii=new Intent(Firstactivity.this,Secondactivity.class);
startActivity(ii);
Import tis one :
import android.content.Intent;
Add both classes in your Manifest.xml:
<activity
android:name="com.example.task.secondActivity"
android:label="#string/app_name" />
Try this one , its may work. check the manfest.xml ,
Source not found ClassLoader.class when invoking an activity from main
this may be the reason. check it out :)
Happy coding :)
I want to call a class of another project. I added it in the build path and also declared that class in the manifest file but when I call it gives me a no class found error. I am calling it from intent.
Intent intent = new Intent(getApplicationContext(), org.coolreader.CoolReader.class);
intent.putExtra("path", adapter.getItem(position).getPath());
startActivity(intent);
If you want to launch CoolReader from your program than look at this answer:
Intent coolReaderIntent = getPackageManager().getLaunchIntentForPackage("org.coolreader.CoolReader");
coolReaderIntent.putExtra("path", adapter.getItem(position).getPath());
startActivity(coolReaderIntent);
Of course, CoolReader (program) should be installed on the device.
Not sure how to explain this,hope you understand me.. Here is the problem :
I have a few packages in my app and I'm doing this :
Intent intent = new Intent(view.getContext(), com.example.app.Lol.class);
startActivity(intent);
and his code is in class which is in package : com.example.anotherone
It's not possible as I saw, that's why I'm asking..what I have to do, so I can be able to create an Intent like the example above.
Thanks anyway!
Make sure you are importing the class you want
import com.example.anotherone.Classname;
and pass Classname.class in your intent,
Intent intent = new Intent(view.getContext(), Classname.class);
And that your Manifest is updated with the correct name for the activity (com.example.anotherone.Classname).
If you want to start an activity from another application, you can do it by specifying the intent action. When you define the activity you want to start in its application Manifest file, you can set it's <action/> tag inside <intent-filter/> to whatever action you want. Then to start this activity from another application call
Intent intent = new Intent("your-action-name");
startActivity(intent);
Your application will be the only to answer on this action request and it will open. Hope this helps.
1.At first import the class you want.
2.pass the class name with intent.
For example:
import package.otherclassname.yourclassname;
Intent intent=new Intent(view.getContext(),yourclassname.class);
I have an Android library project that I would like to use from within another Android project.
The library has a Activity declared in its AndroidManifest. When I try the following within the second project:
Intent intent = new Intent(this, ReaderActivity.class);
startActivity(intent);
I get the following exception:
java.lang.RuntimeException: Unable to start activity ComponentInfo{br.com.digitalpages.reader.demo/br.com.digitalpages.reader.demo.ReaderDemoActivity}: android.content.ActivityNotFoundException: Unable to find explicit activity class {br.com.digitalpages.reader.demo/br.com.digitalpages.reader.ReaderActivity}; have you declared this activity in your AndroidManifest.xml?
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
...
Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {br.com.digitalpages.reader.demo/br.com.digitalpages.reader.ReaderActivity}; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1404)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1378)
...
How can I open the Activity from the another project?
EDIT:
By users answers I added the following line into my second project
<uses-library android:name="br.com.digitalpages.reader" android:required="true" />
But it's still doesn't works
I believe you must include the <activity> in your own AndroidManifest.xml -- I don't think it gets picked up from a library. I don't have my reference for that handy.
Update: It's official solution. From the doc:
Declaring library components in the manifest file
In the manifest file of the application project, you must add
declarations of all components that the application will use that are
imported from a library project. For example, you must declare any
<activity>, <service>, <receiver>, <provider>, and so on, as well as
<permission>, <uses-library>, and similar elements.
Declarations should reference the library components by their
fully-qualified package names, where appropriate.
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
intent.setComponent(new ComponentName("packagename//ex-com.hello",
"classname//ex-com.hello.ExampleActivity"));
startActivity(intent);
And make sure in library you have declared the activities. You don't need to declare the library activities in your current project's manifest.
did you add to the manifest?
http://developer.android.com/guide/topics/manifest/uses-library-element.html
This works:
In your library, put your custom Activity:
public class MyLibraryActivity extends ListActivity { ... }
No need to put it into a manifest.
In your calling Android project, create an empty (dummy) wrapper:
public class MyActivity extends MyLibraryActivity { }
and add reference to this class to your manifest:
<activity android:name="my_package.MyActivity" ... />
I am aware that the question is quite old but I think this might help people like me that came up with the same problem.
Using Eclipse, the best way to share code and activities among libraries is probably the one that can be found in the android documentation here:
Managing Projects from Eclipse with ADT
When using an activity inside the library, the activity should be declared only inside the manifest of library.
The activity can be started from the main app like this:
Intent intent = new Intent();
intent.setClassName(this, "com.duna.remotelocklibrary.activities.MainRemoteActivity");
startActivity(intent);
I tried to start the library's activity like the following code, but i warn you: it doesn't work
Intent intent = new Intent();
intent.setClassName("com.duna.remotelocklibrary", "com.duna.remotelocklibrary.activities.MainRemoteActivity");
startActivity(intent);
you should import just the code of the activity ( not the manifest too ) , and then , declare your Activity ( of the library ) , in the manifest of your second project
You don't need to explicitly add activity in your main project's manifest if you have already added the activity in your library's manifest by using the following code when starting library's activity.
For Kotlin
val myIntent = Intent(activityContext, ActivityToLaunch::class.java)
// Needed to set component to remove explicit activity entry in application's manifest
myIntent.component = ComponentName(activityContext, PickerActivity::class.java)
activityContext.startActivity(myIntent, PICKER_REQUEST_CODE)
For Java
Intent myIntent = new Intent(activityContext, PickerActivity.class);
// Needed to set component to remove explicit activity entry in application's manifest
final ComponentName component = new ComponentName(activityContext, PickerActivity.class);
myIntent.setComponent(component);
activityContext.startActivity(myIntent, REQUEST_CODE);