0
當我嘗試在靜態類中獲取非靜態字符串時出現錯誤。從靜態方法獲取非靜態字符串
這是警告消息
我想DOWNLOAD_URL
字符串值就擺在VIDEO_URL
,但VIDEO_URL
「非靜態字段不能從靜態上下文中引用 」剛剛得到空值,如何我應該這樣做嗎?
這是我的代碼:
package com.sa.asd.ax
public class VideoProvider {
String DOWNLOAD_URL;
public static List<MediaInfo> buildMedia(String url) throws JSONException {
if (null != mediaList) {
return mediaList;
}
Map<String, String> urlPrefixMap = new HashMap<>();
mediaList = new ArrayList<MediaInfo>();
String VIDEO_URL = null;
String mimeType = null;
for (int k = 0; k < videoSpecs.length(); k++) {
JSONObject videoSpec = videoSpecs.getJSONObject(k);
if(TARGET_FORMAT.equals(videoSpec.getString(TAG_VIDEO_TYPE))){
String youtubeLink = urlPrefixMap.get(TARGET_FORMAT) + videoSpec.getString(TAG_VIDEO_URL);
YouTubeUriExtractor ytEx = new YouTubeUriExtractor(null{
@Override
public void onUrisAvailable(String videoId, String videoTitle, SparseArray<YtFile> ytFiles) {
if(ytFiles!=null){
int itag = 22; // a YouTube format identifier
DOWNLOAD_URL = ytFiles.get(itag).getUrl();
}
}
};
ytEx.execute(youtubeLink);
VIDEO_URL = DOWNLOAD_URL;
mimeType = videoSpec.getString(TAG_VIDEO_MIME);
mediaList.add(buildMediaInfo(VIDEO_URL, mimeType));
}
}
return mediaList;
}
}
Make DOWNLOAD_URL static:static String DOWNLOAD_URL; – Jens