無限遞迴導致StackOverflowError

bluepeach發表於2021-09-09
/**
 * Created by wangke on 2017-07-20.
 * 上傳檔案到七牛雲的工具類
 */

public  class UpLoadHelper {

    private static final String accessKey = "KXLzuFCOxgNar5whqU3-0bmrH6rTHOqaidcohRes1";
    private static final String secretKey = "aiw1aAh-dwA9k6nkWAQLExy2Taz9cE1nFYA_01WY1";
    private CompletionListener mCompletionListener = null;

    /**
     * 上傳頭像
     *
     * @param path
     */
    public void upLoadAveter(String path, String key, CompletionListener completionListener) {

        this.mCompletionListener = completionListener;

        Auth auth = Auth.create(accessKey, secretKey);
        //上傳憑證
        String uploadToken = auth.uploadToken("kangyuanmilk", "/aveter");
        UploadManager uploadManager = UpLoadManagerFactory.getInstance();

        uploadManager.put(path, key, uploadToken, new UpCompletionHandler() {
            //上傳進度的回撥
            @Override
            public void complete(String key, ResponseInfo info, JSONObject response) {

                if (mCompletionListener != null) {
                    complete(key, info, response);
                }

            }
        }, null);

    }

    public interface CompletionListener {

        public void complete(String key, ResponseInfo info, JSONObject response);
    }

}

在使用uploadManager.put方法上傳圖片的時候需要新增一個回撥來通知當前上傳檔案的進度等資訊,為了簡化上傳程式碼於是稍微封裝了一下,因為涉及到了一個回撥所以就寫了一個介面用於將當前的上傳狀態通知外界,不幸的是我自己寫的那個回撥方法與uploadManager.put方法中的回撥方法的命名是相同的,這也就導致了後面在呼叫的時候就變成了遞迴呼叫,一直到後面棧溢位。

報錯資訊:

07-20 16:03:00.224 7475-7475/com.merpyzf.kangyuanmilk E/AndroidRuntime: FATAL EXCEPTION: main
                                                                        Process: com.merpyzf.kangyuanmilk, PID: 7475
                                                                        java.lang.StackOverflowError: stack size 8MB

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/2558/viewspace-2799249/,如需轉載,請註明出處,否則將追究法律責任。

相關文章