Java NullPointerException变量
我在Java NullPointerException变量,java,android,oop,nullpointerexception,Java,Android,Oop,Nullpointerexception,我在MainActivity中声明了两个成员变量,并在获得方法中的位置后立即初始化它们 问题是,如果使用静态方法或创建对象从另一个活动调用它们,则会出现NullPointerException 如何解决这个问题 public class MainActivity extends ActionBarActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedLi
MainActivity
中声明了两个成员变量,并在获得方法中的位置后立即初始化它们
问题是,如果使用静态方法或创建对象从另一个
活动
调用它们,则会出现NullPointerException
如何解决这个问题
public class MainActivity extends ActionBarActivity implements GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener, LocationListener {
public static final String TAG = MainActivity.class.getSimpleName();
public static final String DAILY_FORECAST = "DAILY_FORECAST";
public static final String HOURLY_FORECAST = "HOURLY_FORECAST";
private LocationRequest mLocationRequest;
private Forecast mForecast;
private GoogleApiClient mGoogleApiClient;
private Location mLastLocation;
private double mLatitude;
private double mLongitude;
public static String mCity; // declared here
private static String mAdminArea; // declared here
@Bind(R.id.locationLabel) TextView mLocationLabel;
@Bind(R.id.timeLabel) TextView mTimeLabel;
@Bind(R.id.temperatureLabel) TextView mTemperatureLabel;
@Bind(R.id.humidityValue) TextView mHumidityValue;
@Bind(R.id.precipValue) TextView mPrecipValue;
@Bind(R.id.summaryLabel) TextView mSummaryLabel;
@Bind(R.id.iconImageView) ImageView mIconImageView;
@Bind(R.id.refreshImageView) ImageView mRefreshImageView;
@Bind(R.id.progressBar) ProgressBar mProgressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
mProgressBar.setVisibility(View.INVISIBLE);
toggleRefresh();
buildGoogleApiClient();
mGoogleApiClient.connect();
mRefreshImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getForecast(mLatitude, mLongitude);
}
});
}
@Override
protected void onPause(){
super.onPause();
if (mGoogleApiClient != null) {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
}
}
private void getForecast(double latitude, double longitude) {
String apiKey = "48fb6c0ca3567d0b17bf99b400ef5606";
String forecastUrl = "https://api.forecast.io/forecast/" + apiKey +
"/" + latitude + "," + longitude;
if (isNetworkAvailable()) {
toggleRefresh();
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(forecastUrl)
.build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
runOnUiThread(new Runnable() {
@Override
public void run() {
toggleRefresh();
}
});
alertUserAboutError();
}
@Override
public void onResponse(Response response) throws IOException {
runOnUiThread(new Runnable() {
@Override
public void run() {
toggleRefresh();
}
});
try {
String jsonData = response.body().string();
Log.v(TAG, jsonData);
if (response.isSuccessful()) {
mForecast = parseForecastDetails(jsonData);
runOnUiThread(new Runnable() {
@Override
public void run() {
updateDisplay();
}
});
} else {
alertUserAboutError();
}
} catch (IOException e) {
Log.e(TAG, "Exception caught: ", e);
} catch (JSONException e) {
Log.e(TAG, "Exception caught: ", e);
}
}
});
}
else {
Toast.makeText(this, getString(R.string.network_unavailable_message),
Toast.LENGTH_LONG).show();
}
}
private void toggleRefresh() {
if (mProgressBar.getVisibility() == View.INVISIBLE) {
mProgressBar.setVisibility(View.VISIBLE);
mRefreshImageView.setVisibility(View.INVISIBLE);
}
else {
mProgressBar.setVisibility(View.INVISIBLE);
mRefreshImageView.setVisibility(View.VISIBLE);
}
}
private void updateDisplay() {
Current current = mForecast.getCurrent();
mTemperatureLabel.setText(current.getTemperature() + "");
mTimeLabel.setText("At " + current.getFormattedTime() + " it will be");
mHumidityValue.setText(current.getHumidity() + "");
mPrecipValue.setText(current.getPrecipChance() + "%");
mSummaryLabel.setText(current.getSummary());
Drawable drawable = getResources().getDrawable(current.getIconId());
mIconImageView.setImageDrawable(drawable);
}
private Forecast parseForecastDetails(String jsonData) throws JSONException {
Forecast forecast = new Forecast();
forecast.setCurrent(getCurrentDetails(jsonData));
forecast.setHourlyForecast(getHourlyForecast(jsonData));
forecast.setDailyForecast(getDailyForecast(jsonData));
return forecast;
}
private Day[] getDailyForecast(String jsonData) throws JSONException {JSONObject forecast = new JSONObject(jsonData);
String timezone = forecast.getString("timezone");
JSONObject daily = forecast.getJSONObject("daily");
JSONArray data = daily.getJSONArray("data");
Day[] days = new Day[data.length()];
for (int i = 0; i < data.length(); i++) {
JSONObject jsonDay = data.getJSONObject(i);
Day day = new Day();
day.setSummary(jsonDay.getString("summary"));
day.setIcon(jsonDay.getString("icon"));
day.setTime(jsonDay.getLong("time"));
day.setTemperatureMax(jsonDay.getDouble("temperatureMax"));
day.setTimezone(timezone);
days[i] = day;
}
return days;
}
private Hour[] getHourlyForecast(String jsonData) throws JSONException {
JSONObject forecast = new JSONObject(jsonData);
String timezone = forecast.getString("timezone");
JSONObject hourly = forecast.getJSONObject("hourly");
JSONArray data = hourly.getJSONArray("data");
Hour[] hours = new Hour[data.length()];
for (int i = 0; i < data.length(); i++) {
JSONObject jsonHour = data.getJSONObject(i);
Hour hour = new Hour();
hour.setSummary(jsonHour.getString("summary"));
hour.setTemperature(jsonHour.getDouble("temperature"));
hour.setIcon(jsonHour.getString("icon"));
hour.setTime(jsonHour.getLong("time"));
hour.setTimezone(timezone);
hours[i] = hour;
}
return hours;
}
private Current getCurrentDetails(String jsonData) throws JSONException {
JSONObject forecast = new JSONObject(jsonData);
String timezone = forecast.getString("timezone");
Log.i(TAG, "From JSON: " + timezone);
JSONObject currently = forecast.getJSONObject("currently");
Current current = new Current();
current.setHumidity(currently.getDouble("humidity"));
current.setTime(currently.getInt("time"));
current.setSummary(currently.getString("summary"));
current.setTemperature(currently.getInt("temperature"));
current.setIcon(currently.getString("icon"));
current.setPrecipChance(currently.getDouble("precipProbability"));
current.setTimeZone(timezone);
Log.d(TAG, current.getFormattedTime());
return current;
}
private boolean isNetworkAvailable() {
ConnectivityManager manager = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = manager.getActiveNetworkInfo();
boolean isAvailable = false;
if (networkInfo != null && networkInfo.isConnected()) {
isAvailable = true;
}
return isAvailable;
}
private void alertUserAboutError() {
AlertDialogFragment dialog = new AlertDialogFragment();
dialog.show(getFragmentManager(), "error_dialog");
}
@OnClick (R.id.dailyButton)
public void startDailyActivity(View view) {
Intent intent = new Intent(this, DailyForecastActivity.class);
intent.putExtra(DAILY_FORECAST, mForecast.getDailyForecast());
startActivity(intent);
}
@OnClick(R.id.hourlyButton)
public void startHourlyActivity(View view) {
Intent intent = new Intent(this, HourlyForecastActivity.class);
intent.putExtra(HOURLY_FORECAST, mForecast.getHourlyForecast());
startActivity(intent);
}
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
@Override
public void onConnected(Bundle bundle) {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(1000);
mLocationRequest.setFastestInterval(1000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}
@Override
public void onConnectionSuspended(int i) {
Toast.makeText(this,getString(R.string.connection_suspended),Toast.LENGTH_LONG).show();
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Toast.makeText(this,getString(R.string.connection_failed),Toast.LENGTH_SHORT).show();
toggleRefresh();
mLatitude = 37.8267;
mLongitude = -122.423;
getForecast(mLatitude, mLongitude);
}
@Override
public void onLocationChanged(Location location) {
mLastLocation = location;
mLatitude = location.getLatitude();
mLongitude = location.getLongitude();
getLocationName(mLatitude, mLongitude);
toggleRefresh();
getForecast(mLatitude, mLongitude);
//remove location updates if you just need one location:
if (mGoogleApiClient != null) {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
}
}
private void getLocationName(Double latitude, Double longitude) {
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
try {
List<Address> addresses = geocoder.getFromLocation(latitude,
longitude, 1);
if (addresses != null) {
Address address = addresses.get(0);
mCity = address.getLocality(); // initialised here
mAdminArea = address.getAdminArea(); // initialised here
if (mAdminArea != null) {
mLocationLabel.setText(mCity.toString() + ", " + mAdminArea);
}
else {
mLocationLabel.setText(mCity.toString());
}
}
else {
mLocationLabel.setText("No Locality Founded!");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
mLocationLabel.setText("Cannot get Locality!");
}
}
public static String getCity() {
return mCity;
}
public static String getAdminArea() {
return mAdminArea;
}
}
公共类MainActivity扩展了ActionBarActivity实现了GoogleAppClient.ConnectionCallbacks,
GoogleAppClient.OnConnectionFailedListener,LocationListener{
公共静态最终字符串标记=MainActivity.class.getSimpleName();
公共静态最终字符串DAILY\u FORECAST=“DAILY\u FORECAST”;
公共静态最终字符串HOURLY\u FORECAST=“HOURLY\u FORECAST”;
私人位置请求mLocationRequest;
私人预测;
私人GoogleapClient MGoogleapClient;
私人场所;
私人双重国籍;
私人双重身份;
公共静态字符串mCity;//在此处声明
私有静态字符串mAdminArea;//在此处声明
@绑定(R.id.locationLabel)文本视图MLLocationLabel;
@绑定(R.id.timeLabel)文本视图MTIMELABLE;
@绑定(R.id.temperatureLabel)文本视图MTTemperatureLabel;
@绑定(R.id.humidityValue)文本视图mHumidityValue;
@绑定(R.id.precipValue)文本视图值;
@绑定(R.id.summaryLabel)文本视图MSSummaryLabel;
@绑定(R.id.iconImageView)图像视图mIconImageView;
@绑定(R.id.refreshImageView)ImageView mRefreshImageView;
@绑定(R.id.progressBar)progressBar-mProgressBar;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
把(这个)绑起来;
mProgressBar.setVisibility(视图.不可见);
切换刷新();
buildGoogleAppClient();
mGoogleApiClient.connect();
mRefreshImageView.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
getForecast(高度、长度);
}
});
}
@凌驾
受保护的void onPause(){
super.onPause();
if(mGoogleApiClient!=null){
LocationServices.FusedLocationApi.RemovelocationUpdate(mgoogleapClient,this);
}
}
私人预测(双纬度、双经度){
字符串apiKey=“48fb6c0ca3567d0b17bf99b400ef5606”;
字符串预测URL=”https://api.forecast.io/forecast/“+apiKey+
“/”+纬度+”,“+经度;
if(isNetworkAvailable()){
切换刷新();
OkHttpClient=新的OkHttpClient();
Request Request=newrequest.Builder()
.url(预测url)
.build();
Call Call=client.newCall(请求);
call.enqueue(新回调(){
@凌驾
公共void onFailure(请求,IOE异常){
runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
切换刷新();
}
});
alertUserAboutError();
}
@凌驾
public void onResponse(Response-Response)引发IOException{
runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
切换刷新();
}
});
试一试{
String jsonData=response.body().String();
Log.v(标签,jsonData);
if(response.issusccessful()){
mForecast=parseForecastDetails(jsonData);
runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
updateDisplay();
}
});
}否则{
alertUserAboutError();
}
}捕获(IOE异常){
Log.e(标记“异常捕获:”,e);
}捕获(JSONException e){
Log.e(标记“异常捕获:”,e);
}
}
});
}
否则{
Toast.makeText(此,getString(R.string.network\u不可用\u消息),
Toast.LENGTH_LONG).show();
}
}
私有void toggleRefresh(){
if(mProgressBar.getVisibility()==View.INVISIBLE){
mProgressBar.setVisibility(View.VISIBLE);
mRefreshImageView.setVisibility(View.INVISIBLE);
}
否则{
mProgressBar.setVisibility(视图.不可见);
mRefreshImageView.setVisibility(View.VISIBLE);
}
}
私有void updateDisplay(){
Current-Current=mForecast.getCurrent();
mTemperatureLabel.setText(current.getTemperature()+);
mtimelab.setText(“在”+current.getFormattedTime()+“它将是”);
mHumidityValue.setText(current.get湿度()+);
mPrecipValue.setText(当前.getPrecipChance()+“%”);
msummarylab.setText(current.getSummary());
Drawable Drawable=getResources().getDrawable(current.getIconId());
mIconImageView.setImageDrawable(可绘制);
}
私有预测parseForecastDetails(字符串jsonData)抛出JSONException{
预测=新预测();
setCurrent(getCurrentDetails(jsonData));
forecast.setHourlyForecast(getHourlyForecast(jsonData));
setDailyForecast(getDailyForecast(jsonData));
收益预测;
}
私人日[]getDailyForecast(字符串jsonData)抛出JSONException{JSONObject forecast=新JSONObject(jsonData);
String timezone=forecast.getString(“时区”);
JSONObject daily=forecast.getJSONObject(“daily”);
JSONArray data=daily.getJSONArray(“数据”);
Day[]days=新的一天[data.length()];
对于(int i=0;iimport android.app.Application;
public class YourApplication extends Application {
private static YourApplication mInstance;
String city,adminArea;
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
}
public static YourApplication getInstance() {
return mInstance;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getAdminArea() {
return country;
}
public void setAdminArea(String adminArea) {
this.country = country;
}
}
YourApplication.getInstance().getCity();
YourApplication.getInstance().setCity("You city");
<application
android:name=".YourApplication" .....put this line in your manifest file>