So I'm trying to make an HTTPS connection with Android, using a client certificate to validate. I've imported this certificate into a BKS store, and inserted in my res/raw.
All good.
However, when I come to run, I get a constant NullPointerException.
For context, I have a Connection class, extending Application, and the basic constructor should just open the cert as an InputStream and hold it ready. When I come to make the connection, this will be used. As follows (abridged):
public class RCPHandshake2 extends Application {
InputStream in;
public RCPHandshake2(){
super.onCreate();
in = getResources().openRawResource(R.raw.test);
}
}
The build-up to this runs fine, however, when we get to there (line 36 in real version), the stack trace is like so:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.<company>.t1v2/com.<company>.t1v2.SplashAndText}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
at android.app.ActivityThread.access$2200(ActivityThread.java:119)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4363)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.content.ContextWrapper.getResources(ContextWrapper.java:80)
at com.<company>.<component>.RCPHandshake2.<init>(RCPHandshake2.java:36)
at com.<company>.t1v2.SplashAndText.onCreate(SplashAndText.java:43)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
... 11 more
This seems unexpected. Has anyone come across similar before?
Eternal love and appreciation in exchange for any solutions/hints/tips/pointers from someone a bit more experienced with it that me!!
PS - Basically, even though I know the raw resource is there, and it appears in R.java, this seems to think it's unable to access it? Is this the case, or am I missing something blinding?
It seems you can't access resources from constructor, as the object has not been initilized yet. Try to call it later, when you really need this first time.
I had the same problem. Turned out that I used a wrong (empty) Context, so there actually were no resources to access via getResource() -> NullPointerException...
To open these resources with a raw InputStream, call Resources.openRawResource() with the resource ID, which is R.raw.filename.
Related
I've got a problem involving ClassNotFoundException. When i "Run as android application" from eclipse it works fine, but when i download it from Google Play (same source code but "Exported as signed app") it crashes with the following stacktrace:
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.larphoid.aeboing/com.larphoid.aeboing.BoingBoing}: java.lang.ClassNotFoundException: com.larphoid.aeboing.BoingBoing in loader dalvik.system.PathClassLoader[/data/app/com.larphoid.aeboing-1.apk]
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2703)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2797)
at android.app.ActivityThread.access$2300(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2132)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:143)
at android.app.ActivityThread.main(ActivityThread.java:4914)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: com.larphoid.aeboing.BoingBoing in loader dalvik.system.PathClassLoader[/data/app/com.larphoid.aeboing-1.apk]
at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
at android.app.Instrumentation.newActivity(Instrumentation.java:1033)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2695)
... 11 more
I've never had this problem before, and i'm not experienced yet in "reading" Stacktraces, so i'm pretty much puzzled at this. If anyone has some pointers or solution i'd appreciate it.
Thanks.
Gonna copy this from another question as I had the exact same problem before, and this is how I fixed it:
If you're running adt rev 22, you might not be exporting your libraries. I don't know why
Google did this exactly, but in the newest revolution you have to
check to make sure you're exporting your libraries yourself.
Right click your project, click Properties, and check Order & Export
under Java Build Path. It should look like this if you want to export
everything:
Afterwards, rebuild the application and export it again. It should be working now. To test, simply install the .apk to your phone manually before putting it on the store to make sure it works.
ClassNotFoundException indicates that something is missing on the classpath. Maybe you are missing a library in the classpath.
As the name suggests classNotFoundException in Java is a subclass of java.lang.Exception and Comes when Java Virtual Machine tries to load a particular class and doesn't found the requested class in classpath.
Maybe you change your package name while signing your apk.
Check your package and class name
I'm developing for 2.2 (minSdkVersion=8) and suddenly I'm getting this error:
arbitrarily rejecting large method (regs=75 count=28584)
rejected Lcom/Demo/Loyalty/SelectType;.onClick (Landroid/view/View;)V
Verifier rejected class Lcom/Demo/Loyalty/SelectType;
Class init failed in newInstance call (Lcom/Demo/Loyalty/SelectType;)
java.lang.VerifyError: com.Demo.Loyalty.SelectType
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1429)
at android.app.Instrumentation.newActivity(Instrumentation.java:1022)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
at android.app.ActivityThread.access$2300(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
at dalvik.system.NativeStart.main(Native Method)
It was working fine until now.
Note : SelectType class has around 16000 lines of code but that is not that large I guess.
I search a lot on net and from answers, I did following:
Clean the project
Reset the ADB
Restart emulator/device/eclipse
Checked that third party library field is checked in build path
But I'm still getting that error.
Any help appreciated.
The steps you've described probably won't help.
The thing is, it's not a Dalvik issue. Similar verifier is employed in the Oracle Java VM for example. Simple answer: your method is too complex. The error you see is mainly caused by too many:
parameters
local variables
exception handlers
code instructions
More precisely, the issue has been described in this thread: https://groups.google.com/forum/?fromgroups=#!topic/android-developers/4qNoIdBHYFc
To quote:
The value of (number of registers * number of instruction words) is
larger than 2^21. (...)
it's intended to
prevent the verifier from bloating up an app's native heap.
You can also see similar report here: http://www.mentby.com/Group/android-developers/verifyerror-arbitrarily-rejecting-large-method.html with pointers on how to resolve the issue:
Yep, the Dalvik compiler attempts to assign a "register" to every
local variable in the method. It should be able to handle that many,
but apparently can't. By making them instance variables you remove
the compiler's need/desire to "manage" them (and also make the method
a fair amount smaller).
So to solve it, you should generally break the large method (probably onClick()?) into smaller pieces. Also, converting local variables to class fields seemed to help some people with the same issue.
I am working on application which is host on android market. Sometimes (once a month ) I got a crash report:
Unable to instantiate application java.lang.ClassNotFoundException
App downloads are between 10,000-50,000. I don't know why this exceptions raise on some devices not all ( I tested it on 3 different devices & I couldn't re-produce it at my end).
I read articles/suggestions on different android forums regarding the issue but I didn't succeed in solving it. Does anyone face similar issue & suggest me what should I do?
Note:
I am extending application class like this
public class MyApplication extends Application {
}
I register it in the manifest.xml like this
<application android:icon="#drawable/app_icon"
android:label="#string/my_app_name" android:name="MyApplication">
Stack Trace :
java.lang.RuntimeException: Unable to instantiate application com.xyz.MyApplication java.lang.ClassNotFoundException: com.xyz.MyApplication in loader dalvik.system.PathClassLoader[/mnt/asec/com.xyz-1/pkg.apk]
at android.app.ActivityThread$PackageInfo.makeApplication(ActivityThread.java:650)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4238)
at android.app.ActivityThread.access$3000(ActivityThread.java:126)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2076)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4633)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: com.xyz.MyApplication in loader dalvik.system.PathClassLoader[/mnt/asec/com.xyz-1/pkg.apk]
at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
at android.app.Instrumentation.newApplication(Instrumentation.java:942)
at android.app.ActivityThread$PackageInfo.makeApplication(ActivityThread.java:645)
I don't know why application crash on some devices not all.
Some other similar questions indicate that this can be a user error. "/mnt/asec/..." indicates that the app is running from the sdcard. If the sdcard is removed it could cause this error. 3rd party apps or rooted devices can probably move an app to the sdcard even if its not allowed by the manifest.
Similar Question
I think the problem is with getApplication() which I have used in 10 different place. So I have used singleton pattern to solve this.
public class MyApplication extends Application {
private static MyApplication me;
#Override
public void onCreate() {
super.onCreate();
me = this ;
}
public static MyApplication getInstance() {
return me;
}
}
Now I have used getApplication() like this
MyApplication application = MyApplication.getInstance();
insted of
MyApplication application = (MyApplication) getApplication();
I have uploaded the fixed version on the market & now waiting if there is anymore this kind of crash. If everything goes perfect ( if no more crash in 2 weeks) then I will close the question. In meanwhile anyone has better idea or know the solution , please share it. Thanks,
In my case, I was compiling and signing with Eclipse ADT (with File > Export > Export Android Application...) but missing some classes if I decompile my .apk.
To solve it I use "Export an unsigned APK" and sign it using jarsigner and zipalign.
android:name=".MyApplication"
also consider adding full package
I just released an update and got an exception report from the wild after someone updated a previously working application.
The same code works on my phone, the resource is a raw file and not dependent on device, local dependent or anything.
I had a similar thing happen before while developing the update, I cleaned the project and it resolved it.
However this doesn't seem to be a good solution as I cannot verify the fix, don't want to just shove a version out in case it fixes the issue.
Any advice as to cause or resolution would be massively appreciated!
Stack Trace below:
java.lang.RuntimeException: Unable to create service com.beltane.apppro.TickerService:
android.content.res.Resources$NotFoundException: Resource ID #0x7f040005 at
android.app.ActivityThread.handleCreateService(ActivityThread.java:2969) at
android.app.ActivityThread.access$3300(ActivityThread.java:125) at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:2087) at
android.os.Handler.dispatchMessage(Handler.java:99) at
android.os.Looper.loop(Looper.java:123) at
android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method) at
java.lang.reflect.Method.invoke(Method.java:521) at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858) at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) at
dalvik.system.NativeStart.main(Native Method) Caused by:
android.content.res.Resources$NotFoundException: Resource ID #0x7f040005 at
android.content.res.Resources.getValue(Resources.java:892) at
android.content.res.Resources.openRawResourceFd(Resources.java:854) at
android.media.MediaPlayer.create(MediaPlayer.java:647) at
com.beltane.apppro.TickerService.onCreate(TickerService.java:42) at
android.app.ActivityThread.handleCreateService(ActivityThread.java:2959) ... 10 more
For anyone facing a similar problem my resolution as horrible as it is:
Prepare a new release deleting all intermediate generated files, doing a clean build and re-releasing.
I trapped all media player creation code in try catch blocks for resource not found exceptions and pop up a toast explaining to the user what has occurred and to contact for a resolution.
This is pretty nasty but given that this should never occur...
I haven't heard either way from the user that returned the crash report, I have had other good feedback since, I'll update if I hear further either way.
This question already has answers here:
Java.lang.verifyerror how do I fix or even find out the root cause?
(4 answers)
Closed 9 years ago.
Hello I get this error every time I'm trying to open an activity
java.lang.VerifyError: com.karriapps.smartsiddur.Saharit
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1429)
at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
at android.app.ActivityThread.access$2300(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
at dalvik.system.NativeStart.main(Native Method)
Can someone direct me to a solution or a way to check where the problem is coming from
thanks
A VerifyError means your compiled bytecode is referring to something that Android cannot find. In this case, it would appear that you have a reference to a com.karriapps.smartsiddur.Saharit class that Android cannot find.
As CommonsWare mentioned, a VerifyError means that you're trying to reference a class that Dalvik isn't able to load.
It's possible that this class is missing.
It's also possible that you're trying to use framework methods for an API level greater than what's present on the device, and therefore the class is being rejected as invalid.
Try setting your compiler's build level to API Level 7, which corresponds to Android 2.1. (Don't forget to set your AndroidManfest.xml's targetSdkVersion to "7" as well.) This will cause any framework calls that don't exist to raise a compiler error.
You also might want to look at the lines above/below the stack trace you received to see if there's any additional information from the verifier indicating why verification failed.
I found an interesting case that has evidence in runtime. I use:
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="18" />
So some of new Android 4 capabilities are not implenented in Android 2.3 like this one:
imageView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
Ok I can manage it so runtime will not execute it, simply:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
This works well but what about Exception handling?
} catch (NetworkOnMainThreadException nomte) {
// log this exception
} catch (SocketTimeoutException socketTimeoutException) {
// log this exception
}
NetworkOnMainThreadException is not implemented in Android 2.3 so when the class is loaded the exception java.lang.VerifyError occurs.
I ran across this problem today and as CommonsWare mentioned, the issue is that my compiled bytecode was referring to something that no longer existed. But what should you do about it?
Since I'm using Eclipse SDK the simple solution for me was to perform an Eclipse's Project → Clean to remove the pre-compiled byte code in my project that was causing the problem. The Project clean put simply allowed eclipse to perform a full fresh rebuild of my project after the clean.