Bizarre NoClassDefFoundError when running in Android - java

I've tried looking at all the other NoClassDefFoundError threads on here and elsewhere, but they all seem to be a different problem.
My activity refers to a custom FragmentPagerAdapter - let's call it MyFragmentPagerAdapter.
The Activity refers to it with:
MyFragmentPagerAdapter pagerAdapter = new MyFragmentPagerAdapter(getFragmentManager());
ViewPager viewPager = (ViewPager) findViewById(R.id.form_pager);
viewPager.setAdapter(pagerAdapter);
This all seems to compile and build perfectly well in Eclipse. Eclipse can certainly find all the classes - it auto filled the import statement for me.
However, when I try and run the class in Android (emulator or device) I get a NoClassDefFoundError for MyFragmentPagerAdapter and (of course) the app crashes.
I've tried deleting the APK on the device and re-installing in case it was a deployment issue, but it didn't help.
This isn't to do with some 3rd party library - it's my own class that it fails to find!
I have also tried "cleaning" the Project.
I have extracted the apk and inspected the output of
dexdump classes.dex | grep PagerAdapter
and there are references to my class in there, so I can only assume the compiled class is getting itnot the apk - which makes me even more bemused.

NoClassDef refers to classes that are referred by your class (in this case "MyFragmentPagerAdapter") not to the class itself (that's the ClassNotFoundException).
Check the import you have in the class "MyFragmentPagerAdapter" and be sure that they are all in the classpath of your android application i.e. by checking the libs folder and (eventually) other android libraries you are referring to in your project.
Hope this helps.

Related

Cannot create an object of my class - why?

Android studio version : 3.2
I created an class(my first class ever on android studio)
by clicking new on java folder but in main activity I am not able to create an object of that class
In this image you can see an error free Animal class but in main activity I am not able to create an object out of it
Everything else looks fine to me
On rebuilding the project I am getting and error stating "cannot resolve symbol Animal"
I have already tried
rebuilding my project,
clean build,
restarting android studio
Make sure that class Animal is public.
Make sure that class Animal is in your app's package name. This might be the reason if you have copied and pasted the class from a page on the internet.

FATAL EXCEPTION: main java.lang.NoClassDefFoundError: external library.jar.class Android studio

I am developing an android app which process speech and I have speech basic project (dependency for android project) ready on JAVA so I compiled JAVA project in eclipse in JAVA 7 compiler and exported that java project as a runnable jar.
I put this jar into my android studio project's libs folder and by right clicking selected AS A library, I got build successful message. But when I try run the android project it gives me error saying,
FATAL EXCEPTION: main Process: in.automator.automator, PID: 4242
java.lang.NoClassDefFoundError: jar_filename.Storage.class_in_jar_file
but the said class is there in the jar file, the only doubtful thing is the mentioned class file looks something like this
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioFormat.Encoding;
import marf.util.InvalidSampleFormatException;
public class MARFAudioFileFormat extends AudioFileFormat {
....
...
....
}
It refers javax.sound.sampled, so possibly that might causing the problem.
I tried searching on the google for the problem but didn't got the solution which can resolve the issue. I tried everything.
I am using JRE 7 in android studio and exporting java project in Compiler & itself.
How to resolve this error?
Thanks in advance.
You are correct, the problem lies in the fact that the library you're trying to depend on, in turn depends on the javax package hierarchy. Android does not provide javax, so those classes don't exist at runtime (even though they do exist in your computer's JDK, so your compiler doesn't complain - it's basically the same as not having a particular DLL installed that a program on your PC needs).
The NoClassDefFoundError is being thrown on the class that first references the invalid class dependencies, which is probably what's confusing. That class may indeed actually exist in your jar, but it requires classes that don't exist in order to complete its definition - so in effect, your class is not fully defined.
The only way around this is to figure out a way to do whatever you're after, without the javax namespace. I have heard of a few attempts to try to port javax.* into Android, but they never end well - many of the javax classes end up boiling down to native calls, which also won't exist on Android.

Why do I have java.lang.ClassnotFoundException while implementing location listener? (using ECLIPSE)

I know that similar questions have been on SO and I have read through and tried to implement the solutions mentioned but nothing is working. The specific problem is like this (using ECLIPSE):
I am trying to implement location awareness in one of the activities (triggered through menu selection) in my app. As long as my code looks like,
"public class MealHistoryActivity extends Activity {...}",
I don't get any error. As soon as I modify it to
public class MealHistoryActivity extends Activity implements ConnectionCallbacks, OnConnectionFailedListener, LocationListener {..",
it throws in the following error:
E/AndroidRuntime(5027): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.package.activities.MealHistoryActivity" on path: DexPathList[[zip file "/data/app/com.package.activities-2/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
What I have tried:
Adding up the library projects (support library as well as google play services) and cleaning all the projects.
Including Android Private Libraries for the application as well as the library projects.
Double-checking the API key in android manifest as well as the SHA-1 fingerprint key associated to it.
Using all the internet and network permissions in the manifest that are needed for location services.
Would greatly appreciate any ideas on how to solve this since I am stuck on it for the last few days!
The error suggests that the classes from the google-play-service project are not loading.
Make sure you are referencing a copy of the project in your workspace, and not the copy that is in the sdk directory. Try doing that or deleting the google service project from your workspace and redoing the steps in this guide.
Another possible cause for this is version mismatch. If your device's version is less than the version specified in the above mentioned guide than the google services project will not install on your device at all.

Importing from GitHub to Eclipse

I have actually managed to import a project from GitHub to Eclipse, but my problem is when I try to run the project it keeps saying fix errors on source files. Now I know that the code should work as the person who wrote this app has it in the Google App store. This keeps happening for me on all the projects I have downloaded.
Are the suggestions to what I am doing wrong?
Thanks for the replies, I am new to the whole programming world, so I am not sure whether or not the project has a build file or not.
As #Andres has said, my initial thought was that it was to do with the JDK/Android development kit being not the same as I have on Eclipse.
A link to the project is https://github.com/glebpopov/Hacker-News-Droid-App.
The first error I get says in the console window
Unable to resolve target 'android 7'
I also have this example for the beginning of the class Activity Helper:
public class ActivityHelper {
private static final String TAG = "ActivityHelper";
protected Activity mActivity;
protected SharedPreferences sharedPref = null;
And the error here for the second line of code say:
String cannot be resolved into a type
The error here for the third line of code says:
Activity cannot be resolved into a type
And there are plenty more across the whole code.
Some possible causes are:
The version published on GitHub has errors, and is not the same that is published on the google app store.
The project has dependencies that have not been resolved, because of your Maven (or similar product) configuration.
The jdk and/or android development kit required by the project are not the same as the one you have on your Eclipse

Android: Implementing SlidingMenu and ABS

I am trying to implement the SlidingMenu from https://github.com/jfeinstein10/SlidingMenu together with http://actionbarsherlock.com/ and I came across a problem that I could not fix myself that's why I turned to the issues page on SlidingMenu's GitHub. But 2 days and I got no reply so I am gonna ask it here.
Basically on the example project provided by jfeinstein10, I copied the SampleListFragment.java file and I've named it MenuListFragment.java on my project. I've made a couple of changes but those should not be an issue.
I also copied the menu.xml from the layout directory but changed its name attribute to the previous file including the package.
Project builds properly and no errors are encountered. Time to run the project.
Now on my project's main activity, when I call the setMenu(R.layout.menu); statement during runtime, it gives me an error which tells me something about ClassCastException (cannot cast com.dokgu.dota2stats.MenuListFragment to android.app.Fragment).
I've tried a couple of things like change the MenuListFragment.java to extend the android.app.Fragment instead of ListFragment but it didn't solve the problem and more issues came up because of that.
So please, can anyone help me with this please? I really want to make this work.
You can find the issue here as well: https://github.com/jfeinstein10/SlidingMenu/issues/546
Actionbarsherlork supports the lower lever version since android 2.x while the slidingmenu needs android 3.0+.Maybe you can add the android-support-v4.jar ,with some fragment and activity in slidingmenu project extending to.

Categories

Resources