Android java uncaught exception

Android Development: «thread exiting with uncaught exception»

I’m trying to create my first Android App (a game) but I’m having some difficulties getting started. When I run my code I get this error log:

05-25 02:41:51.022: WARN/dalvikvm(634): threadid=1: thread exiting with uncaught exception (group=0x4001d800) 05-25 02:41:51.040: ERROR/AndroidRuntime(634): FATAL EXCEPTION: main 05-25 02:41:51.040: ERROR/AndroidRuntime(634): java.lang.RuntimeException: Unable to start activity ComponentInfo: java.lang.NullPointerException 05-25 02:41:51.040: ERROR/AndroidRuntime(634): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 05-25 02:41:51.040: ERROR/AndroidRuntime(634): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 05-25 02:41:51.040: ERROR/AndroidRuntime(634): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 05-25 02:41:51.040: ERROR/AndroidRuntime(634): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 05-25 02:41:51.040: ERROR/AndroidRuntime(634): at android.os.Handler.dispatchMessage(Handler.java:99) 05-25 02:41:51.040: ERROR/AndroidRuntime(634): at android.os.Looper.loop(Looper.java:123) 05-25 02:41:51.040: ERROR/AndroidRuntime(634): at android.app.ActivityThread.main(ActivityThread.java:4627) 05-25 02:41:51.040: ERROR/AndroidRuntime(634): at java.lang.reflect.Method.invokeNative(Native Method) 05-25 02:41:51.040: ERROR/AndroidRuntime(634): at java.lang.reflect.Method.invoke(Method.java:521) 05-25 02:41:51.040: ERROR/AndroidRuntime(634): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 05-25 02:41:51.040: ERROR/AndroidRuntime(634): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 05-25 02:41:51.040: ERROR/AndroidRuntime(634): at dalvik.system.NativeStart.main(Native Method) 05-25 02:41:51.040: ERROR/AndroidRuntime(634): Caused by: java.lang.NullPointerException 05-25 02:41:51.040: ERROR/AndroidRuntime(634): at com.stickfigs.nmg.NMG.onCreate(NMG.java:32) 05-25 02:41:51.040: ERROR/AndroidRuntime(634): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 05-25 02:41:51.040: ERROR/AndroidRuntime(634): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 05-25 02:41:51.040: ERROR/AndroidRuntime(634): . 11 more 05-25 02:41:51.062: WARN/ActivityManager(59): Force finishing activity com.stickfigs.nmg/.NMG 

I think the problem is this «thread exiting with uncaught exception» part, I have no idea what the exception could be or what’s causing it. Here is my code: NMGView.java: package com.stickfigs.NMG;

import android.content.Context; import android.os.Bundle; import android.util.AttributeSet; import android.view.SurfaceHolder; import android.view.SurfaceView; class NMGView extends SurfaceView implements SurfaceHolder.Callback < class NMGThread extends Thread < //State-tracking constants public static final int STATE_LOSE = 1; public static final int STATE_PAUSE = 2; public static final int STATE_READY = 3; public static final int STATE_RUNNING = 4; public static final int STATE_WIN = 5; /** The state of the game. One of READY, RUNNING, PAUSE, LOSE, or WIN */ private int mode; /** Handle to the surface manager object we interact with */ private SurfaceHolder surfaceHolder; public NMGThread(SurfaceHolder surfaceHolderc, Context contextc) < // get handles to some important objects surfaceHolder = surfaceHolderc; context = contextc; >/** * Restores game state from the indicated Bundle. Typically called when * the Activity is being restored after having been previously * destroyed. * * @param savedState Bundle containing the game state */ public synchronized void restoreState(Bundle savedState) < synchronized (surfaceHolder) < setState(STATE_PAUSE); >> /** * Sets the game mode. That is, whether we are running, paused, in the * failure state, in the victory state, etc. * * @param mode one of the STATE_* constants * @param message string to add to screen or null */ public void setState(int modec) < synchronized (surfaceHolder) < mode = modec; >> > @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) < // TODO Auto-generated method stub >@Override public void surfaceCreated(SurfaceHolder holder) < // TODO Auto-generated method stub >@Override public void surfaceDestroyed(SurfaceHolder holder) < // TODO Auto-generated method stub >/** Handle to the application context, used to e.g. fetch Drawables. */ private Context context; /** The thread that actually draws the animation */ private NMGThread thread; public NMGView(Context context, AttributeSet attrs) < super(context, attrs); // register our interest in hearing about changes to our surface SurfaceHolder holder = getHolder(); holder.addCallback(this); // create thread only; it's started in surfaceCreated() thread = new NMGThread(holder, context); setFocusable(true); // make sure we get key events >/** * Fetches the animation thread corresponding to this LunarView. * * @return the animation thread */ public NMGThread getThread() < return thread; >> 
package com.stickfigs.nmg; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.Window; import com.stickfigs.nmg.NMGView.NMGThread; public class NMG extends Activity < /** Called when the activity is first created. */ /** A handle to the thread that's actually running the animation. */ private NMGThread nMGThread; /** A handle to the View in which the game is running. */ private NMGView nMGView; @Override public void onCreate(Bundle savedInstanceState) < super.onCreate(savedInstanceState); //Turn off the window's title bar // TODO Turn off the status bar requestWindowFeature(Window.FEATURE_NO_TITLE); // tell system to use the layout defined in our XML file setContentView(R.layout.nmg_layout); // get handles to the LunarView from XML, and its LunarThread nMGView = (NMGView) findViewById(R.id.nmg); nMGThread = nMGView.getThread(); if (savedInstanceState == null) < // we were just launched: set up a new game nMGThread.setState(NMGThread.STATE_READY); Log.w(this.getClass().getName(), "SIS is null"); >else < // we are being restored: resume a previous game nMGThread.restoreState(savedInstanceState); Log.w(this.getClass().getName(), "SIS is nonnull"); >> > 
public final class R < public static final class attr < >public static final class drawable < public static final int icon=0x7f020000; >public static final class id < public static final int nmg=0x7f050000; >public static final class layout < public static final int nmg_layout=0x7f030000; >public static final class string < public static final int app_name=0x7f040001; public static final int hello=0x7f040000; >> 

Источник

Читайте также:  Java parsing string to double

Need to handle uncaught exception and send log file

When my app creates an unhandled exception, rather than simply terminating, I’d like to first give the user an opportunity to send a log file. I realize that doing more work after getting a random exception is risky but, hey, the worst is the app finishes crashing and the log file doesn’t get sent. This is turning out to be trickier than I expected 🙂 What works: (1) trapping the uncaught exception, (2) extracting log info and writing to a file. What doesn’t work yet: (3) starting an activity to send email. Ultimately, I’ll have yet another activity to ask the user’s permission. If I get the email activity working, I don’t expect much trouble for the other. The crux of the problem is that the unhandled exception is caught in my Application class. Since that isn’t an Activity, it’s not obvious how to start an activity with Intent.ACTION_SEND. That is, normally to start an activity one calls startActivity and resumes with onActivityResult. These methods are supported by Activity but not by Application. Any suggestions on how to do this? Here are some code snips as a starting guide:

public class MyApplication extends Application < defaultUncaughtHandler = Thread.getDefaultUncaughtExceptionHandler(); public void onCreate () < Thread.setDefaultUncaughtExceptionHandler (new Thread.UncaughtExceptionHandler() < @Override public void uncaughtException (Thread thread, Throwable e) < handleUncaughtException (thread, e); >>); > private void handleUncaughtException (Thread thread, Throwable e) < String fullFileName = extractLogToFile(); // code not shown // The following shows what I'd like, though it won't work like this. Intent intent = new Intent (Intent.ACTION_SEND); intent.setType ("plain/text"); intent.putExtra (Intent.EXTRA_EMAIL, new String[] ); intent.putExtra (Intent.EXTRA_SUBJECT, "log file"); intent.putExtra (Intent.EXTRA_STREAM, Uri.parse ("file://" + fullFileName)); startActivityForResult (intent, ACTIVITY_REQUEST_SEND_LOG); > public void onActivityResult (int requestCode, int resultCode, Intent data) < if (requestCode == ACTIVITY_REQUEST_SEND_LOG) System.exit(1); >> 

Источник

Читайте также:  Php if логическое or пример

Ideal way to set global uncaught exception Handler in Android

I want to set a global uncaught exception handler for all the threads in my Android application. So, in my Application subclass I set an implementation of Thread.UncaughtExceptionHandler as default handler for uncaught exceptions.

Thread.setDefaultUncaughtExceptionHandler( new DefaultExceptionHandler(this)); 

In my implementation, I am trying to display an AlertDialog displaying appropriate exception message. However, this doesn’t seem to work. Whenever, an exception is thrown for any thread which goes un-handled, I get the stock, OS-default dialog («Sorry!-Application-has-stopped-unexpectedly dialog»). What is the correct and ideal way to set a default handler for uncaught exceptions?

If you wish to log your exceptions, have a look at acra.ch. ACRA allows you to send error-reports to a Google-Doc or to you via E-Mail.

5 Answers 5

That should be all you need to do. (Make sure you cause the process to halt afterward — things could be in an uncertain state.)

The first thing to check is whether the Android handler is still getting called. It’s possible that your version is being called but failing fatally and the system_server is showing a generic dialog when it sees the process crash.

Add some log messages at the top of your handler to see if it’s getting there. Print the result from getDefaultUncaughtExceptionHandler and then throw an uncaught exception to cause a crash. Keep an eye on the logcat output to see what’s going on.

«throwing an uncaught exception to cause a crash after you handled the error» is still important. I just experienced it. My app got locked after I handled the exception and did not throw an uncaught exception.

@OneWorld Jepp same here — at least for the locking part — there seems to be no way to «save» the app from crashing after all.

@Zainodis I posted a mildly off-topic answer to this question giving a link to Crittercism — I think they have a feature that allows you to «save» the app from crashing after all. Not sure — I am only using the free version atm.

I posted the simple solution for custom handling of Android crashes a long ago. It’s a little hacky however it works on all Android versions (including the Lollipop).

First a little bit of theory. The main issues when you use uncaught exception handler in Android come with the exceptions thrown in the main (aka UI) thread. And here is why. When the app starts system calls ActivityThread.main method which prepares and starts the Main looper of your app:

public static void main(String[] args)

Main looper is responsible for processing messages posted in the UI thread (including all messages related to UI rendering and interaction). If an exception is thrown in the UI thread it will be caught by your exception handler, but since you’re out of loop() method you won’t be able to show any dialog or activity to the user as there’s no one left to process UI messages for you.

Читайте также:  Html text style editor

The proposed solution is quite simple. We run Looper.loop method by our own and surround it with try-catch block. When an exception is caught we process it as we want (for example start our custom report activity) and call Looper.loop method again.

The following method demonstrates this technique (it should be called from the Application.onCreate listener):

private void startCatcher() < UncaughtExceptionHandler systemUncaughtHandler = Thread.getDefaultUncaughtExceptionHandler(); // the following handler is used to catch exceptions thrown in background threads Thread.setDefaultUncaughtExceptionHandler(new UncaughtHandler(new Handler())); while (true) < try < Looper.loop(); Thread.setDefaultUncaughtExceptionHandler(systemUncaughtHandler); throw new RuntimeException("Main thread loop unexpectedly exited"); >catch (Throwable e) < showCrashDisplayActivity(e); >> > 

As you can see the uncaught exception handler is used only for the exceptions thrown in background threads. The following handler catches those exceptions and propagates them to the UI thread:

static class UncaughtHandler implements UncaughtExceptionHandler < private final Handler mHandler; UncaughtHandler(Handler handler) < mHandler = handler; >public void uncaughtException(Thread thread, final Throwable e) < mHandler.post(new Runnable() < public void run() < throw new BackgroundException(e); >>); > > 

Источник

How do I find and fix an uncaught exception?

I don’t know how to figure out where an uncaught exception is coming from. I have looked through the stackoverflow posts and tried looking for several of the solutions. As far as I can tell, I have my activity, «Monitor», listed in my manifest and it is part of the package. But I get a null pointer exception when I start the «Monitor» activity. What should I be looking for to find this exception?

02-03 21:44:14.192: E/Trace(19701): error opening trace file: No such file or directory (2) 02-03 21:44:14.262: I/BugSenseHandler(19701): Registering default exceptions handler 02-03 21:44:14.633: I/Setup(19701): Setup activity created 02-03 21:44:14.813: I/BugSenseHandler(19701): Flushing. 02-03 21:44:14.823: I/BugSenseHandler(19701): Registering default exceptions handler 02-03 21:44:15.023: W/BugSenseHandler(19701): Transmitting ping Exception No peer certificate 02-03 21:44:15.403: I/Adreno200-EGLSUB(19701): : Format RGBA_8888. 02-03 21:44:15.594: E/(19701): : Can't open file for reading 02-03 21:44:15.594: E/(19701): : Can't open file for reading 02-03 21:44:19.318: W/dalvikvm(19701): threadid=1: thread exiting with uncaught exception (group=0x41bfb438) 02-03 21:44:22.401: E/AndroidRuntime(19701): FATAL EXCEPTION: main 02-03 21:44:22.401: E/AndroidRuntime(19701): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo: java.lang.NullPointerException 02-03 21:44:22.401: E/AndroidRuntime(19701): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2024) 02-03 21:44:22.401: E/AndroidRuntime(19701): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2132) 02-03 21:44:22.401: E/AndroidRuntime(19701): at android.app.ActivityThread.access$600(ActivityThread.java:139) 02-03 21:44:22.401: E/AndroidRuntime(19701): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1231) 02-03 21:44:22.401: E/AndroidRuntime(19701): at android.os.Handler.dispatchMessage(Handler.java:99) 02-03 21:44:22.401: E/AndroidRuntime(19701): at android.os.Looper.loop(Looper.java:137) 02-03 21:44:22.401: E/AndroidRuntime(19701): at android.app.ActivityThread.main(ActivityThread.java:5021) 02-03 21:44:22.401: E/AndroidRuntime(19701): at java.lang.reflect.Method.invokeNative(Native Method) 02-03 21:44:22.401: E/AndroidRuntime(19701): at java.lang.reflect.Method.invoke(Method.java:511) 02-03 21:44:22.401: E/AndroidRuntime(19701): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) 02-03 21:44:22.401: E/AndroidRuntime(19701): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556) 02-03 21:44:22.401: E/AndroidRuntime(19701): at dalvik.system.NativeStart.main(Native Method) 02-03 21:44:22.401: E/AndroidRuntime(19701): Caused by: java.lang.NullPointerException 02-03 21:44:22.401: E/AndroidRuntime(19701): at android.app.Activity.findViewById(Activity.java:1851) 02-03 21:44:22.401: E/AndroidRuntime(19701): at com.mpeterson.sousvide.Monitor.(Monitor.java:51) 02-03 21:44:22.401: E/AndroidRuntime(19701): at java.lang.Class.newInstanceImpl(Native Method) 02-03 21:44:22.401: E/AndroidRuntime(19701): at java.lang.Class.newInstance(Class.java:1319) 02-03 21:44:22.401: E/AndroidRuntime(19701): at android.app.Instrumentation.newActivity(Instrumentation.java:1053) 02-03 21:44:22.401: E/AndroidRuntime(19701): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2015) 02-03 21:44:22.401: E/AndroidRuntime(19701): . 11 more 

Источник

Оцените статью