2


我编写了两段代码,一个用于发送电子邮件,另一个用于在发生 FORCE ERROR 时收集堆栈跟踪,并且这两个代码都单独工作。
现在我的问题是当我使用这两个代码时它都不起作用(日志也正常工作)

public class BaseActivity extends Activity{

public Context context;


public Context getContext() {
    return context;
}

public void setContext(Context context) {
    this.context = context;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
     Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
        @Override
        public void uncaughtException(Thread paramThread, Throwable paramThrowable) {

        Writer result = new StringWriter();
        PrintWriter printWriter = new PrintWriter(result);
        paramThrowable.printStackTrace(printWriter);
        System.out.println("BaseActivity.onCreate(...).new UncaughtExceptionHandler() {...}.uncaughtException()" +result.toString());
        sendMail(result.toString());
        Toast.makeText(getContext(),"cs", 1).show();
        }
    });
}

public void sendMail(String body){
    try {
        GMailSender sender = new GMailSender("email@gmail.com",
                "password");
        sender.sendMail("This is Subject",
                "java.lang.RuntimeException: Unable to start activity ComponentInfo{com.silverSkills.com/com.silverSkills.com.ChildActivity}: java.lang.NullPointerException" +
                "at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)" +
                "   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)" +
                "   at android.app.ActivityThread.access$1500(ActivityThread.java:117)" +
                "   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)" +
                "   at android.os.Handler.dispatchMessage(Handler.java:99)" +
                "   at android.os.Looper.loop(Looper.java:123)" +
                "   at android.app.ActivityThread.main(ActivityThread.java:3683)" +
                "   at java.lang.reflect.Method.invokeNative(Native Method)" +
                "   at java.lang.reflect.Method.invoke(Method.java:507)" +
                "   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)" +
                "   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)" +
                "   at dalvik.system.NativeStart.main(Native Method)" +
                " Caused by: java.lang.NullPointerException" +
                "   at com.silverSkills.com.ChildActivity.onCreate(ChildActivity.java:9)" +
                "   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)" +
                "   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)" +
                "   ... 11 more",
                "email@gmail.com", "email@gmail.com");

        Log.i("Confirm","mail send");

    } catch (Exception e) {
        Log.e("SendMail", e.getMessage(), e);
    }

}

}

这就是 call make 调用此代码的方式。

public class ChildActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContext(this);
    String a=null;
    a.toString();
}

如果你能给我任何建议那就太好了:D

4

0 回答 0