退出循环不起作用的android

时间:2014-01-09 05:52:11

标签: android multithreading loops

Looper.myLooper().quit()无法运行android。弹出窗口反复显示(点击窗口将消失,几秒钟后会再次显示)。我已经让Looper.myLooper().quit()在点击按钮时退出循环,但它无效。

以下是代码:

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import java.lang.Thread.UncaughtExceptionHandler;

public class MainActivity extends Activity {
  @Override
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      Thread.setDefaultUncaughtExceptionHandler(new UnCaughtException(MainActivity.this));
      String s=null;
      s.length();  
    }        
}

class UnCaughtException implements UncaughtExceptionHandler {

    private Context context;
    private static Context context1;

    public UnCaughtException(Context ctx) {
        context = ctx;
        context1 = ctx;
    }

    public void uncaughtException(Thread t, Throwable e) {
        try {
            StringBuilder report = new StringBuilder();

            sendErrorMail(report);
        } catch (Throwable ignore) {
            Log.e(UnCaughtException.class.getName(),
                    "Error while sending error e-mail", ignore);
        }
    }

    /**
     * This method for call alert dialog when application crashed!
     */
    public void sendErrorMail(final StringBuilder errorContent) {
        final AlertDialog.Builder builder = new AlertDialog.Builder(context);
        new Thread() {
            @Override
            public void run() {
                Looper.prepare();
                builder.setTitle("Sorry...!");
               // builder.create();
                builder.setNegativeButton("Cancel",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                 Looper.myLooper().quit();
                                System.exit(0);

                            }
                        });
                builder.setPositiveButton("Report",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,
                                    int which) {

                                Looper.myLooper().quit();
                                System.exit(0);                                   
                            }
                        });
                builder.setMessage("Oops,Your application has crashed");
                builder.show();
                Looper.myLooper().loop();

            }
        }.start();
    }
}

请帮帮我。

0 个答案:

没有答案