Android NoClassDefFoundError when create intent - android

I got a NoClassDefFoundError when try to create an intent for a class which inherited from MapActivity.
here is the code where I got the exception: Intent mapTabIntent = new Intent(this, MapTab.class); Where this is the TabActivity
I tired with Android version 2.1 and 2.2 Google APIs.
I put the manifest file outside the application:
I used in manifest the android.permission.INTERNET too.
My application is works perfectly without the MapTab.
I already recreate the MapTab class and maptab layout, the result is same.
What I missed out?

check this question: https://stackoverflow.com/a/5719847/1317080
It seems your problem has to do with the vm not finding the google maps class you inherited from

Related

Exporting cocos2d Android application as aar and calling its Activity from another project

I had built a cocos2d Android project from cocos creator. (sample application)
And I exported this android application as an android library (sample.aar)
I created another Android application (MyDemo) and imported the android library (sample.aar)
I am calling the AppActivity of (sample.aar) library from the MainActivity of (MyDemo) application on Button click
aarButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent();
intent.setComponent(new ComponentName("org.cocos2d.demo", "org.cocos2dx.javascript.AppActivity"));
startActivity(intent);
}
});
The result is MyDemo app crashed.
Below is the error message I get in the console
android.content.ActivityNotFoundException: Unable to find explicit activity class {org.cocos2d.demo/org.cocos2dx.javascript.AppActivity}; have you declared this activity in your AndroidManifest.xml?
Below the screenshot of the error
#But there is a trick
If I install the (sample) application on the phone first and then create an aar and import in the MyDemo application, then it works fine.
But I don't want to do like this.
I am stuck with this for couple of days.
Any help would be appreciated

Not the right Activity java.lang.SecurityException: Permission Denial: starting Intent

I read all other answers about this problem but it doesn't help me.
I have an app (MyApp) which include a library (named lib (example)). In my app, I call LoginActivity in my lib.
But I have this exception.
java.lang.SecurityException: Permission Denial: starting Intent { cmp=fr.myotherapp/.activity.LoginActivity
Myotherapp is a other app which I created and installed in the same device.
I don't set "exported"="true" in myotherapp manifest because I dont want to use this one but I set "exported"="true" for the activity in the lib manifest.
I use Android Studio.
Why does Android want to use this one even if I call the activity like this ?
final Intent intent = new Intent(mContext, fr.lib.activity.LoginActivity.class);
Thanks
If you are using eclipse, try adding manifestmerger.enabled=true to your project.properties file.
relevant docs :
http://tools.android.com/tech-docs/new-build-system/user-guide/manifest-merger#TOC-Android-Manifest-file-merging
https://roostertech.wordpress.com/2013/10/29/manifest-merger/

Calling an Application Activity from a Library Project in Android

Ok,
So I'm making a library project of UI elements. The Library has some activities which are based of ActionBarSherlock which is a backwards compatibility library for the action bar in android. In these activities I would like to have a button in the action bar which will take the user home regardless of which activity they are using in the Library project.
Some terminology. The 'library' refers to the Android UI library project I'm working on. The 'Application' refers to whatever customer a developer might be using with the Library included.
Usually, when you make an activity and you want to call another, you would do something like this.
intent = new Intent(this, WhateverMyActivityName.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Simple enough. But here's the tricky bit. Android Libraries have little to no knowledge of what application is using them. So 'WhateverMyActivityName.class' is useless as there is no way to predict what the developers application will call their activities.
I need to replace
intent = new Intent(this, WhateverMyActivityName.class);
with something like this
intent = new Intent(this, getApplication().MainActivity().getClass());
or possibly use some sort of intent action which will call the main Activity in the application (Intent.ACTION_MAIN or Intent.CATEGORY_LAUNCHER)
So in short: How do I get an applications main activity from a library project?
We can use reflection to get class object.
Class.forName("com.mypackage.myMainActivity")
Add this code in Library project to call,
try {
Intent myIntent = new Intent(this,Class.forName("com.mypackage.myMainActivity"));
startActivity(myIntent );
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
"com.mypackage.myMainActivity" is the Activity present in Main project, that we need to call from its Library project.
The application calls some method in your library providing the Intent to be invoked, or providing the Class of the activity to be invoked. Your library stores that someplace and uses it.
Your assumption that the right answer is "Intent.ACTION_MAIN or Intent.CATEGORY_LAUNCHER" may be inaccurate. For example, some apps have that be a splash screen activity (which is an issue in its own right, but that's beside the point), and that would not be where a home affordance within the app should go.
You can get the list of activities using the code mentioned in the this post. After that loop through the resolveinfo and check the intenet filter to find the activity with your desired intent action.

Could not find class when application is loaded into emulator

I have an activity (DemoAppActivity) from which I am trying to launch a different activity (MainActivity) when a button is pressed. However I am getting two errors:
1) when the app is loaded into the emulator, I see the following in the Android logs:
10-12 18:48:19.579: ERROR/dalvikvm(620): Could not find class 'com.example.android.hcgallery.MainActivity', referenced from method com.consultknapp.demoapp.DemoAppActivity$1.onClick
2) when i actually push the button that calls startActivity:
10-12 18:54:58.019: ERROR/AndroidRuntime(620): java.lang.NoClassDefFoundError: com.example.android.hcgallery.MainActivity
Here is how I am starting the activity (note: I import the class with an import statment, import com.example.android.hcgallery.MainActivity)
startActivity(new Intent(DemoAppActivity.this, MainActivity.class));
I have the MainActivity project folder in my build path in eclipse, and I even see it load the MainActivity.apk when I compile/run my DemoAppActivity. However, it bombs when I try to run it on the emulator.
What am I missing here? Do I need to jar up my MainActivity and include it in the DemoAppActivity somehow?
I was able to figure it out from another post. Basically you need to do this with the Intent before starting the activity:
Intent i = new Intent();
i.setComponent(new ComponentName("com.example.android.hcgallery", "com.example.android.hcgallery.MainActivity"));
startActivity(i);
The setComponent tells the class loader to make it available. After adding that code, it worked!

Using an Android library project Activity within another project

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);

Categories

Resources