Android ClassNotFoundException error at runtime - java

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.

Related

Error building cordova android project with uppercase letters in package name

I have a Cordova app several years old, which has capital letters in its package name.
It was working well before, with older versions of Cordova and building in Eclipse.
Now I cannot build it with Cordova 6.1.1 and gradle. It gives errors.
I have detected that it doesn't create the folder structure and files for the app class at src folder. It should create PackageFirst folder, then inside it PackageSecond since package name is PackageFirst.PackageSecond
I generated and copied those files myself. The cordova prepare command deletes the folder, so I run prepare and then copy the folder back in. Then I try compile. But it breaks again and the build doesn't succeed, with this error:
Here is my LogCat.
BUILD FAILED at com.android.dx.command.dexer.Main.run(Main.java:277)
at com.android.dx.command.dexer.Main.main(Main.java:245)
at com.android.dx.command.Main.main(Main.java:106)
Caused by: com.android.dx.cf.iface.ParseException: class name (PackageFirst/PackageSecond/MainActivity) does not match path (packagefirst/packagesecond/MainActivity.class)
at com.android.dx.cf.direct.DirectClassFile.parse0(DirectClassFile.java:520)
at com.android.dx.cf.direct.DirectClassFile.parse(DirectClassFile.java:406)
I have been researching and the only solution I find, is that I need to make the package name all lowercase. Since this app is published at Google Play, I believe I cannot do that since it would be a different package name.
I have been able to build this perfectly for years until now.
Can anyone give me a solution to this problem that doesn't involve changing the package name? Thank you so much for any help!
EDIT:
I could get the package to build successfully, but it doesn't run on the device. With all the testing I had left uppercase in one place and lowercase in another. Once I fixed that, it built, but when trying to run it on the device, I get the following error:
Error: Failed to install apk to device: pkg: /data/local/tmp/android-debug.apk
Failure [INSTALL_PARSE_FAILED_MANIFEST_MALFORMED]
Kindly name your packages in lower case. As I can see from error you posted above
com.android.dx.cf.iface.ParseException: class name
(PackageFirst/PackageSecond/MainActivity) does not match path
(packagefirst/packagesecond/MainActivity.cla ss)
the package names have different case. This might be a cause

Unable to run Google's example - Android App Engine Mobile Backend

I am trying to run this example. I reached upto "Modify the Client Application" and I am then having the following errors (and below is my file organization screenshot:
At this stage, as you can see below, I have the following issues in MainActivity.java.
activity_main.xml is called out but the project in its layout folder only has activity_register.xml (see here)
"Checkinendpoint" class is undefined but there is a CheckInEndPoint.java in the "MobileAssistant-AppEngine" project, as you can see in the image below. Is that not supposed to get picked up in MainActivity.java - how to import that? (doesnt work like a normal library import, i.e. "MobileAssistant-AppEngine" is not a library that "MobileAssistant" can reference.
Same question as above for CheckIn class that is also undefinted in the picture below.
Delete deviceinfoendpoint-v1-generated-source & messageendpoint-v1-generated-source from the Project-MobileAssistant and then right click on the MobileAssistant-appengine project then click on Generate Cloud EndPoint Library from Google Sub menu.

Android : Activity error in other package

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

R cannot be resolved to a variable, usual fix doesn't seem to work

I am trying to compile my android app that I am crating by following the google tutorial at https://developer.android.com/training/index.html
I have run into a problem where I get the error "R cannot be resolved to a variable"
I followed the suggestions from the other threads such as cleaning/rebuiding, removing all import android.R, and reinstalling my Android SDK Build-tools for every version
Here is a copy of the tutorial project with the issue MyFirstApp.zip
I have taken the suggestions found in other threads, but their solutions have been unsuccessful.1 2 3 4 Please help!
Please let me know if there is any other information you need to fix this issue.
I have seen your code there is an error inside main_activity_actions.xml which is inside your menu folder
It does not find #string/action_search which must be present in strings.xml
So put <string name="action_search">Search here</string> inside your strings.xml
and clean and build the project.
I hope the error will be solved.
It usually append when you have an error with your XML file.
Maybe you try to use an XML variable who doesn't exist anymore. It will not give you any error on your XML.
Verify your XML variable on your file 'Value' and which you call from your Java file

ClassNotFoundException: previously working Android Project

I had a working android project but needed to start from scratch. I copied all my source files and layout files over. There are no compilation errors but when I try to run, I get the following error.
11-27 17:21:56.793: E/AndroidRuntime(1450): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{projects.mobile.mapappproject/projects.mobile.mapappproject.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "projects.mobile.mapappproject.MainActivity" on path: /data/app/projects.mobile.mapappproject-1.apk
I am running eclipse juno and trying to run on Android 4.2. Thanks !
Rather than doing copy paste try following
1.Create an android application project
2.Delete the files which was created Eg.MainActivity and layout.
2.Right click on project name and click import ,then from File system.Go to your project directory to import the source from there
3.select that, then finish.
4.Clean and build then run
Try these
Add the Activity to AndroidManifest.xml
Is there a difference in your folder names ? For example projects.mobile.mapappproject-1 or projects.mobile.mapappproject !
I copied all my source files and layout files over.
It looks like you forgot to copy over the entries that existed in AndroidManifest.xml, however. Each Activity has an entry there and that entry is required for the framework to find and launch the UI.
I had the same problem, but I found out that a library reference path wasn't right. After I fixed that everything worked fine.
You can check this at Right-click on your project => Properties => Android -> (Library)
Just for the record in case I can help someone :
I had a project that somehow didnt work after importing it from git. I solved this by giving the name of the activity explicitly.
example:
before:
<activity android:name=".Main" [...] /> (relative)
after: <activity android:name="com.example.package.Main" [...] /> (absolute)
This may also occur if you change the manifest package and keep your relative reference (which I think is default).
In my case, following 2 steps fixed the problem
a. Setting compileSdkVersion, buildToolsVersion, targetSdkVersion to the same SDK version in build.gradle.
b. I was missing a dot in application class name in AndroidManifest.xml (Ex. android:name=".ui.ApplicationLoader")

Categories

Resources