问:我正在尝试将Butterknife添加到我的天气应用程序项目中,但似乎无法正常工作。
我经历了将行添加到构建gradle文件中的过程,但是收到了本文主题中显示的错误。
添加到我的构建文件中的行如下所示。(build.gradle模块应用)
依赖项{
编译fileTree(dir:'libs',包括:['* .jar'])
testCompile'junit:junit:4.12'
编译'com.android.support:appcompat-v7:23.0.1'
编译'com.android .support:design:23.0.1'
编译'com.squareup.okhttp:okhttp:2.5.0'
编译'com.jakewharton:butterknife:7.0.1'
}
我的密码
软件包rythmair.com.stormy;
导入android.content.Context;
导入android.net.ConnectivityManager;
导入android.net.NetworkInfo;
导入android.os.Bundle;
导入android.support.v7.app.AppCompatActivity;
导入android.util.Log;
导入android.widget.TextView;
导入android.widget.Toast;
导入com.squareup.okhttp.Call;
导入com.squareup.okhttp.Callback;
导入com.squareup.okhttp.OkHttpClient;
导入com.squareup.okhttp.Request;
导入com.squareup.okhttp.Response;
导入org.json.JSONException;
导入org.json.JSONObject;
导入java.io.IOException;
公共类MainActivity扩展了AppCompatActivity {
公共静态最终字符串TAG = MainActivity.class.getSimpleName();
私人CurrentWeather mCurrentWeather;
私人TextView mTemperatureLabel;
@InjectView(R.id.timeLabel)TextView mTimelabel;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTemperatureLabel =(TextView)findViewById(R.id.temperatureLabel);
字符串apiKey =“ f75066e71a1b9dbf849641e0802fb46d”;
双重纬度= 37.8267;
双经度= -122.423;
字符串ForecastUrl =“ https://api.forecast.io/forecast/” + apiKey +
“ /” +纬度+“,” +经度;
如果(isNetworkAvailable()){
OkHttpClient client = new OkHttpClient();
请求请求=新Request.Builder().
url(forecastUrl)
.build();
通话= client.newCall(request);
call.enqueue(新回调(){
@覆盖
公共无效onFailure处(请求请求,IOException的发送){
}
@覆盖
公共无效onResponse(响应响应)抛出IOException异常{
尝试{
字符串jsonData = response.body()。string();
Log.v(TAG,jsonData);
如果(response.isSuccessful()){
mCurrentWeather = getCurrentDetails(jsonData);
}其他{
alertUserAboutError();
}
}
catch(IOException e){
Log.e(TAG,“ Exception Caught:”,e);
}
catch(JSONException e){
Log.e(TAG,“ Exception Caught:”,e);
}
}
});
}
else {
Toast.makeText(this,R.string.network_unavailable_message,Toast.LENGTH_LONG).show();
}
Log.d(TAG,“主UI代码正在运行”);
}
私人CurrentWeather getCurrentDetails(String jsonData)抛出JSONException {
JSONObject Forecast = new JSONObject(jsonData);
字符串时区= Forecast.getString(“ timezone”);
Log.i(TAG,“来自JSON:” +时区);
JSONObject当前= Forecast.getJSONObject(“当前”);
CurrentWeather currentWeather =新的CurrentWeather();
currentWeather.setHumidity(current.getDouble(“ humidity”));
currentWeather.setTime(currently.getLong(“ time”));
currentWeather.setIcon(currently.getString(“ icon”));
currentWeather.setPrecipChance(current.getDouble(“ precipProbability”));
currentWeather.setSummary(currently.getString(“ summary”));
currentWeather.setTemperature(current.getDouble(“ temperature”));
currentWeather.setTimeZone(timezone);
Log.d(TAG,currentWeather.getFormattedTime());
返回currentWeather;
}
private boolean isNetworkAvailable(){
ConnectivityManager manager =(ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = manager.getActiveNetworkInfo();
boolean isAvailable = false;
如果(networkInfo!= null && networkInfo.isConnected()){
isAvailable = true;
}
return isAvailable;
}
private void alertUserAboutError(){
AlertDialogFragment dialog = new AlertDialogFragment();
dialog.show(getFragmentManager(),“ error_dialog”);
}
}
答:大代码段中的第四行(如下所示)是失败的那一行
@InjectView(R.id.timeLabel)TextView mTimelabel;