我試圖使用谷歌的實用功能命名圖像文件。空指針異常使用類內的靜態「對象」
我把它們從相機應用程序的Util.java中提取出來。 許可證:
/*
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
我正在寫單元測試他們有麻煩createJpegName
public class Util {
private static ImageFileNamer sImageFileNamer;
// don't instantiate this class, dummy
private Util() {
}
public static String createJpegName(long dateTaken) {
synchronized (sImageFileNamer) {
return sImageFileNamer.generateName(dateTaken);
}
}
private static class ImageFileNamer {
private SimpleDateFormat mFormat;
// The date (in milliseconds) used to generate the last name.
private long mLastDate;
// Number of names generated for the same second.
private int mSameSecondCount;
@SuppressWarnings("unused")
public ImageFileNamer(String format) {
mFormat = new SimpleDateFormat(format, Locale.US);
}
public String generateName(long dateTaken) {
Date date = new Date(dateTaken);
String result = mFormat.format(date);
// If the last name was generated for the same second,
// we append _1, _2, etc to the name.
if (dateTaken/1000 == mLastDate/1000) {
mSameSecondCount++;
result += "_" + mSameSecondCount;
} else {
mLastDate = dateTaken;
mSameSecondCount = 0;
}
return result;
}
}
單元測試,爲後人:
public void testCreateJpegName() {
// public static String createJpegName(long dateTaken)
Calendar dateTime = Calendar.getInstance();
dateTime.set(1976, Calendar.SEPTEMBER, 20, 16, 20, 20);
assertEquals(Util.createJpegName(dateTime.getTimeInMillis()),"19760920_162020");
assertEquals(Util.createJpegName(dateTime.getTimeInMillis()),"19760920_162020_1");
}
所以...我假設該sImageFileNamer沒有實例化,因爲我從來沒有創建一個Util對象,對嗎?
這是打算如何使用?我錯過了什麼?
其餘的util函數我可以在原地使用。
我每次嘗試訪問sImageFileNamer時都會得到一個NPE,在上面它發生在對syncronized()的調用中。
謝謝!
你能否提供失敗的單元測試代碼? – ForguesR