[英]Wrong 1st argument type. Found: 'com.example.sunshine.FetchData', required: 'android.content.Context'
我想這個問題更多的是關於理解上下文以及如何正確使用它。 在谷歌搜索和“stackoverflowed”很多之后,我找不到答案。
問題:
使用 DateUtils.formatDateTime 時,我不能使用“this”作為上下文。 錯誤消息如標題中所述。
申請資料:
這是一個簡單的天氣應用程序,通過 JSON 檢索天氣信息並將其顯示在屏幕上。
活動:
- MainActivity.java
- 獲取數據.java
MainActivity:顯示信息
FetchData:從 API 獲取 JSON 信息,對其進行格式化並將其發送回 MainActivity
我在 FetchData.java 活動中使用 DateUtils.formatDateTime 並且使用“this”作為上下文不起作用。 根據我的理解 Context 提供了調用方法的“環境”(?)。
非常感謝幫助。 謝謝 :)
代碼:
private ArrayList<String> getWeatherDataFromJson(String forecastJsontStr) throws JSONException {
ArrayList<String> dailyWeatherInfo = new ArrayList<>();
int dataCount;
DateUtils tempDate = new DateUtils();
JSONObject weatherData = new JSONObject(forecastJsontStr);
JSONArray threeHourWeatherData = weatherData.getJSONArray(JSON_LIST);
dataCount = weatherData.getInt("cnt");
JSONObject tempJSONWeatherData;
for (int i = 0; i < dataCount; i++) {
tempJSONWeatherData = threeHourWeatherData.getJSONObject(i);
tempDate.formatDateTime(this,tempJSONWeatherData.getLong("dt"),
DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY |
DateUtils.FORMAT_ABBREV_ALL);
[more code here]
return dailyWeatherInfo;
}
編輯:我剛剛意識到我遺漏了一個重要的細節,即這個活動擴展了AsyncTask
。 經過一些進一步的研究,顯然您提供了上下文,即添加WeakReference
然后在構造函數中添加上下文。
我添加了以下代碼:
private WeakReference<Context> contextWeakReference;
public FetchData (Content context) {
contextWeakReference = new WeakReference<>();
}
tempDate.formatDateTime(contextWeakReference.get(),tempJSONWeatherData.getLong("dt"),
DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY |
DateUtils.FORMAT_ABBREV_ALL);
這使錯誤消失,但我仍然不明白為什么“這個”不起作用。
我在 FetchData.java 活動中使用 DateUtils.formatDateTime 並且使用“this”作為上下文不起作用。 根據我的理解 Context 提供了調用方法的“環境”(?)。
你錯了, Context 是 Android 上下文,它是(來自文檔):
有關應用程序環境的全局信息的接口。 這是一個抽象類,其實現由Android系統提供。 它允許訪問特定於應用程序的資源和類,以及調用應用程序級操作,例如啟動活動、廣播和接收意圖等。
DateUtils.formatDateTime()
需要 Context 作為其參數之一。 所以,你需要傳遞一個上下文。
Android Activity
是 Context 的子類,因此您可以使用this
(指的是自身)作為上下文,如下所示:
public class MyActivity extends Activity {
...
protected void doSomething() {
// this refer to the MyActivity instance which is a Context.
DateUtils.formatDateTime(this, ...);
}
...
}
您需要為每個不是 Context 子類的類傳遞 Context 。
您不能在AsyncTask 中使用this
,因為它不是 Context 子類。 因此,您需要使用WeakReference
傳遞 Context 以避免Context leaking
,如下所示:
private class AsyncTaskRunner extends AsyncTask<String, String, String> {
private WeakReference<Context> contextWeakReference;
public FetchData (Content context) {
contextWeakReference = new WeakReference<>();
}
private void doSomething() {
// We have the context from the WeakReference
Context context = contextWeakReference.get();
DateUtils.formatDateTime(context, ...);
}
}
最后,在調用DateUtils.formatDateTime()
時不需要創建 DateUtils 對象,因此這不是必需的:
DateUtils tempDate = new DateUtils();
tempDate.formatDateTime(...);
你可以直接調用它,因為它是一個靜態方法:
DateUtils.formatDateTime(...);
tempDate.formatDateTime(this,tempJSONWeatherData.getLong("dt"), 而不是 this 你可以傳遞應用程序的上下文,這指的是類 FetchData
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.