5 Stimmen

Neustart der Aktivität beim Beenden der Anwendung aus dem Unception-Handler

In meiner Anwendung verwende ich einen globalen Exception-Handler, um ein erzwungenes Schließen zu vermeiden, aber eine Aktivität wird nach dem Aufruf von system.exit(0)....???? gestartet.

das angezeigte Logcat ist:

07-18 12:41:12.569: WARN/Resources(8453): Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f07000a}
07-18 12:41:14.648: INFO/ActivityManager(571): Displayed activity com.fss/.Loading: 2643 ms
07-18 12:41:14.689: INFO/System.out(8453): >>>>>>>>SentRequestBE
07-18 12:41:14.689: INFO/System.out(8453): >>>>>>>>StaticStore.dialog.getProgress()0==100
07-18 12:41:15.399: INFO/System.out(8453): >>>>>>>>SentRequestBE
07-18 12:41:15.429: INFO/System.out(8453): >>>>>>>>StaticStore.dialog.getProgress()20==100
07-18 12:41:16.288: DEBUG/SmsProvider(611): insert url=content://sms/inbox, match=2
07-18 12:41:16.510: DEBUG/dalvikvm(8453): GC freed 5512 objects / 258536 bytes in 745ms
07-18 12:41:16.640: INFO/System.out(8453): >>>>>>>>SentRequestBE
07-18 12:41:16.662: INFO/System.out(8453): >>>>>>>>StaticStore.dialog.getProgress()40==100
07-18 12:41:16.688: INFO/System.out(8453): >>>>>>>null
07-18 12:41:16.739: DEBUG/AndroidRuntime(8453): Shutting down VM
07-18 12:41:16.739: WARN/dalvikvm(8453): threadid=3: thread exiting with uncaught exception (group=0x4000fe70)
07-18 12:41:16.799: INFO/System.out(8453): ***************************EXception Ctached here********************
07-18 12:41:16.919: ERROR/Report ::(8453): java.lang.RuntimeException: Unable to start receiver com.fss.SMSRecieve: java.lang.NullPointerException
07-18 12:41:16.919: ERROR/Report ::(8453): --------- Stack trace ---------
07-18 12:41:16.919: ERROR/Report ::(8453):     android.app.ActivityThread.handleReceiver(ActivityThread.java:2417)
07-18 12:41:16.919: ERROR/Report ::(8453):     android.app.ActivityThread.access$2700(ActivityThread.java:112)
07-18 12:41:16.919: ERROR/Report ::(8453):     android.app.ActivityThread$H.handleMessage(ActivityThread.java:1741)
07-18 12:41:16.919: ERROR/Report ::(8453):     android.os.Handler.dispatchMessage(Handler.java:99)
07-18 12:41:16.919: ERROR/Report ::(8453):     android.os.Looper.loop(Looper.java:123)
07-18 12:41:16.919: ERROR/Report ::(8453):     android.app.ActivityThread.main(ActivityThread.java:3948)
07-18 12:41:16.919: ERROR/Report ::(8453):     java.lang.reflect.Method.invokeNative(Native Method)
07-18 12:41:16.919: ERROR/Report ::(8453):     java.lang.reflect.Method.invoke(Method.java:521)
07-18 12:41:16.919: ERROR/Report ::(8453):     com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
07-18 12:41:16.919: ERROR/Report ::(8453):     com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
07-18 12:41:16.919: ERROR/Report ::(8453):     dalvik.system.NativeStart.main(Native Method)
07-18 12:41:16.919: ERROR/Report ::(8453): -------------------------------
07-18 12:41:16.919: ERROR/Report ::(8453): --------- Cause ---------
07-18 12:41:16.919: ERROR/Report ::(8453): java.lang.NullPointerException
07-18 12:41:16.919: ERROR/Report ::(8453):     com.fss.SMSRecieve.onReceive(SMSRecieve.java:26)
07-18 12:41:16.919: ERROR/Report ::(8453):     android.app.ActivityThread.handleReceiver(ActivityThread.java:2408)
07-18 12:41:16.919: ERROR/Report ::(8453):     android.app.ActivityThread.access$2700(ActivityThread.java:112)
07-18 12:41:16.919: ERROR/Report ::(8453):     android.app.ActivityThread$H.handleMessage(ActivityThread.java:1741)
07-18 12:41:16.919: ERROR/Report ::(8453):     android.os.Handler.dispatchMessage(Handler.java:99)
07-18 12:41:16.919: ERROR/Report ::(8453):     android.os.Looper.loop(Looper.java:123)
07-18 12:41:16.919: ERROR/Report ::(8453):     android.app.ActivityThread.main(ActivityThread.java:3948)
07-18 12:41:16.919: ERROR/Report ::(8453):     java.lang.reflect.Method.invokeNative(Native Method)
07-18 12:41:16.919: ERROR/Report ::(8453):     java.lang.reflect.Method.invoke(Method.java:521)
07-18 12:41:16.919: ERROR/Report ::(8453):     com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
07-18 12:41:16.919: ERROR/Report ::(8453):     com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
07-18 12:41:16.919: ERROR/Report ::(8453):     dalvik.system.NativeStart.main(Native Method)
07-18 12:41:16.939: INFO/System.out(8453): *************finally block enetered
07-18 12:41:16.979: INFO/NotificationService(571): enqueueToast pkg=com.fss callback=android.app.ITransientNotification$Stub$Proxy@4381ced0 duration=1
07-18 12:41:17.018: INFO/AndroidRuntime(8453): AndroidRuntime onExit calling exit(0)
07-18 12:41:17.029: ERROR/JavaBinder(8453): Unknown binder error code. 0xfffffff7
07-18 12:41:17.049: ERROR/JavaBinder(8453): Unknown binder error code. 0xfffffff7
07-18 12:41:17.148: INFO/ActivityManager(571): Process com.fss (pid 8453) has died.
07-18 12:41:17.188: INFO/WindowManager(571): WIN DEATH: Window{43721d70 com.fss/com.fss.DynamicCanvas paused=false}
07-18 12:41:17.198: INFO/WindowManager(571): WIN DEATH: Window{43733cf8 com.fss/com.fss.ListSelection paused=false}
07-18 12:41:17.198: INFO/WindowManager(571): WIN DEATH: Window{437ba200 com.fss/com.fss.DynamicCanvas paused=false}
07-18 12:41:17.210: INFO/WindowManager(571): WIN DEATH: Window{43718e10 com.fss/com.fss.Loading paused=false}
07-18 12:41:17.210: INFO/WindowManager(571): WIN DEATH: Window{43744eb8 com.fss/com.fss.Loading paused=false}
07-18 12:41:17.509: INFO/ActivityManager(571): Start proc com.fss for activity com.fss/.DynamicCanvas: pid=8468 uid=10019 gids={3003}
07-18 12:41:17.958: INFO/jdwp(8468): received file descriptor 20 from ADB
07-18 12:41:18.189: WARN/System.err(8468): Can't dispatch DDM chunk 46454154: no handler defined
07-18 12:41:18.219: WARN/System.err(8468): Can't dispatch DDM chunk 4d505251: no handler defined
07-18 12:41:19.710: INFO/ActivityManager(571): Starting activity: Intent { flags=0x24000000 comp={com.fss/com.fss.SplashScreen} (has extras) }
07-18 12:41:19.810: INFO/System.out(8468): Dynamic Canvas......
07-18 12:41:20.010: INFO/System.out(8468): >>>>>5
07-18 12:41:20.020: INFO/System.out(8468): >>>>> M-CONNECT  Login
07-18 12:41:20.030: INFO/System.out(8468): >>>>>APUM
07-18 12:41:20.039: INFO/System.out(8468): >>>>>Enter password
07-18 12:41:20.049: INFO/System.out(8468): >>>>>4-6-N-Y-Y
07-18 12:41:20.079: INFO/System.out(8468): >>>>>1
07-18 12:41:20.089: INFO/System.out(8468): temp >>>>> 1 4-6-N-Y-Y
07-18 12:41:20.489: WARN/NotificationService(571): Object died trying to hide notification android.app.ITransientNotification$Stub$Proxy@4381ced0 in package com.fss
07-18 12:41:20.499: WARN/ActivityManager(571): Activity pause timeout for HistoryRecord{437a8b80 {com.fss/com.fss.DynamicCanvas}}
07-18 12:41:20.509: WARN/ActivityManager(571): setProcessForeground called on unknown pid: 8453
07-18 12:41:20.970: INFO/ActivityManager(571): moveTaskToBack: 50
07-18 12:41:21.699: WARN/InputManagerService(571): Got RemoteException sending setActive(false) notification to pid 8453 uid 10019
07-18 12:41:22.049: WARN/ActivityManager(571): Launch timeout has expired, giving up wake lock!
07-18 12:41:25.041: INFO/System.out(8468): >>>>>>>>>>>>OnCreateMethod
07-18 12:41:25.359: INFO/System.out(8468): >>>>>>>>>>>>DBNAME/data/data/com.fss/databases/MPAY_444444
07-18 12:41:25.369: ERROR/Database(8468): Failure 1 (table MiscDetails0029 already exists) on 0x1acf30 when preparing 'create table MiscDetails0029 (id integer primary key not null,table_row_one text);'.
07-18 12:41:25.388: WARN/System.err(8468): android.database.sqlite.SQLiteException: table MiscDetails0029 already exists: create table MiscDetails0029 (id integer primary key not null,table_row_one text);
07-18 12:41:25.408: WARN/System.err(8468):     at android.database.sqlite.SQLiteDatabase.native_execSQL(Native Method)
07-18 12:41:25.430: WARN/System.err(8468):     at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1496)
07-18 12:41:25.438: WARN/System.err(8468):     at com.fss.RmsStore$CustomSQLiteOpenHelper.onCreate(RmsStore.java:240)
07-18 12:41:25.448: WARN/System.err(8468):     at com.fss.RmsStore.<init>(RmsStore.java:48)
07-18 12:41:25.489: WARN/System.err(8468):     at com.fss.SplashScreen.startApp(SplashScreen.java:72)
07-18 12:41:25.489: WARN/System.err(8468):     at com.fss.SplashScreen$1.run(SplashScreen.java:53)
07-18 12:41:25.608: INFO/System.out(8468): ::::::::::::::::::SataticStore.NO ON ACC6
07-18 12:41:25.628: INFO/System.out(8468): ::::::::::acountNumbers0
07-18 12:41:25.672: INFO/System.out(8468): ::::::::::acountNumbers0
07-18 12:41:25.688: INFO/System.out(8468): ::::::::::acountNumbers0
07-18 12:41:25.699: INFO/System.out(8468): ::::::::::acountNumbers0
07-18 12:41:25.699: INFO/System.out(8468): ::::::::::acountNumbers0
07-18 12:41:25.718: INFO/System.out(8468): ::::::::::acountNumbers0
07-18 12:41:25.768: INFO/ActivityManager(571): Starting activity: Intent { comp={com.fss/com.fss.DynamicCanvas} }
07-18 12:41:25.778: WARN/ActivityManager(571): startActivity called from non-Activity context; forcing Intent.FLAG_ACTIVITY_NEW_TASK for: Intent { comp={com.fss/com.fss.DynamicCanvas} }
07-18 12:41:26.969: INFO/ActivityManager(571): Displayed activity com.fss/.DynamicCanvas: 9671 ms
07-18 12:41:27.670: WARN/NotificationService(571): STOP command without a player
07-18 12:41:29.460: INFO/ActivityManager(571): Stopping service: com.android.mms/.transaction.SmsReceiverService
07-18 12:41:30.088: DEBUG/ActivityManager(571): checkComponentPermission() adjusting {pid,uid} to {542,1013}
07-18 12:41:30.239: DEBUG/ActivityManager(571): checkComponentPermission() adjusting {pid,uid} to {542,1013}
07-18 12:41:30.429: DEBUG/ActivityManager(571): checkComponentPermission() adjusting {pid,uid} to {542,1013}
07-18 12:41:30.628: DEBUG/ActivityManager(571): checkComponentPermission() adjusting {pid,uid} to {542,1013}
07-18 12:41:30.769: ERROR/MediaPlayerService(542): Couldn't open fd for content://settings/system/notification_sound
07-18 12:41:30.799: ERROR/MediaPlayer(571): Unable to to create media player
07-18 12:41:30.839: WARN/NotificationService(571): error loading sound for content://settings/system/notification_sound
07-18 12:41:30.839: WARN/NotificationService(571): java.io.IOException: setDataSource failed.: status=0x80000000
07-18 12:41:30.839: WARN/NotificationService(571):     at android.media.MediaPlayer.setDataSource(Native Method)
07-18 12:41:30.839: WARN/NotificationService(571):     at android.media.MediaPlayer.setDataSource(MediaPlayer.java:610)
07-18 12:41:30.839: WARN/NotificationService(571):     at android.media.AsyncPlayer$Thread.run(AsyncPlayer.java:76)
07-18 12:41:32.818: DEBUG/dalvikvm(658): GC freed 687 objects / 28744 bytes in 537ms
07-18 12:41:37.668: DEBUG/dalvikvm(7930): GC freed 1122 objects / 53896 bytes in 377ms
07-18 12:41:42.769: DEBUG/dalvikvm(611): GC freed 804 objects / 39568 bytes in 476ms
07-18 12:41:48.278: DEBUG/dalvikvm(613): GC freed 445 objects / 21192 bytes in 495ms

Ich habe den Exception-Handler auch angehängt: ....

package com.fss;

import java.lang.Thread.UncaughtExceptionHandler;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Looper;
import android.util.Log;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.widget.ProgressBar;
import android.widget.Toast;

public class TopExceptionHandler implements Thread.UncaughtExceptionHandler   {

private Context context;
//private UncaughtExceptionHandler defaultHandler;

    public TopExceptionHandler(Context context) {

StaticStore.defaultHandler = Thread
.getDefaultUncaughtExceptionHandler();
this.context=context;

    }

      public void uncaughtException(Thread t,final Throwable e)

    {
         StaticStore.mfs=true;
        try{
         new Thread() {
            @Override
            public void run() {
                Looper.prepare();  

            Toast.makeText(context, "TOAST", Toast.LENGTH_LONG).show();

//              context.startActivity(intent);
            //

            //System.exit(0);//If you want to restart activity and want to kill after crash.s
                Looper.loop();

            }
        }.start();

        System.out.println("***************************EXception Ctached here********************");

        StackTraceElement[] arr = e.getStackTrace();

        String report = e.toString()+"\n\n";

        report += "--------- Stack trace ---------\n\n";

        for (int i=0; i<arr.length; i++)

        {

        report += "    "+arr[i].toString()+"\n";

        }

        report += "-------------------------------\n\n";

        // If the exception was thrown in a background thread inside

        // AsyncTask, then the actual exception can be found with getCause

        report += "--------- Cause ---------\n\n";

        Throwable cause = e.getCause();

        if(cause != null) {

        report += cause.toString() + "\n\n";

        arr = cause.getStackTrace();

        for (int i=0; i<arr.length; i++)

        {

        report += "    "+arr[i].toString()+"\n";

        }

        }

        Log.e("Report ::",report);

    }

        catch (Exception ex) {
              ex.printStackTrace();
          } finally {
              StaticStore.index=0;
              System.out.println("*************finally block enetered");
              StaticStore.dialog.dismiss();

//            Intent myIntent = new Intent();
//            myIntent.setClassName("com.fss","com.fss.DynamicCanvas");
//              myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
//              myIntent.putExtra("EXIT", true);
//              context.startActivity(myIntent);
//            System.runFinalizersOnExit(true);
 System.exit(0);
//System.out.println("after sys exit");
//  defaultHandler.uncaughtException(t, e);

          }
 //   throw new RuntimeException("You just caught me..");
    }

}

2voto

Etwas verspätete Antwort, aber... Bei mir tritt das nur auf wenigen Geräten auf, zum Beispiel auf dem Samsung Nexus 7. Um diese lästigen Neustarts zu vermeiden, speichere ich die aktuelle Aktivität in einer statischen Variable und rufe finish() vor System.exit :

// base class for all activities in my application
class BaseActivity extends Activity {
  private static BaseActivity sCurrentActivity = null;

  @Override
  protected void onCreate( Bundle savedInstanceState ) {
    super.onCreate( savedInstanceState );
    sCurrentActivity = this;
  }

  @Override
  protected void onStart() {
    super.onStart();
    sCurrentActivity = this;
  }

  @Override
  protected void onResume() {
    super.onResume();
    sCurrentActivity = this;
  }

  @Override
  protected void onPause() {
    super.onPause();
    sCurrentActivity = null;
  }    

  public static System_exit( int exitCode ) {
    if ( null != sCurrentActivity ) {
      sCurrentActivity.finish();
      sCurrentActivity = null;
    }
    System.exit( exitCode );
  }
}

1voto

Anticro Punkte 41

Danke! Das hat funktioniert - ABER: Sie müssen Ihre Hauptaktivität beenden, es funktioniert nicht, wenn Sie eine andere beenden. Und hier ist noch eine Verbesserung: Wenn Sie einen statischen Verweis auf eine Aktivität haben wollen, verwenden Sie besser einen schwachen Verweis und einen statischen Getter, anstatt eines starken Verweises:

public class MainActivity extends Activity {
    private static WeakReference<MainActivity> sharedActivity = null;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        sharedActivity = new WeakReference<MainActivity>(this);
        setContentView(R.layout.activity_main);
    }

    public static MainActivity sharedActivity() {
        try {
            return sharedActivity.get();
        } catch (Exception e) {
            return null;
        }
    }
}

Die WeakReference wird den Garbage Collector nicht daran hindern, die Aktivität zu GC'ing. Wenn sie gesammelt wurde, ist der Rückgabewert der WeakReference null, ansonsten wird das Activity-Objekt zurückgegeben. Dies erleichtert Ihren Code und verhindert, dass Sie auf eine abgeschlossene Aktivität zugreifen.

CodeJaeger.com

CodeJaeger ist eine Gemeinschaft für Programmierer, die täglich Hilfe erhalten..
Wir haben viele Inhalte, und Sie können auch Ihre eigenen Fragen stellen oder die Fragen anderer Leute lösen.

Powered by:

X