Since a couple of weeks, I'm seeing more and more crashes of my app with the following exception
Fatal Exception: java.lang.NoClassDefFoundError
android.os.AsyncTask
This code has run for month without any issue, and it seems now to fail on some devices (75% android 2.3.x and 25% android 4.0.3)
It fails when I create a new instance of a class which extends AsyncTask.
I create this class from the UI thread.
How can that class be not found as it's defined within the SDK ?
Yes, looks like it is a problem with one of the versions of Google play Services. See https://code.google.com/p/android/issues/detail?id=81083
A work around is to add:
try {
Class.forName("android.os.AsyncTask");
}
catch(Throwable ignore) {
// ignored
}
into your Application#onCreate()
this appears to ensure that the root classloader loads AsyncTask so that it is then available from within Play Services.
It looks like yet another Google Play Services bug...
https://groups.google.com/forum/#!topic/google-admob-ads-sdk/_x12qmjWI7M
Edit: confirmed by Google staff => https://groups.google.com/d/msg/google-admob-ads-sdk/_x12qmjWI7M/9ZQs-v0ZZTMJ
Same issue here.
I see them for 95% of the cases on android 4.0.3 devices. remaining 5% for 2.3 devices
Errors are randomly occurring from different parts of the code.
Some examples:
java.lang.NoClassDefFoundError: android/os/AsyncTask
at android.webkit.WebView.setupPackageListener(WebView.java:1305)
at android.webkit.WebView.<init>(WebView.java:1176)
at android.webkit.WebView.<init>(WebView.java:1136)
and
java.lang.NoClassDefFoundError: android/os/AsyncTask
at android.webkit.WebView.setupPackageListener(WebView.java:1354)
at android.webkit.WebView.access$10900(WebView.java:363)
at android.webkit.WebView$PrivateHandler.handleMessage(WebView.java:10411)
and
java.lang.NoClassDefFoundError: android.os.AsyncTask
at android.webkit.WebView.setupPackageListener(WebView.java:1385)
at android.webkit.WebView.<init>(WebView.java:1192)
at android.webkit.WebView.<init>(WebView.java:1150)
at android.webkit.WebView.<init>(WebView.java:1135)
at android.webkit.WebView.<init>(WebView.java:1106)
at android.webkit.WebView.<init>(WebView.java:1093)
at com.google.android.gms.ads.internal.util.g.f(SourceFile:400)
at com.google.android.gms.ads.internal.util.g.a(SourceFile:385)
it is completely unclear why these errors are happening. usually i dont see anything in the stacktrace pointing to my code.
I have the same error:
BuscaDatosJugador().execute(participante.getIconImageUrl(),String.valueOf(altoenvio), String.valueOf(contador));
My solution:
final Runnable r = new Runnable()
{
public void run()
{
try {
--- my code ---
}
};
r.run();
}
I experienced same error on android 2.3.3, but same app was stable on 4.0+. It's a Freemium and the error occurs only when in FREE mode, which runs Google Admob adverts. So the error has to be connected with this but I do no have the detail. Here is how I solved the problem:
Execute a statement that would cause the AsyncTask class to be loaded before loading the ads.
steps 1: Create a dummy AsyncTask extension class
public class DummyAsyncTask extends AsyncTask<Void, Void, Void>{
#Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
return null;
}
}
step 2: just in your main activity:
public class MainActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new DummyAsyncTask();
.
.some code
.
load your ads here
}
}
After step 2 above, all other code section that instantiates AsyncTask extended class run normally.
Related
I have a problem with jsoup on android. I have seen the other posts and tried solutions that were suggested there ( re-adding the jars, calling android fix tool, etc.)
I have added the jsoup jar to my android project (using build path), and added the required
internet permission to my manifest.
<uses-permission android:name="android.permission.INTERNET" />
but when I am trying to run my application I am getting
Could not find method org.jsoup.Jsoup.connect, referenced from method com.example.test.MainActivity.onCreate
I have tried to use the android fix tool but it did not solve the problem.
All I have is a main activity and I am trying to call
Document doc = Jsoup.connect("http://en.wikipedia.org/").get();
attached is part of my code
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
Document doc = Jsoup.connect("http://en.wikipedia.org/").get();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
You are trying to run your connection in your main thread. Use AsyncTask and it will work.
I.E.
public class JsoupParser extends AsyncTask...
Why you have to use AsyncTask for network connections in android?
AsyncTask is an abstract helper class that enables you to use the UI thread correctly, while performing background operations in a different thread, without having to really handle threads or controllers. Since android is implemented using a single thread model, each time you launch an application, a new thread will be created.
Imagine you have a single thread model where you at a button click will parse a website using Jsoup. This would have worked fine in earler android versions, though you would have had a non-responsive screen until the network operation is done. The AsyncTask will run in the background enabling your screen to still be responsive while another thread takes care of the network communication.
Take a look in the API:
AsyncTask
NetworkOnMainThreadException
Delete all statements like:
System.out.println(something);
It worked for me, realizing this took me 2 hours.
In you normal activity
use this
public static int SDK_INT = android.os.Build.VERSION.SDK_INT;
and before fetching Document
write this inside try block
if (SDK_INT >= 10) {
ThreadPolicy tp = ThreadPolicy.LAX;
StrictMode.setThreadPolicy(tp);
}
it worked for me
I need to implement Sentry for my android app, I try to find an example about how I have to implement this, but I can't find it.
I saw the Sentry documentation in http://sentry.readthedocs.org/en/latest/developer/client/index.html#server_name
But I have some questions.
If my app crash, the exception will be captured?
Should I put this code line into my try/catch?
var $resultId = myClient->captureException($myException); (in android code)
If somebody has a sample in android I will be grateful.
Thank you!
I am a little late but I just recently released a Sentry client for Android. It's in its early stages so feel free to pull request any changes that you see.
https://github.com/joshdholtz/Sentry-Android
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Sentry will look for uncaught exceptions from previous runs and send them
Sentry.init(this, "YOUR-SENTRY-DSN");
}
}
Maybe try using something like BugSense? http://www.bugsense.com/
If it definitely has to be Sentry, then look at this example: https://stackoverflow.com/a/755151/349012
It shows you how to set your own uncaught exception handler so you can try and upload to Sentry.
I do have an old app that refuses to work on Android 4.1 devices. It's the NetworkOnMainThreadException that jumps in here.
So I tried to permit this with the following steps - but these don't work. I tested that with the 4.1 emulator. What is really needed to come around that error - app rewrite is no option. Currently I exclude 4.1 devices from my apps.
A class file ...
public class StrictModeWrapper {
static {
try {
Class.forName("android.os.StrictMode");
} catch (Exception exception) {
throw new RuntimeException(exception);
}
}
public static void checkAvailable() {
}
#SuppressLint("NewApi")
public static void setThreadPolicy() {
StrictMode.ThreadPolicy strictModeThreadPolicy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(strictModeThreadPolicy);
}
}
... called in an extended Application class:
public class MyApplication extends Application {
#Override
public void onCreate() {
super.onCreate();
try {
StrictModeWrapper.checkAvailable();
StrictModeWrapper.setThreadPolicy();
} catch (Throwable throwable) {
}
}
}
The extended Application class is registered in the Manifest and working.
Nothing seems to have changed in StrictMode since api 11. It must be the changes in some other android classes you used, that caused a StrictMode policy violation.
The Android documentation itself says
"But don't feel compelled to fix everything that StrictMode finds. "
But since its a NetworkOnMainThreadException you must do a thorough check. See all network communications in your app, and ensure that they are not blocking your main thread.
And make sure you remove/disable the StrictMode code in your release build, as it is only a developer tool to identify accidental mistakes.
Update:
Your app crashed because :
You had not blocked the execution of StrictMode policy setting code in your release build. It should be executed only while testing.
Something changed in the StrictMode class that caused the strict mode policy to reset after onCreate.
I have 2 questions :
Doesnt the crash indicate that the StrictMode policy was working? There was a policy violation and hence it crashed.
Doesnt it indicate that there is some network code in your app that blocks the main thread?
StrictMode behaves different on Android version >= 16 than prior releases. The docs suggest to issue StrictMode calls in onCreate() of an extended Application, Activity, etc.. At least onCreate() in an extended Application works different now and proofes the docs wrong (as of today).
Here's the StrictMode doc that describes how to add StrictMode calls to an extended application for example (that's wrong as of today):
StrictMode
Here's a Google Code issue that describes the problem and gives a workaround:
Google Code Issue 35298
Hey,
So im newish to android and Im lost because although my code uses AsyncTask for it's heavy lifting I am still getting a ANR error when I run my one class. So here is the relevant peices of my code:
package com.cody.color;
import android.app.Activity;
import android.os.Bundle;
public class Play extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
System.out.println("about to start threader");
new GuiThreader().execute();
System.out.println("Threader finished");
setContentView(R.layout.colorboard_small);
}
}
package com.cody.color;
import android.os.AsyncTask;
public class GuiThreader extends AsyncTask<Void, Void, Void>{
#Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
System.out.println("threader moving");
GUIdriver game = new GUIdriver();
game.play();
return null;
}
}
There aren't any problems with the code above that would cause an ANR, so most likely one of the following is happening:
You are trying to modify the user interface (UI) from doInBackground
An exception is occuring in your doInBackground
If you post the LogCat output or stacktrace when you get the error then it will be easier to identify.
welcome to the world of JAVA programming :) When you encounter an error your best tool to solve it is usually going to be a stacktrace. You need to learn how to analyze a stacktrace and also to include it in your questions...
What is a stacktrace you ask? The simplistic answer is that a stacktrace is an error message (although it contains a lot more info than only that). In case of an exception it tells the programmer WHAT happened, WHERE it happened and WHEN it happened. If you want to program in JAVA you need to get yourself quite familiar with them. I recommend you read some JAVA tutorials before diving into Android development.
As for your code, without the stacktrace I cannot say more... there is no obvious error except the fact that game.play(); looks like it might contain some changes to the UI, which are not allowed in another thread than the main thread (that's why AsyncTask has onPostExecute());
Im having a problem starting or running any activity unit tests from within eclipse.
Even i start a clean project and make a simple test class it always prints to the console:
[2010-10-05 13:10:24 - testAndroid] Collecting test information
[2010-10-05 13:10:25 - testAndroid] Test run failed: Test run incomplete. Expected 2 tests, received 0
Any ideas ?
Just for testing, I have created a fresh Android project called Demo with a test project called DemoTest
The main activity to test is called Main and I have created a simple testclass MainTest that looks like this:
package net.demo.test;
import android.test.ActivityInstrumentationTestCase2;
import net.demo.Main;
public class MainTest extends ActivityInstrumentationTestCase2<Main>
{
public MainTest()
{
super("net.demo", Main.class);
// TODO Auto-generated constructor stub
}
}
My tests used to run fine before, but suddenly I cant run any of them, they all fail with the same error, even I create new a project. It seems like it something to do with Eclipse or and not with the Code.
Update:
Seems like extending SingleLaunchActivityTestCase<Main> is working, but still got no clue about how to make ActivityInstrumentationTestCase2<Main> working.
I had no regression problems. I just couldn't get the example to work. I finally fixed it by defining two constructors:
public MainActivityTest(String pkg, Class<MainActivity> activityClass) {
super("com.myapp", MainActivity.class);
}
public MainActivityTest() {
super("com.myapp", MainActivity.class);
}
It turned out that most emulators before 2.3.3 were silently swallowing the error generated when construction went wrong.
You must put at least 2 methods (i.e 2 test cases) into the Test class. even methods without definition inside can do the trick