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.
Related
I am trying to generate build key but it is showing
class FragmentNearbyBinding is public, should be declared in a file named FragmentNearbyBinding.java
public final class FragmentNearbyBinding implements ViewBinding {
^
as file under the build folder cannot be edited how can I get rid of this any help would be appreciated
I try invalidate cache and restart and ugrade android studio and build Gradle but still the problem remain the
same
I am developing an app in Android Studio. The package name is com.mycompany.myapp . Then, I want to reuse some codes from another package : com.theta360.sample.v2 .
To do this, I added a package named "com.theta360.sample.v2" in Android studio. And then, I simply copied the src directory of com.theta360.sample.v2 to project/app/src/main/, which also includes mycompany/myapp .
In MainActivity.java. I need to use class LogView from theta360.sample.v2. So, added "import com.theta360.sample.v2.view.LogView".
Then, I successfully build the app.
However, when I launched the app on device, I received error with message:
..Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.LogView" on path: DexPathList...
My questions are:
(1) why at run-time, the system try to look for android.view.LogView; LogView is from com.theta360.sample.v2.view . How to fix it?
(2) What's the correct way to use another package with source. Can I simply copy the codes as I did above?
I have solved my own question. The error is caused by LogView used in the layout xml. It should be changed to a name with it's package prefix.
This is my project stricture :
-com.android.test
--com.android.test.activity
---MainActivity.java
---SplashActivity.java
--com.android.test.ui
---ActivityBase.java
I created all activity in com.android.test.activity package and all activities extended from Base activity in com.android.test.ui package
But i don't know , when i creating activities on com.android.test.activity i see .R error in my Eclips , but when i creating activity on com.android.test package ,no need for importing the .R class.
I'm very beginner , please help
When you build an android project, the build tools generate a class called R.java. Because R is a class, it will exist in a package, just like all your other classes. By default, R will be generated in the package you've specified as the project package. For you, this should be com.android.test.
So your package structure is actually like this, even though R is in gen/, not src/.
-com.android.test
--R.java
--com.android.test.activity
---MainActivity.java
---SplashActivity.java
--com.android.test.ui
---ActivityBase.java
In Java, you will require an import statement if a class is not declared inside the package of the current class. So because com.android.test is not contained in com.android.test.activity, you will need to add import com.android.test.R; to MainActivity and SplashActivity, in order to use R in them.
If R is not being generated for you, check out Developing for Android in Eclipse: R.java not regenerating. I would possibly recommend learning to use IntelliJ IDEA, as I've encountered fewer strange build issues than in eclipse, but this is completely optional.
Another possible source of error is if you accidentally imported android.R, which is where the resources of the android SDK are referenced from. This could easily mess you up, since you could write code like R.string.foo and think you are using com.android.test.R.string.foo, but in reality, you are using android.R.string.foo, which may not exist. An easy way to find the problem is to explicitly say in your code, for example:
// you were originally getting an error here
MyActivity.this.getString(R.string.foo);
// try this to get a more obvious error, or see if it fixes it
MyActivity.this.getString(com.android.test.R.string.foo);
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.
I have a class which extends OrmliteSqliteOpenHelper in order to provide a persistence module for an Android application. The problem is, whenever I access this class from the test project in Eclipse, it throws a NoClassDefFound Exception. The source code appears to be fine as the package is imported and Eclipse has no highlighting to indicate anything will go wrong, but it does.
Here's the kicker: not only does everything else in the project work fine when accessed from the test project, but when I move this file into the test project voila it works perfectly! The issue is that it needs to be in the actual project in order for other components to make calls to it. Has anyone ever encountered this error or have any idea where I might be going wrong?
The class path is set correctly as all the other classes work and the test project always has the main project added to its class path. I also tried moving the OrmliteSqliteOpenHelper object to an inner class: this allowed the outer class to be visible and touched but as soon as it tried to instantiate the inner class the same error occurs. Also, if I copy this file into both projects in the same package, an error occurs stating "class ref in pre-verified class resolved to unexpected implementation" whenever something in the main project tries to access this class. Clearly, there is some difference in the way the class is being compiled/handled between the two projects.
I'm quite stumped :/ Thanks a ton for any assistance!
Well friends, I seem to have hacked together a solution for this issue:
I linked the entire 'src' folder from the main project into the one for the test project. Now, when it goes searching for the particular package my file is located in, it finds the file in its own source tree first. Hooray!
Why does this work? I haven't a clue, perhaps someone could enlighten us to that fact.
Does it work? For now...
There is how I fix the problem:
Remove OrmLite from Build Path of the test project.
Export them from the main project.